2 * Copyright © 2006-2007 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Eric Anholt <eric@anholt.net>
27 #include <linux/dmi.h>
28 #include <linux/module.h>
29 #include <linux/i2c.h>
30 #include <linux/kernel.h>
31 #include <drm/drm_edid.h>
33 #include "intel_drv.h"
34 #include <drm/i915_drm.h>
36 #include "i915_trace.h"
37 #include <drm/drm_dp_helper.h>
38 #include <drm/drm_crtc_helper.h>
40 #define DIV_ROUND_CLOSEST_ULL(ll, d) \
41 ({ unsigned long long _tmp = (ll)+(d)/2; do_div(_tmp, d); _tmp; })
43 static void intel_increase_pllclock(struct drm_crtc *crtc);
44 static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
46 static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
47 struct intel_crtc_config *pipe_config);
48 static void ironlake_pch_clock_get(struct intel_crtc *crtc,
49 struct intel_crtc_config *pipe_config);
51 static int intel_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
52 int x, int y, struct drm_framebuffer *old_fb);
53 static int intel_framebuffer_init(struct drm_device *dev,
54 struct intel_framebuffer *ifb,
55 struct drm_mode_fb_cmd2 *mode_cmd,
56 struct drm_i915_gem_object *obj);
57 static void intel_dp_set_m_n(struct intel_crtc *crtc);
58 static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
59 static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
60 static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
61 struct intel_link_m_n *m_n);
62 static void ironlake_set_pipeconf(struct drm_crtc *crtc);
63 static void haswell_set_pipeconf(struct drm_crtc *crtc);
64 static void intel_set_pipe_csc(struct drm_crtc *crtc);
65 static void vlv_prepare_pll(struct intel_crtc *crtc);
76 typedef struct intel_limit intel_limit_t;
78 intel_range_t dot, vco, n, m, m1, m2, p, p1;
83 intel_pch_rawclk(struct drm_device *dev)
85 struct drm_i915_private *dev_priv = dev->dev_private;
87 WARN_ON(!HAS_PCH_SPLIT(dev));
89 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
92 static inline u32 /* units of 100MHz */
93 intel_fdi_link_freq(struct drm_device *dev)
96 struct drm_i915_private *dev_priv = dev->dev_private;
97 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
102 static const intel_limit_t intel_limits_i8xx_dac = {
103 .dot = { .min = 25000, .max = 350000 },
104 .vco = { .min = 908000, .max = 1512000 },
105 .n = { .min = 2, .max = 16 },
106 .m = { .min = 96, .max = 140 },
107 .m1 = { .min = 18, .max = 26 },
108 .m2 = { .min = 6, .max = 16 },
109 .p = { .min = 4, .max = 128 },
110 .p1 = { .min = 2, .max = 33 },
111 .p2 = { .dot_limit = 165000,
112 .p2_slow = 4, .p2_fast = 2 },
115 static const intel_limit_t intel_limits_i8xx_dvo = {
116 .dot = { .min = 25000, .max = 350000 },
117 .vco = { .min = 908000, .max = 1512000 },
118 .n = { .min = 2, .max = 16 },
119 .m = { .min = 96, .max = 140 },
120 .m1 = { .min = 18, .max = 26 },
121 .m2 = { .min = 6, .max = 16 },
122 .p = { .min = 4, .max = 128 },
123 .p1 = { .min = 2, .max = 33 },
124 .p2 = { .dot_limit = 165000,
125 .p2_slow = 4, .p2_fast = 4 },
128 static const intel_limit_t intel_limits_i8xx_lvds = {
129 .dot = { .min = 25000, .max = 350000 },
130 .vco = { .min = 908000, .max = 1512000 },
131 .n = { .min = 2, .max = 16 },
132 .m = { .min = 96, .max = 140 },
133 .m1 = { .min = 18, .max = 26 },
134 .m2 = { .min = 6, .max = 16 },
135 .p = { .min = 4, .max = 128 },
136 .p1 = { .min = 1, .max = 6 },
137 .p2 = { .dot_limit = 165000,
138 .p2_slow = 14, .p2_fast = 7 },
141 static const intel_limit_t intel_limits_i9xx_sdvo = {
142 .dot = { .min = 20000, .max = 400000 },
143 .vco = { .min = 1400000, .max = 2800000 },
144 .n = { .min = 1, .max = 6 },
145 .m = { .min = 70, .max = 120 },
146 .m1 = { .min = 8, .max = 18 },
147 .m2 = { .min = 3, .max = 7 },
148 .p = { .min = 5, .max = 80 },
149 .p1 = { .min = 1, .max = 8 },
150 .p2 = { .dot_limit = 200000,
151 .p2_slow = 10, .p2_fast = 5 },
154 static const intel_limit_t intel_limits_i9xx_lvds = {
155 .dot = { .min = 20000, .max = 400000 },
156 .vco = { .min = 1400000, .max = 2800000 },
157 .n = { .min = 1, .max = 6 },
158 .m = { .min = 70, .max = 120 },
159 .m1 = { .min = 8, .max = 18 },
160 .m2 = { .min = 3, .max = 7 },
161 .p = { .min = 7, .max = 98 },
162 .p1 = { .min = 1, .max = 8 },
163 .p2 = { .dot_limit = 112000,
164 .p2_slow = 14, .p2_fast = 7 },
168 static const intel_limit_t intel_limits_g4x_sdvo = {
169 .dot = { .min = 25000, .max = 270000 },
170 .vco = { .min = 1750000, .max = 3500000},
171 .n = { .min = 1, .max = 4 },
172 .m = { .min = 104, .max = 138 },
173 .m1 = { .min = 17, .max = 23 },
174 .m2 = { .min = 5, .max = 11 },
175 .p = { .min = 10, .max = 30 },
176 .p1 = { .min = 1, .max = 3},
177 .p2 = { .dot_limit = 270000,
183 static const intel_limit_t intel_limits_g4x_hdmi = {
184 .dot = { .min = 22000, .max = 400000 },
185 .vco = { .min = 1750000, .max = 3500000},
186 .n = { .min = 1, .max = 4 },
187 .m = { .min = 104, .max = 138 },
188 .m1 = { .min = 16, .max = 23 },
189 .m2 = { .min = 5, .max = 11 },
190 .p = { .min = 5, .max = 80 },
191 .p1 = { .min = 1, .max = 8},
192 .p2 = { .dot_limit = 165000,
193 .p2_slow = 10, .p2_fast = 5 },
196 static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
197 .dot = { .min = 20000, .max = 115000 },
198 .vco = { .min = 1750000, .max = 3500000 },
199 .n = { .min = 1, .max = 3 },
200 .m = { .min = 104, .max = 138 },
201 .m1 = { .min = 17, .max = 23 },
202 .m2 = { .min = 5, .max = 11 },
203 .p = { .min = 28, .max = 112 },
204 .p1 = { .min = 2, .max = 8 },
205 .p2 = { .dot_limit = 0,
206 .p2_slow = 14, .p2_fast = 14
210 static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
211 .dot = { .min = 80000, .max = 224000 },
212 .vco = { .min = 1750000, .max = 3500000 },
213 .n = { .min = 1, .max = 3 },
214 .m = { .min = 104, .max = 138 },
215 .m1 = { .min = 17, .max = 23 },
216 .m2 = { .min = 5, .max = 11 },
217 .p = { .min = 14, .max = 42 },
218 .p1 = { .min = 2, .max = 6 },
219 .p2 = { .dot_limit = 0,
220 .p2_slow = 7, .p2_fast = 7
224 static const intel_limit_t intel_limits_pineview_sdvo = {
225 .dot = { .min = 20000, .max = 400000},
226 .vco = { .min = 1700000, .max = 3500000 },
227 /* Pineview's Ncounter is a ring counter */
228 .n = { .min = 3, .max = 6 },
229 .m = { .min = 2, .max = 256 },
230 /* Pineview only has one combined m divider, which we treat as m2. */
231 .m1 = { .min = 0, .max = 0 },
232 .m2 = { .min = 0, .max = 254 },
233 .p = { .min = 5, .max = 80 },
234 .p1 = { .min = 1, .max = 8 },
235 .p2 = { .dot_limit = 200000,
236 .p2_slow = 10, .p2_fast = 5 },
239 static const intel_limit_t intel_limits_pineview_lvds = {
240 .dot = { .min = 20000, .max = 400000 },
241 .vco = { .min = 1700000, .max = 3500000 },
242 .n = { .min = 3, .max = 6 },
243 .m = { .min = 2, .max = 256 },
244 .m1 = { .min = 0, .max = 0 },
245 .m2 = { .min = 0, .max = 254 },
246 .p = { .min = 7, .max = 112 },
247 .p1 = { .min = 1, .max = 8 },
248 .p2 = { .dot_limit = 112000,
249 .p2_slow = 14, .p2_fast = 14 },
252 /* Ironlake / Sandybridge
254 * We calculate clock using (register_value + 2) for N/M1/M2, so here
255 * the range value for them is (actual_value - 2).
257 static const intel_limit_t intel_limits_ironlake_dac = {
258 .dot = { .min = 25000, .max = 350000 },
259 .vco = { .min = 1760000, .max = 3510000 },
260 .n = { .min = 1, .max = 5 },
261 .m = { .min = 79, .max = 127 },
262 .m1 = { .min = 12, .max = 22 },
263 .m2 = { .min = 5, .max = 9 },
264 .p = { .min = 5, .max = 80 },
265 .p1 = { .min = 1, .max = 8 },
266 .p2 = { .dot_limit = 225000,
267 .p2_slow = 10, .p2_fast = 5 },
270 static const intel_limit_t intel_limits_ironlake_single_lvds = {
271 .dot = { .min = 25000, .max = 350000 },
272 .vco = { .min = 1760000, .max = 3510000 },
273 .n = { .min = 1, .max = 3 },
274 .m = { .min = 79, .max = 118 },
275 .m1 = { .min = 12, .max = 22 },
276 .m2 = { .min = 5, .max = 9 },
277 .p = { .min = 28, .max = 112 },
278 .p1 = { .min = 2, .max = 8 },
279 .p2 = { .dot_limit = 225000,
280 .p2_slow = 14, .p2_fast = 14 },
283 static const intel_limit_t intel_limits_ironlake_dual_lvds = {
284 .dot = { .min = 25000, .max = 350000 },
285 .vco = { .min = 1760000, .max = 3510000 },
286 .n = { .min = 1, .max = 3 },
287 .m = { .min = 79, .max = 127 },
288 .m1 = { .min = 12, .max = 22 },
289 .m2 = { .min = 5, .max = 9 },
290 .p = { .min = 14, .max = 56 },
291 .p1 = { .min = 2, .max = 8 },
292 .p2 = { .dot_limit = 225000,
293 .p2_slow = 7, .p2_fast = 7 },
296 /* LVDS 100mhz refclk limits. */
297 static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
298 .dot = { .min = 25000, .max = 350000 },
299 .vco = { .min = 1760000, .max = 3510000 },
300 .n = { .min = 1, .max = 2 },
301 .m = { .min = 79, .max = 126 },
302 .m1 = { .min = 12, .max = 22 },
303 .m2 = { .min = 5, .max = 9 },
304 .p = { .min = 28, .max = 112 },
305 .p1 = { .min = 2, .max = 8 },
306 .p2 = { .dot_limit = 225000,
307 .p2_slow = 14, .p2_fast = 14 },
310 static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
311 .dot = { .min = 25000, .max = 350000 },
312 .vco = { .min = 1760000, .max = 3510000 },
313 .n = { .min = 1, .max = 3 },
314 .m = { .min = 79, .max = 126 },
315 .m1 = { .min = 12, .max = 22 },
316 .m2 = { .min = 5, .max = 9 },
317 .p = { .min = 14, .max = 42 },
318 .p1 = { .min = 2, .max = 6 },
319 .p2 = { .dot_limit = 225000,
320 .p2_slow = 7, .p2_fast = 7 },
323 static const intel_limit_t intel_limits_vlv = {
325 * These are the data rate limits (measured in fast clocks)
326 * since those are the strictest limits we have. The fast
327 * clock and actual rate limits are more relaxed, so checking
328 * them would make no difference.
330 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
331 .vco = { .min = 4000000, .max = 6000000 },
332 .n = { .min = 1, .max = 7 },
333 .m1 = { .min = 2, .max = 3 },
334 .m2 = { .min = 11, .max = 156 },
335 .p1 = { .min = 2, .max = 3 },
336 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
339 static const intel_limit_t intel_limits_chv = {
341 * These are the data rate limits (measured in fast clocks)
342 * since those are the strictest limits we have. The fast
343 * clock and actual rate limits are more relaxed, so checking
344 * them would make no difference.
346 .dot = { .min = 25000 * 5, .max = 540000 * 5},
347 .vco = { .min = 4860000, .max = 6700000 },
348 .n = { .min = 1, .max = 1 },
349 .m1 = { .min = 2, .max = 2 },
350 .m2 = { .min = 24 << 22, .max = 175 << 22 },
351 .p1 = { .min = 2, .max = 4 },
352 .p2 = { .p2_slow = 1, .p2_fast = 14 },
355 static void vlv_clock(int refclk, intel_clock_t *clock)
357 clock->m = clock->m1 * clock->m2;
358 clock->p = clock->p1 * clock->p2;
359 if (WARN_ON(clock->n == 0 || clock->p == 0))
361 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
362 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
366 * Returns whether any output on the specified pipe is of the specified type
368 static bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
370 struct drm_device *dev = crtc->dev;
371 struct intel_encoder *encoder;
373 for_each_encoder_on_crtc(dev, crtc, encoder)
374 if (encoder->type == type)
380 static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
383 struct drm_device *dev = crtc->dev;
384 const intel_limit_t *limit;
386 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
387 if (intel_is_dual_link_lvds(dev)) {
388 if (refclk == 100000)
389 limit = &intel_limits_ironlake_dual_lvds_100m;
391 limit = &intel_limits_ironlake_dual_lvds;
393 if (refclk == 100000)
394 limit = &intel_limits_ironlake_single_lvds_100m;
396 limit = &intel_limits_ironlake_single_lvds;
399 limit = &intel_limits_ironlake_dac;
404 static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
406 struct drm_device *dev = crtc->dev;
407 const intel_limit_t *limit;
409 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
410 if (intel_is_dual_link_lvds(dev))
411 limit = &intel_limits_g4x_dual_channel_lvds;
413 limit = &intel_limits_g4x_single_channel_lvds;
414 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
415 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
416 limit = &intel_limits_g4x_hdmi;
417 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
418 limit = &intel_limits_g4x_sdvo;
419 } else /* The option is for other outputs */
420 limit = &intel_limits_i9xx_sdvo;
425 static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
427 struct drm_device *dev = crtc->dev;
428 const intel_limit_t *limit;
430 if (HAS_PCH_SPLIT(dev))
431 limit = intel_ironlake_limit(crtc, refclk);
432 else if (IS_G4X(dev)) {
433 limit = intel_g4x_limit(crtc);
434 } else if (IS_PINEVIEW(dev)) {
435 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
436 limit = &intel_limits_pineview_lvds;
438 limit = &intel_limits_pineview_sdvo;
439 } else if (IS_CHERRYVIEW(dev)) {
440 limit = &intel_limits_chv;
441 } else if (IS_VALLEYVIEW(dev)) {
442 limit = &intel_limits_vlv;
443 } else if (!IS_GEN2(dev)) {
444 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
445 limit = &intel_limits_i9xx_lvds;
447 limit = &intel_limits_i9xx_sdvo;
449 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
450 limit = &intel_limits_i8xx_lvds;
451 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO))
452 limit = &intel_limits_i8xx_dvo;
454 limit = &intel_limits_i8xx_dac;
459 /* m1 is reserved as 0 in Pineview, n is a ring counter */
460 static void pineview_clock(int refclk, intel_clock_t *clock)
462 clock->m = clock->m2 + 2;
463 clock->p = clock->p1 * clock->p2;
464 if (WARN_ON(clock->n == 0 || clock->p == 0))
466 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
467 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
470 static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
472 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
475 static void i9xx_clock(int refclk, intel_clock_t *clock)
477 clock->m = i9xx_dpll_compute_m(clock);
478 clock->p = clock->p1 * clock->p2;
479 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
481 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
482 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
485 static void chv_clock(int refclk, intel_clock_t *clock)
487 clock->m = clock->m1 * clock->m2;
488 clock->p = clock->p1 * clock->p2;
489 if (WARN_ON(clock->n == 0 || clock->p == 0))
491 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
493 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
496 #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
498 * Returns whether the given set of divisors are valid for a given refclk with
499 * the given connectors.
502 static bool intel_PLL_is_valid(struct drm_device *dev,
503 const intel_limit_t *limit,
504 const intel_clock_t *clock)
506 if (clock->n < limit->n.min || limit->n.max < clock->n)
507 INTELPllInvalid("n out of range\n");
508 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
509 INTELPllInvalid("p1 out of range\n");
510 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
511 INTELPllInvalid("m2 out of range\n");
512 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
513 INTELPllInvalid("m1 out of range\n");
515 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev))
516 if (clock->m1 <= clock->m2)
517 INTELPllInvalid("m1 <= m2\n");
519 if (!IS_VALLEYVIEW(dev)) {
520 if (clock->p < limit->p.min || limit->p.max < clock->p)
521 INTELPllInvalid("p out of range\n");
522 if (clock->m < limit->m.min || limit->m.max < clock->m)
523 INTELPllInvalid("m out of range\n");
526 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
527 INTELPllInvalid("vco out of range\n");
528 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
529 * connector, etc., rather than just a single range.
531 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
532 INTELPllInvalid("dot out of range\n");
538 i9xx_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
539 int target, int refclk, intel_clock_t *match_clock,
540 intel_clock_t *best_clock)
542 struct drm_device *dev = crtc->dev;
546 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
548 * For LVDS just rely on its current settings for dual-channel.
549 * We haven't figured out how to reliably set up different
550 * single/dual channel state, if we even can.
552 if (intel_is_dual_link_lvds(dev))
553 clock.p2 = limit->p2.p2_fast;
555 clock.p2 = limit->p2.p2_slow;
557 if (target < limit->p2.dot_limit)
558 clock.p2 = limit->p2.p2_slow;
560 clock.p2 = limit->p2.p2_fast;
563 memset(best_clock, 0, sizeof(*best_clock));
565 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
567 for (clock.m2 = limit->m2.min;
568 clock.m2 <= limit->m2.max; clock.m2++) {
569 if (clock.m2 >= clock.m1)
571 for (clock.n = limit->n.min;
572 clock.n <= limit->n.max; clock.n++) {
573 for (clock.p1 = limit->p1.min;
574 clock.p1 <= limit->p1.max; clock.p1++) {
577 i9xx_clock(refclk, &clock);
578 if (!intel_PLL_is_valid(dev, limit,
582 clock.p != match_clock->p)
585 this_err = abs(clock.dot - target);
586 if (this_err < err) {
595 return (err != target);
599 pnv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
600 int target, int refclk, intel_clock_t *match_clock,
601 intel_clock_t *best_clock)
603 struct drm_device *dev = crtc->dev;
607 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
609 * For LVDS just rely on its current settings for dual-channel.
610 * We haven't figured out how to reliably set up different
611 * single/dual channel state, if we even can.
613 if (intel_is_dual_link_lvds(dev))
614 clock.p2 = limit->p2.p2_fast;
616 clock.p2 = limit->p2.p2_slow;
618 if (target < limit->p2.dot_limit)
619 clock.p2 = limit->p2.p2_slow;
621 clock.p2 = limit->p2.p2_fast;
624 memset(best_clock, 0, sizeof(*best_clock));
626 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
628 for (clock.m2 = limit->m2.min;
629 clock.m2 <= limit->m2.max; clock.m2++) {
630 for (clock.n = limit->n.min;
631 clock.n <= limit->n.max; clock.n++) {
632 for (clock.p1 = limit->p1.min;
633 clock.p1 <= limit->p1.max; clock.p1++) {
636 pineview_clock(refclk, &clock);
637 if (!intel_PLL_is_valid(dev, limit,
641 clock.p != match_clock->p)
644 this_err = abs(clock.dot - target);
645 if (this_err < err) {
654 return (err != target);
658 g4x_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
659 int target, int refclk, intel_clock_t *match_clock,
660 intel_clock_t *best_clock)
662 struct drm_device *dev = crtc->dev;
666 /* approximately equals target * 0.00585 */
667 int err_most = (target >> 8) + (target >> 9);
670 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
671 if (intel_is_dual_link_lvds(dev))
672 clock.p2 = limit->p2.p2_fast;
674 clock.p2 = limit->p2.p2_slow;
676 if (target < limit->p2.dot_limit)
677 clock.p2 = limit->p2.p2_slow;
679 clock.p2 = limit->p2.p2_fast;
682 memset(best_clock, 0, sizeof(*best_clock));
683 max_n = limit->n.max;
684 /* based on hardware requirement, prefer smaller n to precision */
685 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
686 /* based on hardware requirement, prefere larger m1,m2 */
687 for (clock.m1 = limit->m1.max;
688 clock.m1 >= limit->m1.min; clock.m1--) {
689 for (clock.m2 = limit->m2.max;
690 clock.m2 >= limit->m2.min; clock.m2--) {
691 for (clock.p1 = limit->p1.max;
692 clock.p1 >= limit->p1.min; clock.p1--) {
695 i9xx_clock(refclk, &clock);
696 if (!intel_PLL_is_valid(dev, limit,
700 this_err = abs(clock.dot - target);
701 if (this_err < err_most) {
715 vlv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
716 int target, int refclk, intel_clock_t *match_clock,
717 intel_clock_t *best_clock)
719 struct drm_device *dev = crtc->dev;
721 unsigned int bestppm = 1000000;
722 /* min update 19.2 MHz */
723 int max_n = min(limit->n.max, refclk / 19200);
726 target *= 5; /* fast clock */
728 memset(best_clock, 0, sizeof(*best_clock));
730 /* based on hardware requirement, prefer smaller n to precision */
731 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
732 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
733 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
734 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
735 clock.p = clock.p1 * clock.p2;
736 /* based on hardware requirement, prefer bigger m1,m2 values */
737 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
738 unsigned int ppm, diff;
740 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
743 vlv_clock(refclk, &clock);
745 if (!intel_PLL_is_valid(dev, limit,
749 diff = abs(clock.dot - target);
750 ppm = div_u64(1000000ULL * diff, target);
752 if (ppm < 100 && clock.p > best_clock->p) {
758 if (bestppm >= 10 && ppm < bestppm - 10) {
772 chv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
773 int target, int refclk, intel_clock_t *match_clock,
774 intel_clock_t *best_clock)
776 struct drm_device *dev = crtc->dev;
781 memset(best_clock, 0, sizeof(*best_clock));
784 * Based on hardware doc, the n always set to 1, and m1 always
785 * set to 2. If requires to support 200Mhz refclk, we need to
786 * revisit this because n may not 1 anymore.
788 clock.n = 1, clock.m1 = 2;
789 target *= 5; /* fast clock */
791 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
792 for (clock.p2 = limit->p2.p2_fast;
793 clock.p2 >= limit->p2.p2_slow;
794 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
796 clock.p = clock.p1 * clock.p2;
798 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
799 clock.n) << 22, refclk * clock.m1);
801 if (m2 > INT_MAX/clock.m1)
806 chv_clock(refclk, &clock);
808 if (!intel_PLL_is_valid(dev, limit, &clock))
811 /* based on hardware requirement, prefer bigger p
813 if (clock.p > best_clock->p) {
823 bool intel_crtc_active(struct drm_crtc *crtc)
825 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
827 /* Be paranoid as we can arrive here with only partial
828 * state retrieved from the hardware during setup.
830 * We can ditch the adjusted_mode.crtc_clock check as soon
831 * as Haswell has gained clock readout/fastboot support.
833 * We can ditch the crtc->primary->fb check as soon as we can
834 * properly reconstruct framebuffers.
836 return intel_crtc->active && crtc->primary->fb &&
837 intel_crtc->config.adjusted_mode.crtc_clock;
840 enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
843 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
844 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
846 return intel_crtc->config.cpu_transcoder;
849 static void g4x_wait_for_vblank(struct drm_device *dev, int pipe)
851 struct drm_i915_private *dev_priv = dev->dev_private;
852 u32 frame, frame_reg = PIPE_FRMCOUNT_GM45(pipe);
854 frame = I915_READ(frame_reg);
856 if (wait_for(I915_READ_NOTRACE(frame_reg) != frame, 50))
857 WARN(1, "vblank wait timed out\n");
861 * intel_wait_for_vblank - wait for vblank on a given pipe
863 * @pipe: pipe to wait for
865 * Wait for vblank to occur on a given pipe. Needed for various bits of
868 void intel_wait_for_vblank(struct drm_device *dev, int pipe)
870 struct drm_i915_private *dev_priv = dev->dev_private;
871 int pipestat_reg = PIPESTAT(pipe);
873 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
874 g4x_wait_for_vblank(dev, pipe);
878 /* Clear existing vblank status. Note this will clear any other
879 * sticky status fields as well.
881 * This races with i915_driver_irq_handler() with the result
882 * that either function could miss a vblank event. Here it is not
883 * fatal, as we will either wait upon the next vblank interrupt or
884 * timeout. Generally speaking intel_wait_for_vblank() is only
885 * called during modeset at which time the GPU should be idle and
886 * should *not* be performing page flips and thus not waiting on
888 * Currently, the result of us stealing a vblank from the irq
889 * handler is that a single frame will be skipped during swapbuffers.
891 I915_WRITE(pipestat_reg,
892 I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
894 /* Wait for vblank interrupt bit to set */
895 if (wait_for(I915_READ(pipestat_reg) &
896 PIPE_VBLANK_INTERRUPT_STATUS,
898 DRM_DEBUG_KMS("vblank wait timed out\n");
901 static bool pipe_dsl_stopped(struct drm_device *dev, enum i915_pipe pipe)
903 struct drm_i915_private *dev_priv = dev->dev_private;
904 u32 reg = PIPEDSL(pipe);
909 line_mask = DSL_LINEMASK_GEN2;
911 line_mask = DSL_LINEMASK_GEN3;
913 line1 = I915_READ(reg) & line_mask;
915 line2 = I915_READ(reg) & line_mask;
917 return line1 == line2;
921 * intel_wait_for_pipe_off - wait for pipe to turn off
923 * @pipe: pipe to wait for
925 * After disabling a pipe, we can't wait for vblank in the usual way,
926 * spinning on the vblank interrupt status bit, since we won't actually
927 * see an interrupt when the pipe is disabled.
930 * wait for the pipe register state bit to turn off
933 * wait for the display line value to settle (it usually
934 * ends up stopping at the start of the next frame).
937 void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
939 struct drm_i915_private *dev_priv = dev->dev_private;
940 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
943 if (INTEL_INFO(dev)->gen >= 4) {
944 int reg = PIPECONF(cpu_transcoder);
946 /* Wait for the Pipe State to go off */
947 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
949 WARN(1, "pipe_off wait timed out\n");
951 /* Wait for the display line to settle */
952 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
953 WARN(1, "pipe_off wait timed out\n");
958 * ibx_digital_port_connected - is the specified port connected?
959 * @dev_priv: i915 private structure
960 * @port: the port to test
962 * Returns true if @port is connected, false otherwise.
964 bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
965 struct intel_digital_port *port)
969 if (HAS_PCH_IBX(dev_priv->dev)) {
970 switch (port->port) {
972 bit = SDE_PORTB_HOTPLUG;
975 bit = SDE_PORTC_HOTPLUG;
978 bit = SDE_PORTD_HOTPLUG;
984 switch (port->port) {
986 bit = SDE_PORTB_HOTPLUG_CPT;
989 bit = SDE_PORTC_HOTPLUG_CPT;
992 bit = SDE_PORTD_HOTPLUG_CPT;
999 return I915_READ(SDEISR) & bit;
1002 static const char *state_string(bool enabled)
1004 return enabled ? "on" : "off";
1007 /* Only for pre-ILK configs */
1008 void assert_pll(struct drm_i915_private *dev_priv,
1009 enum i915_pipe pipe, bool state)
1016 val = I915_READ(reg);
1017 cur_state = !!(val & DPLL_VCO_ENABLE);
1018 WARN(cur_state != state,
1019 "PLL state assertion failure (expected %s, current %s)\n",
1020 state_string(state), state_string(cur_state));
1023 /* XXX: the dsi pll is shared between MIPI DSI ports */
1024 static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1029 mutex_lock(&dev_priv->dpio_lock);
1030 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
1031 mutex_unlock(&dev_priv->dpio_lock);
1033 cur_state = val & DSI_PLL_VCO_EN;
1034 WARN(cur_state != state,
1035 "DSI PLL state assertion failure (expected %s, current %s)\n",
1036 state_string(state), state_string(cur_state));
1038 #define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1039 #define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1041 struct intel_shared_dpll *
1042 intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
1044 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1046 if (crtc->config.shared_dpll < 0)
1049 return &dev_priv->shared_dplls[crtc->config.shared_dpll];
1053 void assert_shared_dpll(struct drm_i915_private *dev_priv,
1054 struct intel_shared_dpll *pll,
1058 struct intel_dpll_hw_state hw_state;
1060 if (HAS_PCH_LPT(dev_priv->dev)) {
1061 DRM_DEBUG_DRIVER("LPT detected: skipping PCH PLL test\n");
1066 "asserting DPLL %s with no DPLL\n", state_string(state)))
1069 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
1070 WARN(cur_state != state,
1071 "%s assertion failure (expected %s, current %s)\n",
1072 pll->name, state_string(state), state_string(cur_state));
1075 static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1076 enum i915_pipe pipe, bool state)
1081 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1084 if (HAS_DDI(dev_priv->dev)) {
1085 /* DDI does not have a specific FDI_TX register */
1086 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
1087 val = I915_READ(reg);
1088 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
1090 reg = FDI_TX_CTL(pipe);
1091 val = I915_READ(reg);
1092 cur_state = !!(val & FDI_TX_ENABLE);
1094 WARN(cur_state != state,
1095 "FDI TX state assertion failure (expected %s, current %s)\n",
1096 state_string(state), state_string(cur_state));
1098 #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1099 #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1101 static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1102 enum i915_pipe pipe, bool state)
1108 reg = FDI_RX_CTL(pipe);
1109 val = I915_READ(reg);
1110 cur_state = !!(val & FDI_RX_ENABLE);
1111 WARN(cur_state != state,
1112 "FDI RX state assertion failure (expected %s, current %s)\n",
1113 state_string(state), state_string(cur_state));
1115 #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1116 #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1118 static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1119 enum i915_pipe pipe)
1124 /* ILK FDI PLL is always enabled */
1125 if (INTEL_INFO(dev_priv->dev)->gen == 5)
1128 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
1129 if (HAS_DDI(dev_priv->dev))
1132 reg = FDI_TX_CTL(pipe);
1133 val = I915_READ(reg);
1134 WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
1137 void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1138 enum i915_pipe pipe, bool state)
1144 reg = FDI_RX_CTL(pipe);
1145 val = I915_READ(reg);
1146 cur_state = !!(val & FDI_RX_PLL_ENABLE);
1147 WARN(cur_state != state,
1148 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1149 state_string(state), state_string(cur_state));
1152 static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1153 enum i915_pipe pipe)
1155 int pp_reg, lvds_reg;
1157 enum i915_pipe panel_pipe = PIPE_A;
1160 if (HAS_PCH_SPLIT(dev_priv->dev)) {
1161 pp_reg = PCH_PP_CONTROL;
1162 lvds_reg = PCH_LVDS;
1164 pp_reg = PP_CONTROL;
1168 val = I915_READ(pp_reg);
1169 if (!(val & PANEL_POWER_ON) ||
1170 ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
1173 if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
1174 panel_pipe = PIPE_B;
1176 WARN(panel_pipe == pipe && locked,
1177 "panel assertion failure, pipe %c regs locked\n",
1181 static void assert_cursor(struct drm_i915_private *dev_priv,
1182 enum i915_pipe pipe, bool state)
1184 struct drm_device *dev = dev_priv->dev;
1187 if (IS_845G(dev) || IS_I865G(dev))
1188 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
1190 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
1192 WARN(cur_state != state,
1193 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1194 pipe_name(pipe), state_string(state), state_string(cur_state));
1196 #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1197 #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1199 void assert_pipe(struct drm_i915_private *dev_priv,
1200 enum i915_pipe pipe, bool state)
1205 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1208 /* if we need the pipe A quirk it must be always on */
1209 if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
1212 if (!intel_display_power_enabled(dev_priv,
1213 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
1216 reg = PIPECONF(cpu_transcoder);
1217 val = I915_READ(reg);
1218 cur_state = !!(val & PIPECONF_ENABLE);
1221 WARN(cur_state != state,
1222 "pipe %c assertion failure (expected %s, current %s)\n",
1223 pipe_name(pipe), state_string(state), state_string(cur_state));
1226 static void assert_plane(struct drm_i915_private *dev_priv,
1227 enum plane plane, bool state)
1233 reg = DSPCNTR(plane);
1234 val = I915_READ(reg);
1235 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
1236 WARN(cur_state != state,
1237 "plane %c assertion failure (expected %s, current %s)\n",
1238 plane_name(plane), state_string(state), state_string(cur_state));
1241 #define assert_plane_enabled(d, p) assert_plane(d, p, true)
1242 #define assert_plane_disabled(d, p) assert_plane(d, p, false)
1244 static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1245 enum i915_pipe pipe)
1247 struct drm_device *dev = dev_priv->dev;
1252 /* Primary planes are fixed to pipes on gen4+ */
1253 if (INTEL_INFO(dev)->gen >= 4) {
1254 reg = DSPCNTR(pipe);
1255 val = I915_READ(reg);
1256 WARN(val & DISPLAY_PLANE_ENABLE,
1257 "plane %c assertion failure, should be disabled but not\n",
1262 /* Need to check both planes against the pipe */
1265 val = I915_READ(reg);
1266 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1267 DISPPLANE_SEL_PIPE_SHIFT;
1268 WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
1269 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1270 plane_name(i), pipe_name(pipe));
1274 static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1275 enum i915_pipe pipe)
1277 struct drm_device *dev = dev_priv->dev;
1281 if (IS_VALLEYVIEW(dev)) {
1282 for_each_sprite(pipe, sprite) {
1283 reg = SPCNTR(pipe, sprite);
1284 val = I915_READ(reg);
1285 WARN(val & SP_ENABLE,
1286 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1287 sprite_name(pipe, sprite), pipe_name(pipe));
1289 } else if (INTEL_INFO(dev)->gen >= 7) {
1291 val = I915_READ(reg);
1292 WARN(val & SPRITE_ENABLE,
1293 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1294 plane_name(pipe), pipe_name(pipe));
1295 } else if (INTEL_INFO(dev)->gen >= 5) {
1296 reg = DVSCNTR(pipe);
1297 val = I915_READ(reg);
1298 WARN(val & DVS_ENABLE,
1299 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1300 plane_name(pipe), pipe_name(pipe));
1304 static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
1309 WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
1311 val = I915_READ(PCH_DREF_CONTROL);
1312 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1313 DREF_SUPERSPREAD_SOURCE_MASK));
1314 WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
1317 static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1318 enum i915_pipe pipe)
1324 reg = PCH_TRANSCONF(pipe);
1325 val = I915_READ(reg);
1326 enabled = !!(val & TRANS_ENABLE);
1328 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1332 static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1333 enum i915_pipe pipe, u32 port_sel, u32 val)
1335 if ((val & DP_PORT_EN) == 0)
1338 if (HAS_PCH_CPT(dev_priv->dev)) {
1339 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1340 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1341 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1343 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1344 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1347 if ((val & DP_PIPE_MASK) != (pipe << 30))
1353 static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1354 enum i915_pipe pipe, u32 val)
1356 if ((val & SDVO_ENABLE) == 0)
1359 if (HAS_PCH_CPT(dev_priv->dev)) {
1360 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
1362 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1363 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1366 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
1372 static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1373 enum i915_pipe pipe, u32 val)
1375 if ((val & LVDS_PORT_EN) == 0)
1378 if (HAS_PCH_CPT(dev_priv->dev)) {
1379 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1382 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1388 static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1389 enum i915_pipe pipe, u32 val)
1391 if ((val & ADPA_DAC_ENABLE) == 0)
1393 if (HAS_PCH_CPT(dev_priv->dev)) {
1394 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1397 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1403 static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
1404 enum i915_pipe pipe, int reg, u32 port_sel)
1406 u32 val = I915_READ(reg);
1407 WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
1408 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
1409 reg, pipe_name(pipe));
1411 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
1412 && (val & DP_PIPEB_SELECT),
1413 "IBX PCH dp port still using transcoder B\n");
1416 static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1417 enum i915_pipe pipe, int reg)
1419 u32 val = I915_READ(reg);
1420 WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
1421 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
1422 reg, pipe_name(pipe));
1424 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
1425 && (val & SDVO_PIPE_B_SELECT),
1426 "IBX PCH hdmi port still using transcoder B\n");
1429 static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1430 enum i915_pipe pipe)
1435 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1436 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1437 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
1440 val = I915_READ(reg);
1441 WARN(adpa_pipe_enabled(dev_priv, pipe, val),
1442 "PCH VGA enabled on transcoder %c, should be disabled\n",
1446 val = I915_READ(reg);
1447 WARN(lvds_pipe_enabled(dev_priv, pipe, val),
1448 "PCH LVDS enabled on transcoder %c, should be disabled\n",
1451 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1452 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1453 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
1456 static void intel_init_dpio(struct drm_device *dev)
1458 struct drm_i915_private *dev_priv = dev->dev_private;
1460 if (!IS_VALLEYVIEW(dev))
1464 * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
1465 * CHV x1 PHY (DP/HDMI D)
1466 * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
1468 if (IS_CHERRYVIEW(dev)) {
1469 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
1470 DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
1472 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
1476 static void intel_reset_dpio(struct drm_device *dev)
1478 struct drm_i915_private *dev_priv = dev->dev_private;
1480 if (!IS_VALLEYVIEW(dev))
1483 if (IS_CHERRYVIEW(dev)) {
1487 for (phy = DPIO_PHY0; phy < I915_NUM_PHYS_VLV; phy++) {
1488 /* Poll for phypwrgood signal */
1489 if (wait_for(I915_READ(DISPLAY_PHY_STATUS) &
1490 PHY_POWERGOOD(phy), 1))
1491 DRM_ERROR("Display PHY %d is not power up\n", phy);
1494 * Deassert common lane reset for PHY.
1496 * This should only be done on init and resume from S3
1497 * with both PLLs disabled, or we risk losing DPIO and
1498 * PLL synchronization.
1500 val = I915_READ(DISPLAY_PHY_CONTROL);
1501 I915_WRITE(DISPLAY_PHY_CONTROL,
1502 PHY_COM_LANE_RESET_DEASSERT(phy, val));
1507 * If DPIO has already been reset, e.g. by BIOS, just skip all
1510 if (I915_READ(DPIO_CTL) & DPIO_CMNRST)
1514 * From VLV2A0_DP_eDP_HDMI_DPIO_driver_vbios_notes_11.docx:
1515 * Need to assert and de-assert PHY SB reset by gating the
1516 * common lane power, then un-gating it.
1517 * Simply ungating isn't enough to reset the PHY enough to get
1518 * ports and lanes running.
1520 __vlv_set_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_BC,
1522 __vlv_set_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_BC,
1527 static void vlv_enable_pll(struct intel_crtc *crtc)
1529 struct drm_device *dev = crtc->base.dev;
1530 struct drm_i915_private *dev_priv = dev->dev_private;
1531 int reg = DPLL(crtc->pipe);
1532 u32 dpll = crtc->config.dpll_hw_state.dpll;
1534 assert_pipe_disabled(dev_priv, crtc->pipe);
1536 /* No really, not for ILK+ */
1537 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1539 /* PLL is protected by panel, make sure we can write it */
1540 if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
1541 assert_panel_unlocked(dev_priv, crtc->pipe);
1543 I915_WRITE(reg, dpll);
1547 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1548 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1550 I915_WRITE(DPLL_MD(crtc->pipe), crtc->config.dpll_hw_state.dpll_md);
1551 POSTING_READ(DPLL_MD(crtc->pipe));
1553 /* We do this three times for luck */
1554 I915_WRITE(reg, dpll);
1556 udelay(150); /* wait for warmup */
1557 I915_WRITE(reg, dpll);
1559 udelay(150); /* wait for warmup */
1560 I915_WRITE(reg, dpll);
1562 udelay(150); /* wait for warmup */
1565 static void chv_enable_pll(struct intel_crtc *crtc)
1567 struct drm_device *dev = crtc->base.dev;
1568 struct drm_i915_private *dev_priv = dev->dev_private;
1569 int pipe = crtc->pipe;
1570 enum dpio_channel port = vlv_pipe_to_channel(pipe);
1573 assert_pipe_disabled(dev_priv, crtc->pipe);
1575 BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
1577 mutex_lock(&dev_priv->dpio_lock);
1579 /* Enable back the 10bit clock to display controller */
1580 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1581 tmp |= DPIO_DCLKP_EN;
1582 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1585 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1590 I915_WRITE(DPLL(pipe), crtc->config.dpll_hw_state.dpll);
1592 /* Check PLL is locked */
1593 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1594 DRM_ERROR("PLL %d failed to lock\n", pipe);
1596 /* not sure when this should be written */
1597 I915_WRITE(DPLL_MD(pipe), crtc->config.dpll_hw_state.dpll_md);
1598 POSTING_READ(DPLL_MD(pipe));
1600 mutex_unlock(&dev_priv->dpio_lock);
1603 static void i9xx_enable_pll(struct intel_crtc *crtc)
1605 struct drm_device *dev = crtc->base.dev;
1606 struct drm_i915_private *dev_priv = dev->dev_private;
1607 int reg = DPLL(crtc->pipe);
1608 u32 dpll = crtc->config.dpll_hw_state.dpll;
1610 assert_pipe_disabled(dev_priv, crtc->pipe);
1612 /* No really, not for ILK+ */
1613 BUG_ON(INTEL_INFO(dev)->gen >= 5);
1615 /* PLL is protected by panel, make sure we can write it */
1616 if (IS_MOBILE(dev) && !IS_I830(dev))
1617 assert_panel_unlocked(dev_priv, crtc->pipe);
1619 I915_WRITE(reg, dpll);
1621 /* Wait for the clocks to stabilize. */
1625 if (INTEL_INFO(dev)->gen >= 4) {
1626 I915_WRITE(DPLL_MD(crtc->pipe),
1627 crtc->config.dpll_hw_state.dpll_md);
1629 /* The pixel multiplier can only be updated once the
1630 * DPLL is enabled and the clocks are stable.
1632 * So write it again.
1634 I915_WRITE(reg, dpll);
1637 /* We do this three times for luck */
1638 I915_WRITE(reg, dpll);
1640 udelay(150); /* wait for warmup */
1641 I915_WRITE(reg, dpll);
1643 udelay(150); /* wait for warmup */
1644 I915_WRITE(reg, dpll);
1646 udelay(150); /* wait for warmup */
1650 * i9xx_disable_pll - disable a PLL
1651 * @dev_priv: i915 private structure
1652 * @pipe: pipe PLL to disable
1654 * Disable the PLL for @pipe, making sure the pipe is off first.
1656 * Note! This is for pre-ILK only.
1658 static void i9xx_disable_pll(struct drm_i915_private *dev_priv, enum i915_pipe pipe)
1660 /* Don't disable pipe A or pipe A PLLs if needed */
1661 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1664 /* Make sure the pipe isn't still relying on us */
1665 assert_pipe_disabled(dev_priv, pipe);
1667 I915_WRITE(DPLL(pipe), 0);
1668 POSTING_READ(DPLL(pipe));
1671 static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum i915_pipe pipe)
1675 /* Make sure the pipe isn't still relying on us */
1676 assert_pipe_disabled(dev_priv, pipe);
1679 * Leave integrated clock source and reference clock enabled for pipe B.
1680 * The latter is needed for VGA hotplug / manual detection.
1683 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
1684 I915_WRITE(DPLL(pipe), val);
1685 POSTING_READ(DPLL(pipe));
1689 static void chv_disable_pll(struct drm_i915_private *dev_priv, enum i915_pipe pipe)
1691 enum dpio_channel port = vlv_pipe_to_channel(pipe);
1694 /* Make sure the pipe isn't still relying on us */
1695 assert_pipe_disabled(dev_priv, pipe);
1697 /* Set PLL en = 0 */
1698 val = DPLL_SSC_REF_CLOCK_CHV;
1700 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1701 I915_WRITE(DPLL(pipe), val);
1702 POSTING_READ(DPLL(pipe));
1704 mutex_lock(&dev_priv->dpio_lock);
1706 /* Disable 10bit clock to display controller */
1707 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1708 val &= ~DPIO_DCLKP_EN;
1709 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1711 mutex_unlock(&dev_priv->dpio_lock);
1714 void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
1715 struct intel_digital_port *dport)
1720 switch (dport->port) {
1722 port_mask = DPLL_PORTB_READY_MASK;
1726 port_mask = DPLL_PORTC_READY_MASK;
1730 port_mask = DPLL_PORTD_READY_MASK;
1731 dpll_reg = DPIO_PHY_STATUS;
1737 if (wait_for((I915_READ(dpll_reg) & port_mask) == 0, 1000))
1738 WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
1739 port_name(dport->port), I915_READ(dpll_reg));
1742 static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1744 struct drm_device *dev = crtc->base.dev;
1745 struct drm_i915_private *dev_priv = dev->dev_private;
1746 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1748 WARN_ON(!pll->refcount);
1749 if (pll->active == 0) {
1750 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1752 assert_shared_dpll_disabled(dev_priv, pll);
1754 pll->mode_set(dev_priv, pll);
1759 * intel_enable_shared_dpll - enable PCH PLL
1760 * @dev_priv: i915 private structure
1761 * @pipe: pipe PLL to enable
1763 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1764 * drives the transcoder clock.
1766 static void intel_enable_shared_dpll(struct intel_crtc *crtc)
1768 struct drm_device *dev = crtc->base.dev;
1769 struct drm_i915_private *dev_priv = dev->dev_private;
1770 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1772 if (WARN_ON(pll == NULL))
1775 if (WARN_ON(pll->refcount == 0))
1778 DRM_DEBUG_KMS("enable %s (active %d, on? %d)for crtc %d\n",
1779 pll->name, pll->active, pll->on,
1780 crtc->base.base.id);
1782 if (pll->active++) {
1784 assert_shared_dpll_enabled(dev_priv, pll);
1789 DRM_DEBUG_KMS("enabling %s\n", pll->name);
1790 pll->enable(dev_priv, pll);
1794 static void intel_disable_shared_dpll(struct intel_crtc *crtc)
1796 struct drm_device *dev = crtc->base.dev;
1797 struct drm_i915_private *dev_priv = dev->dev_private;
1798 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1800 /* PCH only available on ILK+ */
1801 BUG_ON(INTEL_INFO(dev)->gen < 5);
1802 if (WARN_ON(pll == NULL))
1805 if (WARN_ON(pll->refcount == 0))
1808 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1809 pll->name, pll->active, pll->on,
1810 crtc->base.base.id);
1812 if (WARN_ON(pll->active == 0)) {
1813 assert_shared_dpll_disabled(dev_priv, pll);
1817 assert_shared_dpll_enabled(dev_priv, pll);
1822 DRM_DEBUG_KMS("disabling %s\n", pll->name);
1823 pll->disable(dev_priv, pll);
1827 static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1828 enum i915_pipe pipe)
1830 struct drm_device *dev = dev_priv->dev;
1831 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1832 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1833 uint32_t reg, val, pipeconf_val;
1835 /* PCH only available on ILK+ */
1836 BUG_ON(INTEL_INFO(dev)->gen < 5);
1838 /* Make sure PCH DPLL is enabled */
1839 assert_shared_dpll_enabled(dev_priv,
1840 intel_crtc_to_shared_dpll(intel_crtc));
1842 /* FDI must be feeding us bits for PCH ports */
1843 assert_fdi_tx_enabled(dev_priv, pipe);
1844 assert_fdi_rx_enabled(dev_priv, pipe);
1846 if (HAS_PCH_CPT(dev)) {
1847 /* Workaround: Set the timing override bit before enabling the
1848 * pch transcoder. */
1849 reg = TRANS_CHICKEN2(pipe);
1850 val = I915_READ(reg);
1851 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1852 I915_WRITE(reg, val);
1855 reg = PCH_TRANSCONF(pipe);
1856 val = I915_READ(reg);
1857 pipeconf_val = I915_READ(PIPECONF(pipe));
1859 if (HAS_PCH_IBX(dev_priv->dev)) {
1861 * make the BPC in transcoder be consistent with
1862 * that in pipeconf reg.
1864 val &= ~PIPECONF_BPC_MASK;
1865 val |= pipeconf_val & PIPECONF_BPC_MASK;
1868 val &= ~TRANS_INTERLACE_MASK;
1869 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
1870 if (HAS_PCH_IBX(dev_priv->dev) &&
1871 intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
1872 val |= TRANS_LEGACY_INTERLACED_ILK;
1874 val |= TRANS_INTERLACED;
1876 val |= TRANS_PROGRESSIVE;
1878 I915_WRITE(reg, val | TRANS_ENABLE);
1879 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
1880 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
1883 static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1884 enum transcoder cpu_transcoder)
1886 u32 val, pipeconf_val;
1888 /* PCH only available on ILK+ */
1889 BUG_ON(INTEL_INFO(dev_priv->dev)->gen < 5);
1891 /* FDI must be feeding us bits for PCH ports */
1892 assert_fdi_tx_enabled(dev_priv, (enum i915_pipe) cpu_transcoder);
1893 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
1895 /* Workaround: set timing override bit. */
1896 val = I915_READ(_TRANSA_CHICKEN2);
1897 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1898 I915_WRITE(_TRANSA_CHICKEN2, val);
1901 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
1903 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1904 PIPECONF_INTERLACED_ILK)
1905 val |= TRANS_INTERLACED;
1907 val |= TRANS_PROGRESSIVE;
1909 I915_WRITE(LPT_TRANSCONF, val);
1910 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
1911 DRM_ERROR("Failed to enable PCH transcoder\n");
1914 static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1915 enum i915_pipe pipe)
1917 struct drm_device *dev = dev_priv->dev;
1920 /* FDI relies on the transcoder */
1921 assert_fdi_tx_disabled(dev_priv, pipe);
1922 assert_fdi_rx_disabled(dev_priv, pipe);
1924 /* Ports must be off as well */
1925 assert_pch_ports_disabled(dev_priv, pipe);
1927 reg = PCH_TRANSCONF(pipe);
1928 val = I915_READ(reg);
1929 val &= ~TRANS_ENABLE;
1930 I915_WRITE(reg, val);
1931 /* wait for PCH transcoder off, transcoder state */
1932 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
1933 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
1935 if (!HAS_PCH_IBX(dev)) {
1936 /* Workaround: Clear the timing override chicken bit again. */
1937 reg = TRANS_CHICKEN2(pipe);
1938 val = I915_READ(reg);
1939 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1940 I915_WRITE(reg, val);
1944 static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
1948 val = I915_READ(LPT_TRANSCONF);
1949 val &= ~TRANS_ENABLE;
1950 I915_WRITE(LPT_TRANSCONF, val);
1951 /* wait for PCH transcoder off, transcoder state */
1952 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
1953 DRM_ERROR("Failed to disable PCH transcoder\n");
1955 /* Workaround: clear timing override bit. */
1956 val = I915_READ(_TRANSA_CHICKEN2);
1957 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1958 I915_WRITE(_TRANSA_CHICKEN2, val);
1962 * intel_enable_pipe - enable a pipe, asserting requirements
1963 * @crtc: crtc responsible for the pipe
1965 * Enable @crtc's pipe, making sure that various hardware specific requirements
1966 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
1968 static void intel_enable_pipe(struct intel_crtc *crtc)
1970 struct drm_device *dev = crtc->base.dev;
1971 struct drm_i915_private *dev_priv = dev->dev_private;
1972 enum i915_pipe pipe = crtc->pipe;
1973 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1975 enum i915_pipe pch_transcoder;
1979 assert_planes_disabled(dev_priv, pipe);
1980 assert_cursor_disabled(dev_priv, pipe);
1981 assert_sprites_disabled(dev_priv, pipe);
1983 if (HAS_PCH_LPT(dev_priv->dev))
1984 pch_transcoder = TRANSCODER_A;
1986 pch_transcoder = pipe;
1989 * A pipe without a PLL won't actually be able to drive bits from
1990 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1993 if (!HAS_PCH_SPLIT(dev_priv->dev))
1994 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DSI))
1995 assert_dsi_pll_enabled(dev_priv);
1997 assert_pll_enabled(dev_priv, pipe);
1999 if (crtc->config.has_pch_encoder) {
2000 /* if driving the PCH, we need FDI enabled */
2001 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
2002 assert_fdi_tx_pll_enabled(dev_priv,
2003 (enum i915_pipe) cpu_transcoder);
2005 /* FIXME: assert CPU port conditions for SNB+ */
2008 reg = PIPECONF(cpu_transcoder);
2009 val = I915_READ(reg);
2010 if (val & PIPECONF_ENABLE) {
2011 WARN_ON(!(pipe == PIPE_A &&
2012 dev_priv->quirks & QUIRK_PIPEA_FORCE));
2016 I915_WRITE(reg, val | PIPECONF_ENABLE);
2021 * intel_disable_pipe - disable a pipe, asserting requirements
2022 * @dev_priv: i915 private structure
2023 * @pipe: pipe to disable
2025 * Disable @pipe, making sure that various hardware specific requirements
2026 * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
2028 * @pipe should be %PIPE_A or %PIPE_B.
2030 * Will wait until the pipe has shut down before returning.
2032 static void intel_disable_pipe(struct drm_i915_private *dev_priv,
2033 enum i915_pipe pipe)
2035 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
2041 * Make sure planes won't keep trying to pump pixels to us,
2042 * or we might hang the display.
2044 assert_planes_disabled(dev_priv, pipe);
2045 assert_cursor_disabled(dev_priv, pipe);
2046 assert_sprites_disabled(dev_priv, pipe);
2048 /* Don't disable pipe A or pipe A PLLs if needed */
2049 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
2052 reg = PIPECONF(cpu_transcoder);
2053 val = I915_READ(reg);
2054 if ((val & PIPECONF_ENABLE) == 0)
2057 I915_WRITE(reg, val & ~PIPECONF_ENABLE);
2058 intel_wait_for_pipe_off(dev_priv->dev, pipe);
2062 * Plane regs are double buffered, going from enabled->disabled needs a
2063 * trigger in order to latch. The display address reg provides this.
2065 void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
2068 struct drm_device *dev = dev_priv->dev;
2069 u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
2071 I915_WRITE(reg, I915_READ(reg));
2076 * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
2077 * @dev_priv: i915 private structure
2078 * @plane: plane to enable
2079 * @pipe: pipe being fed
2081 * Enable @plane on @pipe, making sure that @pipe is running first.
2083 static void intel_enable_primary_hw_plane(struct drm_i915_private *dev_priv,
2084 enum plane plane, enum i915_pipe pipe)
2086 struct drm_device *dev = dev_priv->dev;
2087 struct intel_crtc *intel_crtc =
2088 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
2092 /* If the pipe isn't enabled, we can't pump pixels and may hang */
2093 assert_pipe_enabled(dev_priv, pipe);
2095 if (intel_crtc->primary_enabled)
2098 intel_crtc->primary_enabled = true;
2100 reg = DSPCNTR(plane);
2101 val = I915_READ(reg);
2102 WARN_ON(val & DISPLAY_PLANE_ENABLE);
2104 I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
2105 intel_flush_primary_plane(dev_priv, plane);
2108 * BDW signals flip done immediately if the plane
2109 * is disabled, even if the plane enable is already
2110 * armed to occur at the next vblank :(
2112 if (IS_BROADWELL(dev))
2113 intel_wait_for_vblank(dev, intel_crtc->pipe);
2117 * intel_disable_primary_hw_plane - disable the primary hardware plane
2118 * @dev_priv: i915 private structure
2119 * @plane: plane to disable
2120 * @pipe: pipe consuming the data
2122 * Disable @plane; should be an independent operation.
2124 static void intel_disable_primary_hw_plane(struct drm_i915_private *dev_priv,
2125 enum plane plane, enum i915_pipe pipe)
2127 struct intel_crtc *intel_crtc =
2128 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
2132 if (!intel_crtc->primary_enabled)
2135 intel_crtc->primary_enabled = false;
2137 reg = DSPCNTR(plane);
2138 val = I915_READ(reg);
2139 WARN_ON((val & DISPLAY_PLANE_ENABLE) == 0);
2141 I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
2142 intel_flush_primary_plane(dev_priv, plane);
2145 static bool need_vtd_wa(struct drm_device *dev)
2147 #ifdef CONFIG_INTEL_IOMMU
2148 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2154 static int intel_align_height(struct drm_device *dev, int height, bool tiled)
2158 tile_height = tiled ? (IS_GEN2(dev) ? 16 : 8) : 1;
2159 return ALIGN(height, tile_height);
2163 intel_pin_and_fence_fb_obj(struct drm_device *dev,
2164 struct drm_i915_gem_object *obj,
2165 struct intel_engine_cs *pipelined)
2167 struct drm_i915_private *dev_priv = dev->dev_private;
2171 switch (obj->tiling_mode) {
2172 case I915_TILING_NONE:
2173 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
2174 alignment = 128 * 1024;
2175 else if (INTEL_INFO(dev)->gen >= 4)
2176 alignment = 4 * 1024;
2178 alignment = 64 * 1024;
2181 /* pin() will align the object as required by fence */
2185 WARN(1, "Y tiled bo slipped through, driver bug!\n");
2191 /* Note that the w/a also requires 64 PTE of padding following the
2192 * bo. We currently fill all unused PTE with the shadow page and so
2193 * we should always have valid PTE following the scanout preventing
2196 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2197 alignment = 256 * 1024;
2199 dev_priv->mm.interruptible = false;
2200 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
2202 goto err_interruptible;
2204 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2205 * fence, whereas 965+ only requires a fence if using
2206 * framebuffer compression. For simplicity, we always install
2207 * a fence as the cost is not that onerous.
2209 ret = i915_gem_object_get_fence(obj);
2213 i915_gem_object_pin_fence(obj);
2215 dev_priv->mm.interruptible = true;
2219 i915_gem_object_unpin_from_display_plane(obj);
2221 dev_priv->mm.interruptible = true;
2225 void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
2227 i915_gem_object_unpin_fence(obj);
2228 i915_gem_object_unpin_from_display_plane(obj);
2231 /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2232 * is assumed to be a power-of-two. */
2233 unsigned long intel_gen4_compute_page_offset(int *x, int *y,
2234 unsigned int tiling_mode,
2238 if (tiling_mode != I915_TILING_NONE) {
2239 unsigned int tile_rows, tiles;
2244 tiles = *x / (512/cpp);
2247 return tile_rows * pitch * 8 + tiles * 4096;
2249 unsigned int offset;
2251 offset = *y * pitch + *x * cpp;
2253 *x = (offset & 4095) / cpp;
2254 return offset & -4096;
2258 int intel_format_to_fourcc(int format)
2261 case DISPPLANE_8BPP:
2262 return DRM_FORMAT_C8;
2263 case DISPPLANE_BGRX555:
2264 return DRM_FORMAT_XRGB1555;
2265 case DISPPLANE_BGRX565:
2266 return DRM_FORMAT_RGB565;
2268 case DISPPLANE_BGRX888:
2269 return DRM_FORMAT_XRGB8888;
2270 case DISPPLANE_RGBX888:
2271 return DRM_FORMAT_XBGR8888;
2272 case DISPPLANE_BGRX101010:
2273 return DRM_FORMAT_XRGB2101010;
2274 case DISPPLANE_RGBX101010:
2275 return DRM_FORMAT_XBGR2101010;
2279 static bool intel_alloc_plane_obj(struct intel_crtc *crtc,
2280 struct intel_plane_config *plane_config)
2282 struct drm_device *dev = crtc->base.dev;
2283 struct drm_i915_gem_object *obj = NULL;
2284 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2285 u32 base = plane_config->base;
2287 if (plane_config->size == 0)
2290 obj = i915_gem_object_create_stolen_for_preallocated(dev, base, base,
2291 plane_config->size);
2295 if (plane_config->tiled) {
2296 obj->tiling_mode = I915_TILING_X;
2297 obj->stride = crtc->base.primary->fb->pitches[0];
2300 mode_cmd.pixel_format = crtc->base.primary->fb->pixel_format;
2301 mode_cmd.width = crtc->base.primary->fb->width;
2302 mode_cmd.height = crtc->base.primary->fb->height;
2303 mode_cmd.pitches[0] = crtc->base.primary->fb->pitches[0];
2305 mutex_lock(&dev->struct_mutex);
2307 if (intel_framebuffer_init(dev, to_intel_framebuffer(crtc->base.primary->fb),
2309 DRM_DEBUG_KMS("intel fb init failed\n");
2313 mutex_unlock(&dev->struct_mutex);
2315 DRM_DEBUG_KMS("plane fb obj %p\n", obj);
2319 drm_gem_object_unreference(&obj->base);
2320 mutex_unlock(&dev->struct_mutex);
2324 static void intel_find_plane_obj(struct intel_crtc *intel_crtc,
2325 struct intel_plane_config *plane_config)
2327 struct drm_device *dev = intel_crtc->base.dev;
2329 struct intel_crtc *i;
2330 struct intel_framebuffer *fb;
2332 if (!intel_crtc->base.primary->fb)
2335 if (intel_alloc_plane_obj(intel_crtc, plane_config))
2338 kfree(intel_crtc->base.primary->fb);
2339 intel_crtc->base.primary->fb = NULL;
2342 * Failed to alloc the obj, check to see if we should share
2343 * an fb with another CRTC instead
2345 for_each_crtc(dev, c) {
2346 i = to_intel_crtc(c);
2348 if (c == &intel_crtc->base)
2351 if (!i->active || !c->primary->fb)
2354 fb = to_intel_framebuffer(c->primary->fb);
2355 if (i915_gem_obj_ggtt_offset(fb->obj) == plane_config->base) {
2356 drm_framebuffer_reference(c->primary->fb);
2357 intel_crtc->base.primary->fb = c->primary->fb;
2363 static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2364 struct drm_framebuffer *fb,
2367 struct drm_device *dev = crtc->dev;
2368 struct drm_i915_private *dev_priv = dev->dev_private;
2369 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2370 struct intel_framebuffer *intel_fb;
2371 struct drm_i915_gem_object *obj;
2372 int plane = intel_crtc->plane;
2373 unsigned long linear_offset;
2377 intel_fb = to_intel_framebuffer(fb);
2378 obj = intel_fb->obj;
2380 reg = DSPCNTR(plane);
2381 dspcntr = I915_READ(reg);
2382 /* Mask out pixel format bits in case we change it */
2383 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
2384 switch (fb->pixel_format) {
2386 dspcntr |= DISPPLANE_8BPP;
2388 case DRM_FORMAT_XRGB1555:
2389 case DRM_FORMAT_ARGB1555:
2390 dspcntr |= DISPPLANE_BGRX555;
2392 case DRM_FORMAT_RGB565:
2393 dspcntr |= DISPPLANE_BGRX565;
2395 case DRM_FORMAT_XRGB8888:
2396 case DRM_FORMAT_ARGB8888:
2397 dspcntr |= DISPPLANE_BGRX888;
2399 case DRM_FORMAT_XBGR8888:
2400 case DRM_FORMAT_ABGR8888:
2401 dspcntr |= DISPPLANE_RGBX888;
2403 case DRM_FORMAT_XRGB2101010:
2404 case DRM_FORMAT_ARGB2101010:
2405 dspcntr |= DISPPLANE_BGRX101010;
2407 case DRM_FORMAT_XBGR2101010:
2408 case DRM_FORMAT_ABGR2101010:
2409 dspcntr |= DISPPLANE_RGBX101010;
2415 if (INTEL_INFO(dev)->gen >= 4) {
2416 if (obj->tiling_mode != I915_TILING_NONE)
2417 dspcntr |= DISPPLANE_TILED;
2419 dspcntr &= ~DISPPLANE_TILED;
2423 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2425 I915_WRITE(reg, dspcntr);
2427 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
2429 if (INTEL_INFO(dev)->gen >= 4) {
2430 intel_crtc->dspaddr_offset =
2431 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2432 fb->bits_per_pixel / 8,
2434 linear_offset -= intel_crtc->dspaddr_offset;
2436 intel_crtc->dspaddr_offset = linear_offset;
2439 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2440 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2442 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
2443 if (INTEL_INFO(dev)->gen >= 4) {
2444 I915_WRITE(DSPSURF(plane),
2445 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
2446 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2447 I915_WRITE(DSPLINOFF(plane), linear_offset);
2449 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
2453 static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2454 struct drm_framebuffer *fb,
2457 struct drm_device *dev = crtc->dev;
2458 struct drm_i915_private *dev_priv = dev->dev_private;
2459 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2460 struct intel_framebuffer *intel_fb;
2461 struct drm_i915_gem_object *obj;
2462 int plane = intel_crtc->plane;
2463 unsigned long linear_offset;
2467 intel_fb = to_intel_framebuffer(fb);
2468 obj = intel_fb->obj;
2470 reg = DSPCNTR(plane);
2471 dspcntr = I915_READ(reg);
2472 /* Mask out pixel format bits in case we change it */
2473 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
2474 switch (fb->pixel_format) {
2476 dspcntr |= DISPPLANE_8BPP;
2478 case DRM_FORMAT_RGB565:
2479 dspcntr |= DISPPLANE_BGRX565;
2481 case DRM_FORMAT_XRGB8888:
2482 case DRM_FORMAT_ARGB8888:
2483 dspcntr |= DISPPLANE_BGRX888;
2485 case DRM_FORMAT_XBGR8888:
2486 case DRM_FORMAT_ABGR8888:
2487 dspcntr |= DISPPLANE_RGBX888;
2489 case DRM_FORMAT_XRGB2101010:
2490 case DRM_FORMAT_ARGB2101010:
2491 dspcntr |= DISPPLANE_BGRX101010;
2493 case DRM_FORMAT_XBGR2101010:
2494 case DRM_FORMAT_ABGR2101010:
2495 dspcntr |= DISPPLANE_RGBX101010;
2501 if (obj->tiling_mode != I915_TILING_NONE)
2502 dspcntr |= DISPPLANE_TILED;
2504 dspcntr &= ~DISPPLANE_TILED;
2506 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2507 dspcntr &= ~DISPPLANE_TRICKLE_FEED_DISABLE;
2509 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2511 I915_WRITE(reg, dspcntr);
2513 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
2514 intel_crtc->dspaddr_offset =
2515 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2516 fb->bits_per_pixel / 8,
2518 linear_offset -= intel_crtc->dspaddr_offset;
2520 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2521 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2523 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
2524 I915_WRITE(DSPSURF(plane),
2525 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
2526 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
2527 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2529 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2530 I915_WRITE(DSPLINOFF(plane), linear_offset);
2535 /* Assume fb object is pinned & idle & fenced and just update base pointers */
2537 intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2538 int x, int y, enum mode_set_atomic state)
2540 struct drm_device *dev = crtc->dev;
2541 struct drm_i915_private *dev_priv = dev->dev_private;
2543 if (dev_priv->display.disable_fbc)
2544 dev_priv->display.disable_fbc(dev);
2545 intel_increase_pllclock(crtc);
2547 dev_priv->display.update_primary_plane(crtc, fb, x, y);
2552 void intel_display_handle_reset(struct drm_device *dev)
2554 struct drm_i915_private *dev_priv = dev->dev_private;
2555 struct drm_crtc *crtc;
2558 * Flips in the rings have been nuked by the reset,
2559 * so complete all pending flips so that user space
2560 * will get its events and not get stuck.
2562 * Also update the base address of all primary
2563 * planes to the the last fb to make sure we're
2564 * showing the correct fb after a reset.
2566 * Need to make two loops over the crtcs so that we
2567 * don't try to grab a crtc mutex before the
2568 * pending_flip_queue really got woken up.
2571 for_each_crtc(dev, crtc) {
2572 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2573 enum plane plane = intel_crtc->plane;
2575 intel_prepare_page_flip(dev, plane);
2576 intel_finish_page_flip_plane(dev, plane);
2579 for_each_crtc(dev, crtc) {
2580 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2582 drm_modeset_lock(&crtc->mutex, NULL);
2584 * FIXME: Once we have proper support for primary planes (and
2585 * disabling them without disabling the entire crtc) allow again
2586 * a NULL crtc->primary->fb.
2588 if (intel_crtc->active && crtc->primary->fb)
2589 dev_priv->display.update_primary_plane(crtc,
2593 drm_modeset_unlock(&crtc->mutex);
2598 intel_finish_fb(struct drm_framebuffer *old_fb)
2600 struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
2601 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2602 bool was_interruptible = dev_priv->mm.interruptible;
2605 /* Big Hammer, we also need to ensure that any pending
2606 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2607 * current scanout is retired before unpinning the old
2610 * This should only fail upon a hung GPU, in which case we
2611 * can safely continue.
2613 dev_priv->mm.interruptible = false;
2614 ret = i915_gem_object_finish_gpu(obj);
2615 dev_priv->mm.interruptible = was_interruptible;
2620 static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
2622 struct drm_device *dev = crtc->dev;
2623 struct drm_i915_private *dev_priv = dev->dev_private;
2624 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2627 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
2628 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
2631 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
2632 pending = to_intel_crtc(crtc)->unpin_work != NULL;
2633 lockmgr(&dev->event_lock, LK_RELEASE);
2639 intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
2640 struct drm_framebuffer *fb)
2642 struct drm_device *dev = crtc->dev;
2643 struct drm_i915_private *dev_priv = dev->dev_private;
2644 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2645 struct drm_framebuffer *old_fb;
2648 if (intel_crtc_has_pending_flip(crtc)) {
2649 DRM_ERROR("pipe is still busy with an old pageflip\n");
2655 DRM_ERROR("No FB bound\n");
2659 if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
2660 DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
2661 plane_name(intel_crtc->plane),
2662 INTEL_INFO(dev)->num_pipes);
2666 mutex_lock(&dev->struct_mutex);
2667 ret = intel_pin_and_fence_fb_obj(dev,
2668 to_intel_framebuffer(fb)->obj,
2670 mutex_unlock(&dev->struct_mutex);
2672 DRM_ERROR("pin & fence failed\n");
2677 * Update pipe size and adjust fitter if needed: the reason for this is
2678 * that in compute_mode_changes we check the native mode (not the pfit
2679 * mode) to see if we can flip rather than do a full mode set. In the
2680 * fastboot case, we'll flip, but if we don't update the pipesrc and
2681 * pfit state, we'll end up with a big fb scanned out into the wrong
2684 * To fix this properly, we need to hoist the checks up into
2685 * compute_mode_changes (or above), check the actual pfit state and
2686 * whether the platform allows pfit disable with pipe active, and only
2687 * then update the pipesrc and pfit state, even on the flip path.
2689 if (i915.fastboot) {
2690 const struct drm_display_mode *adjusted_mode =
2691 &intel_crtc->config.adjusted_mode;
2693 I915_WRITE(PIPESRC(intel_crtc->pipe),
2694 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
2695 (adjusted_mode->crtc_vdisplay - 1));
2696 if (!intel_crtc->config.pch_pfit.enabled &&
2697 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
2698 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
2699 I915_WRITE(PF_CTL(intel_crtc->pipe), 0);
2700 I915_WRITE(PF_WIN_POS(intel_crtc->pipe), 0);
2701 I915_WRITE(PF_WIN_SZ(intel_crtc->pipe), 0);
2703 intel_crtc->config.pipe_src_w = adjusted_mode->crtc_hdisplay;
2704 intel_crtc->config.pipe_src_h = adjusted_mode->crtc_vdisplay;
2707 dev_priv->display.update_primary_plane(crtc, fb, x, y);
2709 old_fb = crtc->primary->fb;
2710 crtc->primary->fb = fb;
2715 if (intel_crtc->active && old_fb != fb)
2716 intel_wait_for_vblank(dev, intel_crtc->pipe);
2717 mutex_lock(&dev->struct_mutex);
2718 intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
2719 mutex_unlock(&dev->struct_mutex);
2722 mutex_lock(&dev->struct_mutex);
2723 intel_update_fbc(dev);
2724 intel_edp_psr_update(dev);
2725 mutex_unlock(&dev->struct_mutex);
2730 static void intel_fdi_normal_train(struct drm_crtc *crtc)
2732 struct drm_device *dev = crtc->dev;
2733 struct drm_i915_private *dev_priv = dev->dev_private;
2734 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2735 int pipe = intel_crtc->pipe;
2738 /* enable normal train */
2739 reg = FDI_TX_CTL(pipe);
2740 temp = I915_READ(reg);
2741 if (IS_IVYBRIDGE(dev)) {
2742 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2743 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
2745 temp &= ~FDI_LINK_TRAIN_NONE;
2746 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
2748 I915_WRITE(reg, temp);
2750 reg = FDI_RX_CTL(pipe);
2751 temp = I915_READ(reg);
2752 if (HAS_PCH_CPT(dev)) {
2753 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2754 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2756 temp &= ~FDI_LINK_TRAIN_NONE;
2757 temp |= FDI_LINK_TRAIN_NONE;
2759 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2761 /* wait one idle pattern time */
2765 /* IVB wants error correction enabled */
2766 if (IS_IVYBRIDGE(dev))
2767 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2768 FDI_FE_ERRC_ENABLE);
2771 static bool pipe_has_enabled_pch(struct intel_crtc *crtc)
2773 return crtc->base.enabled && crtc->active &&
2774 crtc->config.has_pch_encoder;
2777 static void ivb_modeset_global_resources(struct drm_device *dev)
2779 struct drm_i915_private *dev_priv = dev->dev_private;
2780 struct intel_crtc *pipe_B_crtc =
2781 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
2782 struct intel_crtc *pipe_C_crtc =
2783 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
2787 * When everything is off disable fdi C so that we could enable fdi B
2788 * with all lanes. Note that we don't care about enabled pipes without
2789 * an enabled pch encoder.
2791 if (!pipe_has_enabled_pch(pipe_B_crtc) &&
2792 !pipe_has_enabled_pch(pipe_C_crtc)) {
2793 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
2794 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
2796 temp = I915_READ(SOUTH_CHICKEN1);
2797 temp &= ~FDI_BC_BIFURCATION_SELECT;
2798 DRM_DEBUG_KMS("disabling fdi C rx\n");
2799 I915_WRITE(SOUTH_CHICKEN1, temp);
2803 /* The FDI link training functions for ILK/Ibexpeak. */
2804 static void ironlake_fdi_link_train(struct drm_crtc *crtc)
2806 struct drm_device *dev = crtc->dev;
2807 struct drm_i915_private *dev_priv = dev->dev_private;
2808 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2809 int pipe = intel_crtc->pipe;
2810 u32 reg, temp, tries;
2812 /* FDI needs bits from pipe first */
2813 assert_pipe_enabled(dev_priv, pipe);
2815 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2817 reg = FDI_RX_IMR(pipe);
2818 temp = I915_READ(reg);
2819 temp &= ~FDI_RX_SYMBOL_LOCK;
2820 temp &= ~FDI_RX_BIT_LOCK;
2821 I915_WRITE(reg, temp);
2825 /* enable CPU FDI TX and PCH FDI RX */
2826 reg = FDI_TX_CTL(pipe);
2827 temp = I915_READ(reg);
2828 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2829 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
2830 temp &= ~FDI_LINK_TRAIN_NONE;
2831 temp |= FDI_LINK_TRAIN_PATTERN_1;
2832 I915_WRITE(reg, temp | FDI_TX_ENABLE);
2834 reg = FDI_RX_CTL(pipe);
2835 temp = I915_READ(reg);
2836 temp &= ~FDI_LINK_TRAIN_NONE;
2837 temp |= FDI_LINK_TRAIN_PATTERN_1;
2838 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2843 /* Ironlake workaround, enable clock pointer after FDI enable*/
2844 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2845 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
2846 FDI_RX_PHASE_SYNC_POINTER_EN);
2848 reg = FDI_RX_IIR(pipe);
2849 for (tries = 0; tries < 5; tries++) {
2850 temp = I915_READ(reg);
2851 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2853 if ((temp & FDI_RX_BIT_LOCK)) {
2854 DRM_DEBUG_KMS("FDI train 1 done.\n");
2855 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2860 DRM_ERROR("FDI train 1 fail!\n");
2863 reg = FDI_TX_CTL(pipe);
2864 temp = I915_READ(reg);
2865 temp &= ~FDI_LINK_TRAIN_NONE;
2866 temp |= FDI_LINK_TRAIN_PATTERN_2;
2867 I915_WRITE(reg, temp);
2869 reg = FDI_RX_CTL(pipe);
2870 temp = I915_READ(reg);
2871 temp &= ~FDI_LINK_TRAIN_NONE;
2872 temp |= FDI_LINK_TRAIN_PATTERN_2;
2873 I915_WRITE(reg, temp);
2878 reg = FDI_RX_IIR(pipe);
2879 for (tries = 0; tries < 5; tries++) {
2880 temp = I915_READ(reg);
2881 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2883 if (temp & FDI_RX_SYMBOL_LOCK) {
2884 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2885 DRM_DEBUG_KMS("FDI train 2 done.\n");
2890 DRM_ERROR("FDI train 2 fail!\n");
2892 DRM_DEBUG_KMS("FDI train done\n");
2896 static const int snb_b_fdi_train_param[] = {
2897 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
2898 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
2899 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
2900 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
2903 /* The FDI link training functions for SNB/Cougarpoint. */
2904 static void gen6_fdi_link_train(struct drm_crtc *crtc)
2906 struct drm_device *dev = crtc->dev;
2907 struct drm_i915_private *dev_priv = dev->dev_private;
2908 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2909 int pipe = intel_crtc->pipe;
2910 u32 reg, temp, i, retry;
2912 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2914 reg = FDI_RX_IMR(pipe);
2915 temp = I915_READ(reg);
2916 temp &= ~FDI_RX_SYMBOL_LOCK;
2917 temp &= ~FDI_RX_BIT_LOCK;
2918 I915_WRITE(reg, temp);
2923 /* enable CPU FDI TX and PCH FDI RX */
2924 reg = FDI_TX_CTL(pipe);
2925 temp = I915_READ(reg);
2926 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2927 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
2928 temp &= ~FDI_LINK_TRAIN_NONE;
2929 temp |= FDI_LINK_TRAIN_PATTERN_1;
2930 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2932 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2933 I915_WRITE(reg, temp | FDI_TX_ENABLE);
2935 I915_WRITE(FDI_RX_MISC(pipe),
2936 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2938 reg = FDI_RX_CTL(pipe);
2939 temp = I915_READ(reg);
2940 if (HAS_PCH_CPT(dev)) {
2941 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2942 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2944 temp &= ~FDI_LINK_TRAIN_NONE;
2945 temp |= FDI_LINK_TRAIN_PATTERN_1;
2947 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2952 for (i = 0; i < 4; i++) {
2953 reg = FDI_TX_CTL(pipe);
2954 temp = I915_READ(reg);
2955 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2956 temp |= snb_b_fdi_train_param[i];
2957 I915_WRITE(reg, temp);
2962 for (retry = 0; retry < 5; retry++) {
2963 reg = FDI_RX_IIR(pipe);
2964 temp = I915_READ(reg);
2965 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2966 if (temp & FDI_RX_BIT_LOCK) {
2967 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2968 DRM_DEBUG_KMS("FDI train 1 done.\n");
2977 DRM_ERROR("FDI train 1 fail!\n");
2980 reg = FDI_TX_CTL(pipe);
2981 temp = I915_READ(reg);
2982 temp &= ~FDI_LINK_TRAIN_NONE;
2983 temp |= FDI_LINK_TRAIN_PATTERN_2;
2985 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2987 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2989 I915_WRITE(reg, temp);
2991 reg = FDI_RX_CTL(pipe);
2992 temp = I915_READ(reg);
2993 if (HAS_PCH_CPT(dev)) {
2994 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2995 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2997 temp &= ~FDI_LINK_TRAIN_NONE;
2998 temp |= FDI_LINK_TRAIN_PATTERN_2;
3000 I915_WRITE(reg, temp);
3005 for (i = 0; i < 4; i++) {
3006 reg = FDI_TX_CTL(pipe);
3007 temp = I915_READ(reg);
3008 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3009 temp |= snb_b_fdi_train_param[i];
3010 I915_WRITE(reg, temp);
3015 for (retry = 0; retry < 5; retry++) {
3016 reg = FDI_RX_IIR(pipe);
3017 temp = I915_READ(reg);
3018 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3019 if (temp & FDI_RX_SYMBOL_LOCK) {
3020 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3021 DRM_DEBUG_KMS("FDI train 2 done.\n");
3030 DRM_ERROR("FDI train 2 fail!\n");
3032 DRM_DEBUG_KMS("FDI train done.\n");
3035 /* Manual link training for Ivy Bridge A0 parts */
3036 static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3038 struct drm_device *dev = crtc->dev;
3039 struct drm_i915_private *dev_priv = dev->dev_private;
3040 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3041 int pipe = intel_crtc->pipe;
3042 u32 reg, temp, i, j;
3044 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3046 reg = FDI_RX_IMR(pipe);
3047 temp = I915_READ(reg);
3048 temp &= ~FDI_RX_SYMBOL_LOCK;
3049 temp &= ~FDI_RX_BIT_LOCK;
3050 I915_WRITE(reg, temp);
3055 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3056 I915_READ(FDI_RX_IIR(pipe)));
3058 /* Try each vswing and preemphasis setting twice before moving on */
3059 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3060 /* disable first in case we need to retry */
3061 reg = FDI_TX_CTL(pipe);
3062 temp = I915_READ(reg);
3063 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3064 temp &= ~FDI_TX_ENABLE;
3065 I915_WRITE(reg, temp);
3067 reg = FDI_RX_CTL(pipe);
3068 temp = I915_READ(reg);
3069 temp &= ~FDI_LINK_TRAIN_AUTO;
3070 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3071 temp &= ~FDI_RX_ENABLE;
3072 I915_WRITE(reg, temp);
3074 /* enable CPU FDI TX and PCH FDI RX */
3075 reg = FDI_TX_CTL(pipe);
3076 temp = I915_READ(reg);
3077 temp &= ~FDI_DP_PORT_WIDTH_MASK;
3078 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
3079 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
3080 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3081 temp |= snb_b_fdi_train_param[j/2];
3082 temp |= FDI_COMPOSITE_SYNC;
3083 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3085 I915_WRITE(FDI_RX_MISC(pipe),
3086 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3088 reg = FDI_RX_CTL(pipe);
3089 temp = I915_READ(reg);
3090 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3091 temp |= FDI_COMPOSITE_SYNC;
3092 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3095 udelay(1); /* should be 0.5us */
3097 for (i = 0; i < 4; i++) {
3098 reg = FDI_RX_IIR(pipe);
3099 temp = I915_READ(reg);
3100 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3102 if (temp & FDI_RX_BIT_LOCK ||
3103 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3104 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3105 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3109 udelay(1); /* should be 0.5us */
3112 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3117 reg = FDI_TX_CTL(pipe);
3118 temp = I915_READ(reg);
3119 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3120 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3121 I915_WRITE(reg, temp);
3123 reg = FDI_RX_CTL(pipe);
3124 temp = I915_READ(reg);
3125 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3126 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3127 I915_WRITE(reg, temp);
3130 udelay(2); /* should be 1.5us */
3132 for (i = 0; i < 4; i++) {
3133 reg = FDI_RX_IIR(pipe);
3134 temp = I915_READ(reg);
3135 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3137 if (temp & FDI_RX_SYMBOL_LOCK ||
3138 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3139 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3140 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3144 udelay(2); /* should be 1.5us */
3147 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
3151 DRM_DEBUG_KMS("FDI train done.\n");
3154 static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
3156 struct drm_device *dev = intel_crtc->base.dev;
3157 struct drm_i915_private *dev_priv = dev->dev_private;
3158 int pipe = intel_crtc->pipe;
3162 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
3163 reg = FDI_RX_CTL(pipe);
3164 temp = I915_READ(reg);
3165 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
3166 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
3167 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
3168 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3173 /* Switch from Rawclk to PCDclk */
3174 temp = I915_READ(reg);
3175 I915_WRITE(reg, temp | FDI_PCDCLK);
3180 /* Enable CPU FDI TX PLL, always on for Ironlake */
3181 reg = FDI_TX_CTL(pipe);
3182 temp = I915_READ(reg);
3183 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3184 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
3191 static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3193 struct drm_device *dev = intel_crtc->base.dev;
3194 struct drm_i915_private *dev_priv = dev->dev_private;
3195 int pipe = intel_crtc->pipe;
3198 /* Switch from PCDclk to Rawclk */
3199 reg = FDI_RX_CTL(pipe);
3200 temp = I915_READ(reg);
3201 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3203 /* Disable CPU FDI TX PLL */
3204 reg = FDI_TX_CTL(pipe);
3205 temp = I915_READ(reg);
3206 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3211 reg = FDI_RX_CTL(pipe);
3212 temp = I915_READ(reg);
3213 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3215 /* Wait for the clocks to turn off. */
3220 static void ironlake_fdi_disable(struct drm_crtc *crtc)
3222 struct drm_device *dev = crtc->dev;
3223 struct drm_i915_private *dev_priv = dev->dev_private;
3224 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3225 int pipe = intel_crtc->pipe;
3228 /* disable CPU FDI tx and PCH FDI rx */
3229 reg = FDI_TX_CTL(pipe);
3230 temp = I915_READ(reg);
3231 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3234 reg = FDI_RX_CTL(pipe);
3235 temp = I915_READ(reg);
3236 temp &= ~(0x7 << 16);
3237 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
3238 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3243 /* Ironlake workaround, disable clock pointer after downing FDI */
3244 if (HAS_PCH_IBX(dev))
3245 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3247 /* still set train pattern 1 */
3248 reg = FDI_TX_CTL(pipe);
3249 temp = I915_READ(reg);
3250 temp &= ~FDI_LINK_TRAIN_NONE;
3251 temp |= FDI_LINK_TRAIN_PATTERN_1;
3252 I915_WRITE(reg, temp);
3254 reg = FDI_RX_CTL(pipe);
3255 temp = I915_READ(reg);
3256 if (HAS_PCH_CPT(dev)) {
3257 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3258 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3260 temp &= ~FDI_LINK_TRAIN_NONE;
3261 temp |= FDI_LINK_TRAIN_PATTERN_1;
3263 /* BPC in FDI rx is consistent with that in PIPECONF */
3264 temp &= ~(0x07 << 16);
3265 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
3266 I915_WRITE(reg, temp);
3272 bool intel_has_pending_fb_unpin(struct drm_device *dev)
3274 struct intel_crtc *crtc;
3276 /* Note that we don't need to be called with mode_config.lock here
3277 * as our list of CRTC objects is static for the lifetime of the
3278 * device and so cannot disappear as we iterate. Similarly, we can
3279 * happily treat the predicates as racy, atomic checks as userspace
3280 * cannot claim and pin a new fb without at least acquring the
3281 * struct_mutex and so serialising with us.
3283 for_each_intel_crtc(dev, crtc) {
3284 if (atomic_read(&crtc->unpin_work_count) == 0)
3287 if (crtc->unpin_work)
3288 intel_wait_for_vblank(dev, crtc->pipe);
3296 void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
3298 struct drm_device *dev = crtc->dev;
3299 struct drm_i915_private *dev_priv = dev->dev_private;
3301 if (crtc->primary->fb == NULL)
3304 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
3306 WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3307 !intel_crtc_has_pending_flip(crtc),
3310 mutex_lock(&dev->struct_mutex);
3311 intel_finish_fb(crtc->primary->fb);
3312 mutex_unlock(&dev->struct_mutex);
3315 /* Program iCLKIP clock to the desired frequency */
3316 static void lpt_program_iclkip(struct drm_crtc *crtc)
3318 struct drm_device *dev = crtc->dev;
3319 struct drm_i915_private *dev_priv = dev->dev_private;
3320 int clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
3321 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3324 mutex_lock(&dev_priv->dpio_lock);
3326 /* It is necessary to ungate the pixclk gate prior to programming
3327 * the divisors, and gate it back when it is done.
3329 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3331 /* Disable SSCCTL */
3332 intel_sbi_write(dev_priv, SBI_SSCCTL6,
3333 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3337 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
3338 if (clock == 20000) {
3343 /* The iCLK virtual clock root frequency is in MHz,
3344 * but the adjusted_mode->crtc_clock in in KHz. To get the
3345 * divisors, it is necessary to divide one by another, so we
3346 * convert the virtual clock precision to KHz here for higher
3349 u32 iclk_virtual_root_freq = 172800 * 1000;
3350 u32 iclk_pi_range = 64;
3351 u32 desired_divisor, msb_divisor_value, pi_value;
3353 desired_divisor = (iclk_virtual_root_freq / clock);
3354 msb_divisor_value = desired_divisor / iclk_pi_range;
3355 pi_value = desired_divisor % iclk_pi_range;
3358 divsel = msb_divisor_value - 2;
3359 phaseinc = pi_value;
3362 /* This should not happen with any sane values */
3363 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3364 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3365 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3366 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3368 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
3375 /* Program SSCDIVINTPHASE6 */
3376 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
3377 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3378 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3379 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3380 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3381 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3382 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
3383 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
3385 /* Program SSCAUXDIV */
3386 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
3387 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3388 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
3389 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
3391 /* Enable modulator and associated divider */
3392 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3393 temp &= ~SBI_SSCCTL_DISABLE;
3394 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
3396 /* Wait for initialization time */
3399 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
3401 mutex_unlock(&dev_priv->dpio_lock);
3404 static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3405 enum i915_pipe pch_transcoder)
3407 struct drm_device *dev = crtc->base.dev;
3408 struct drm_i915_private *dev_priv = dev->dev_private;
3409 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
3411 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3412 I915_READ(HTOTAL(cpu_transcoder)));
3413 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3414 I915_READ(HBLANK(cpu_transcoder)));
3415 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3416 I915_READ(HSYNC(cpu_transcoder)));
3418 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3419 I915_READ(VTOTAL(cpu_transcoder)));
3420 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
3421 I915_READ(VBLANK(cpu_transcoder)));
3422 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
3423 I915_READ(VSYNC(cpu_transcoder)));
3424 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
3425 I915_READ(VSYNCSHIFT(cpu_transcoder)));
3428 static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
3430 struct drm_i915_private *dev_priv = dev->dev_private;
3433 temp = I915_READ(SOUTH_CHICKEN1);
3434 if (temp & FDI_BC_BIFURCATION_SELECT)
3437 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
3438 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
3440 temp |= FDI_BC_BIFURCATION_SELECT;
3441 DRM_DEBUG_KMS("enabling fdi C rx\n");
3442 I915_WRITE(SOUTH_CHICKEN1, temp);
3443 POSTING_READ(SOUTH_CHICKEN1);
3446 static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
3448 struct drm_device *dev = intel_crtc->base.dev;
3449 struct drm_i915_private *dev_priv = dev->dev_private;
3451 switch (intel_crtc->pipe) {
3455 if (intel_crtc->config.fdi_lanes > 2)
3456 WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
3458 cpt_enable_fdi_bc_bifurcation(dev);
3462 cpt_enable_fdi_bc_bifurcation(dev);
3471 * Enable PCH resources required for PCH ports:
3473 * - FDI training & RX/TX
3474 * - update transcoder timings
3475 * - DP transcoding bits
3478 static void ironlake_pch_enable(struct drm_crtc *crtc)
3480 struct drm_device *dev = crtc->dev;
3481 struct drm_i915_private *dev_priv = dev->dev_private;
3482 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3483 int pipe = intel_crtc->pipe;
3486 assert_pch_transcoder_disabled(dev_priv, pipe);
3488 if (IS_IVYBRIDGE(dev))
3489 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
3491 /* Write the TU size bits before fdi link training, so that error
3492 * detection works. */
3493 I915_WRITE(FDI_RX_TUSIZE1(pipe),
3494 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
3496 /* For PCH output, training FDI link */
3497 dev_priv->display.fdi_link_train(crtc);
3499 /* We need to program the right clock selection before writing the pixel
3500 * mutliplier into the DPLL. */
3501 if (HAS_PCH_CPT(dev)) {
3504 temp = I915_READ(PCH_DPLL_SEL);
3505 temp |= TRANS_DPLL_ENABLE(pipe);
3506 sel = TRANS_DPLLB_SEL(pipe);
3507 if (intel_crtc->config.shared_dpll == DPLL_ID_PCH_PLL_B)
3511 I915_WRITE(PCH_DPLL_SEL, temp);
3514 /* XXX: pch pll's can be enabled any time before we enable the PCH
3515 * transcoder, and we actually should do this to not upset any PCH
3516 * transcoder that already use the clock when we share it.
3518 * Note that enable_shared_dpll tries to do the right thing, but
3519 * get_shared_dpll unconditionally resets the pll - we need that to have
3520 * the right LVDS enable sequence. */
3521 intel_enable_shared_dpll(intel_crtc);
3523 /* set transcoder timing, panel must allow it */
3524 assert_panel_unlocked(dev_priv, pipe);
3525 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
3527 intel_fdi_normal_train(crtc);
3529 /* For PCH DP, enable TRANS_DP_CTL */
3530 if (HAS_PCH_CPT(dev) &&
3531 (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
3532 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
3533 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
3534 reg = TRANS_DP_CTL(pipe);
3535 temp = I915_READ(reg);
3536 temp &= ~(TRANS_DP_PORT_SEL_MASK |
3537 TRANS_DP_SYNC_MASK |
3539 temp |= (TRANS_DP_OUTPUT_ENABLE |
3540 TRANS_DP_ENH_FRAMING);
3541 temp |= bpc << 9; /* same format but at 11:9 */
3543 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
3544 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
3545 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
3546 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
3548 switch (intel_trans_dp_port_sel(crtc)) {
3550 temp |= TRANS_DP_PORT_SEL_B;
3553 temp |= TRANS_DP_PORT_SEL_C;
3556 temp |= TRANS_DP_PORT_SEL_D;
3562 I915_WRITE(reg, temp);
3565 ironlake_enable_pch_transcoder(dev_priv, pipe);
3568 static void lpt_pch_enable(struct drm_crtc *crtc)
3570 struct drm_device *dev = crtc->dev;
3571 struct drm_i915_private *dev_priv = dev->dev_private;
3572 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3573 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
3575 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
3577 lpt_program_iclkip(crtc);
3579 /* Set transcoder timing. */
3580 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
3582 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
3585 static void intel_put_shared_dpll(struct intel_crtc *crtc)
3587 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
3592 if (pll->refcount == 0) {
3593 WARN(1, "bad %s refcount\n", pll->name);
3597 if (--pll->refcount == 0) {
3599 WARN_ON(pll->active);
3602 crtc->config.shared_dpll = DPLL_ID_PRIVATE;
3605 static struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc)
3607 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3608 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
3609 enum intel_dpll_id i;
3612 DRM_DEBUG_KMS("CRTC:%d dropping existing %s\n",
3613 crtc->base.base.id, pll->name);
3614 intel_put_shared_dpll(crtc);
3617 if (HAS_PCH_IBX(dev_priv->dev)) {
3618 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
3619 i = (enum intel_dpll_id) crtc->pipe;
3620 pll = &dev_priv->shared_dplls[i];
3622 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
3623 crtc->base.base.id, pll->name);
3625 WARN_ON(pll->refcount);
3630 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3631 pll = &dev_priv->shared_dplls[i];
3633 /* Only want to check enabled timings first */
3634 if (pll->refcount == 0)
3637 if (memcmp(&crtc->config.dpll_hw_state, &pll->hw_state,
3638 sizeof(pll->hw_state)) == 0) {
3639 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (refcount %d, ative %d)\n",
3641 pll->name, pll->refcount, pll->active);
3647 /* Ok no matching timings, maybe there's a free one? */
3648 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3649 pll = &dev_priv->shared_dplls[i];
3650 if (pll->refcount == 0) {
3651 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
3652 crtc->base.base.id, pll->name);
3660 if (pll->refcount == 0)
3661 pll->hw_state = crtc->config.dpll_hw_state;
3663 crtc->config.shared_dpll = i;
3664 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
3665 pipe_name(crtc->pipe));
3672 static void cpt_verify_modeset(struct drm_device *dev, int pipe)
3674 struct drm_i915_private *dev_priv = dev->dev_private;
3675 int dslreg = PIPEDSL(pipe);
3678 temp = I915_READ(dslreg);
3680 if (wait_for(I915_READ(dslreg) != temp, 5)) {
3681 if (wait_for(I915_READ(dslreg) != temp, 5))
3682 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
3686 static void ironlake_pfit_enable(struct intel_crtc *crtc)
3688 struct drm_device *dev = crtc->base.dev;
3689 struct drm_i915_private *dev_priv = dev->dev_private;
3690 int pipe = crtc->pipe;
3692 if (crtc->config.pch_pfit.enabled) {
3693 /* Force use of hard-coded filter coefficients
3694 * as some pre-programmed values are broken,
3697 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
3698 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3699 PF_PIPE_SEL_IVB(pipe));
3701 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
3702 I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
3703 I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
3707 static void intel_enable_planes(struct drm_crtc *crtc)
3709 struct drm_device *dev = crtc->dev;
3710 enum i915_pipe pipe = to_intel_crtc(crtc)->pipe;
3711 struct drm_plane *plane;
3712 struct intel_plane *intel_plane;
3714 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
3715 intel_plane = to_intel_plane(plane);
3716 if (intel_plane->pipe == pipe)
3717 intel_plane_restore(&intel_plane->base);
3721 static void intel_disable_planes(struct drm_crtc *crtc)
3723 struct drm_device *dev = crtc->dev;
3724 enum i915_pipe pipe = to_intel_crtc(crtc)->pipe;
3725 struct drm_plane *plane;
3726 struct intel_plane *intel_plane;
3728 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
3729 intel_plane = to_intel_plane(plane);
3730 if (intel_plane->pipe == pipe)
3731 intel_plane_disable(&intel_plane->base);
3735 void hsw_enable_ips(struct intel_crtc *crtc)
3737 struct drm_device *dev = crtc->base.dev;
3738 struct drm_i915_private *dev_priv = dev->dev_private;
3740 if (!crtc->config.ips_enabled)
3743 /* We can only enable IPS after we enable a plane and wait for a vblank */
3744 intel_wait_for_vblank(dev, crtc->pipe);
3746 assert_plane_enabled(dev_priv, crtc->plane);
3747 if (IS_BROADWELL(dev)) {
3748 mutex_lock(&dev_priv->rps.hw_lock);
3749 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
3750 mutex_unlock(&dev_priv->rps.hw_lock);
3751 /* Quoting Art Runyan: "its not safe to expect any particular
3752 * value in IPS_CTL bit 31 after enabling IPS through the
3753 * mailbox." Moreover, the mailbox may return a bogus state,
3754 * so we need to just enable it and continue on.
3757 I915_WRITE(IPS_CTL, IPS_ENABLE);
3758 /* The bit only becomes 1 in the next vblank, so this wait here
3759 * is essentially intel_wait_for_vblank. If we don't have this
3760 * and don't wait for vblanks until the end of crtc_enable, then
3761 * the HW state readout code will complain that the expected
3762 * IPS_CTL value is not the one we read. */
3763 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
3764 DRM_ERROR("Timed out waiting for IPS enable\n");
3768 void hsw_disable_ips(struct intel_crtc *crtc)
3770 struct drm_device *dev = crtc->base.dev;
3771 struct drm_i915_private *dev_priv = dev->dev_private;
3773 if (!crtc->config.ips_enabled)
3776 assert_plane_enabled(dev_priv, crtc->plane);
3777 if (IS_BROADWELL(dev)) {
3778 mutex_lock(&dev_priv->rps.hw_lock);
3779 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
3780 mutex_unlock(&dev_priv->rps.hw_lock);
3781 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
3782 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
3783 DRM_ERROR("Timed out waiting for IPS disable\n");
3785 I915_WRITE(IPS_CTL, 0);
3786 POSTING_READ(IPS_CTL);
3789 /* We need to wait for a vblank before we can disable the plane. */
3790 intel_wait_for_vblank(dev, crtc->pipe);
3793 /** Loads the palette/gamma unit for the CRTC with the prepared values */
3794 static void intel_crtc_load_lut(struct drm_crtc *crtc)
3796 struct drm_device *dev = crtc->dev;
3797 struct drm_i915_private *dev_priv = dev->dev_private;
3798 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3799 enum i915_pipe pipe = intel_crtc->pipe;
3800 int palreg = PALETTE(pipe);
3802 bool reenable_ips = false;
3804 /* The clocks have to be on to load the palette. */
3805 if (!crtc->enabled || !intel_crtc->active)
3808 if (!HAS_PCH_SPLIT(dev_priv->dev)) {
3809 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
3810 assert_dsi_pll_enabled(dev_priv);
3812 assert_pll_enabled(dev_priv, pipe);
3815 /* use legacy palette for Ironlake */
3816 if (HAS_PCH_SPLIT(dev))
3817 palreg = LGC_PALETTE(pipe);
3819 /* Workaround : Do not read or write the pipe palette/gamma data while
3820 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
3822 if (IS_HASWELL(dev) && intel_crtc->config.ips_enabled &&
3823 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
3824 GAMMA_MODE_MODE_SPLIT)) {
3825 hsw_disable_ips(intel_crtc);
3826 reenable_ips = true;
3829 for (i = 0; i < 256; i++) {
3830 I915_WRITE(palreg + 4 * i,
3831 (intel_crtc->lut_r[i] << 16) |
3832 (intel_crtc->lut_g[i] << 8) |
3833 intel_crtc->lut_b[i]);
3837 hsw_enable_ips(intel_crtc);
3840 static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
3842 if (!enable && intel_crtc->overlay) {
3843 struct drm_device *dev = intel_crtc->base.dev;
3844 struct drm_i915_private *dev_priv = dev->dev_private;
3846 mutex_lock(&dev->struct_mutex);
3847 dev_priv->mm.interruptible = false;
3848 (void) intel_overlay_switch_off(intel_crtc->overlay);
3849 dev_priv->mm.interruptible = true;
3850 mutex_unlock(&dev->struct_mutex);
3853 /* Let userspace switch the overlay on again. In most cases userspace
3854 * has to recompute where to put it anyway.
3859 * i9xx_fixup_plane - ugly workaround for G45 to fire up the hardware
3860 * cursor plane briefly if not already running after enabling the display
3862 * This workaround avoids occasional blank screens when self refresh is
3866 g4x_fixup_plane(struct drm_i915_private *dev_priv, enum i915_pipe pipe)
3868 u32 cntl = I915_READ(CURCNTR(pipe));
3870 if ((cntl & CURSOR_MODE) == 0) {
3871 u32 fw_bcl_self = I915_READ(FW_BLC_SELF);
3873 I915_WRITE(FW_BLC_SELF, fw_bcl_self & ~FW_BLC_SELF_EN);
3874 I915_WRITE(CURCNTR(pipe), CURSOR_MODE_64_ARGB_AX);
3875 intel_wait_for_vblank(dev_priv->dev, pipe);
3876 I915_WRITE(CURCNTR(pipe), cntl);
3877 I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
3878 I915_WRITE(FW_BLC_SELF, fw_bcl_self);
3882 static void intel_crtc_enable_planes(struct drm_crtc *crtc)
3884 struct drm_device *dev = crtc->dev;
3885 struct drm_i915_private *dev_priv = dev->dev_private;
3886 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3887 int pipe = intel_crtc->pipe;
3888 int plane = intel_crtc->plane;
3890 intel_enable_primary_hw_plane(dev_priv, plane, pipe);
3891 intel_enable_planes(crtc);
3892 /* The fixup needs to happen before cursor is enabled */
3894 g4x_fixup_plane(dev_priv, pipe);
3895 intel_crtc_update_cursor(crtc, true);
3896 intel_crtc_dpms_overlay(intel_crtc, true);
3898 hsw_enable_ips(intel_crtc);
3900 mutex_lock(&dev->struct_mutex);
3901 intel_update_fbc(dev);
3902 intel_edp_psr_update(dev);
3903 mutex_unlock(&dev->struct_mutex);
3906 static void intel_crtc_disable_planes(struct drm_crtc *crtc)
3908 struct drm_device *dev = crtc->dev;
3909 struct drm_i915_private *dev_priv = dev->dev_private;
3910 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3911 int pipe = intel_crtc->pipe;
3912 int plane = intel_crtc->plane;
3914 intel_crtc_wait_for_pending_flips(crtc);
3915 drm_crtc_vblank_off(crtc);
3917 if (dev_priv->fbc.plane == plane)
3918 intel_disable_fbc(dev);
3920 hsw_disable_ips(intel_crtc);
3922 intel_crtc_dpms_overlay(intel_crtc, false);
3923 intel_crtc_update_cursor(crtc, false);
3924 intel_disable_planes(crtc);
3925 intel_disable_primary_hw_plane(dev_priv, plane, pipe);
3928 static void ironlake_crtc_enable(struct drm_crtc *crtc)
3930 struct drm_device *dev = crtc->dev;
3931 struct drm_i915_private *dev_priv = dev->dev_private;
3932 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3933 struct intel_encoder *encoder;
3934 int pipe = intel_crtc->pipe;
3935 enum plane plane = intel_crtc->plane;
3937 WARN_ON(!crtc->enabled);
3939 if (intel_crtc->active)
3942 if (intel_crtc->config.has_pch_encoder)
3943 intel_prepare_shared_dpll(intel_crtc);
3945 if (intel_crtc->config.has_dp_encoder)
3946 intel_dp_set_m_n(intel_crtc);
3948 intel_set_pipe_timings(intel_crtc);
3950 if (intel_crtc->config.has_pch_encoder) {
3951 intel_cpu_transcoder_set_m_n(intel_crtc,
3952 &intel_crtc->config.fdi_m_n);
3955 ironlake_set_pipeconf(crtc);
3957 /* Set up the display plane register */
3958 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
3959 POSTING_READ(DSPCNTR(plane));
3961 dev_priv->display.update_primary_plane(crtc, crtc->primary->fb,
3964 intel_crtc->active = true;
3966 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
3967 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
3969 for_each_encoder_on_crtc(dev, crtc, encoder)
3970 if (encoder->pre_enable)
3971 encoder->pre_enable(encoder);
3973 if (intel_crtc->config.has_pch_encoder) {
3974 /* Note: FDI PLL enabling _must_ be done before we enable the
3975 * cpu pipes, hence this is separate from all the other fdi/pch
3977 ironlake_fdi_pll_enable(intel_crtc);
3979 assert_fdi_tx_disabled(dev_priv, pipe);
3980 assert_fdi_rx_disabled(dev_priv, pipe);
3983 ironlake_pfit_enable(intel_crtc);
3986 * On ILK+ LUT must be loaded before the pipe is running but with
3989 intel_crtc_load_lut(crtc);
3991 intel_update_watermarks(crtc);
3992 intel_enable_pipe(intel_crtc);
3994 if (intel_crtc->config.has_pch_encoder)
3995 ironlake_pch_enable(crtc);
3997 for_each_encoder_on_crtc(dev, crtc, encoder)
3998 encoder->enable(encoder);
4000 if (HAS_PCH_CPT(dev))
4001 cpt_verify_modeset(dev, intel_crtc->pipe);
4003 intel_crtc_enable_planes(crtc);
4005 drm_crtc_vblank_on(crtc);
4008 /* IPS only exists on ULT machines and is tied to pipe A. */
4009 static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4011 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
4015 * This implements the workaround described in the "notes" section of the mode
4016 * set sequence documentation. When going from no pipes or single pipe to
4017 * multiple pipes, and planes are enabled after the pipe, we need to wait at
4018 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
4020 static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
4022 struct drm_device *dev = crtc->base.dev;
4023 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
4025 /* We want to get the other_active_crtc only if there's only 1 other
4027 for_each_intel_crtc(dev, crtc_it) {
4028 if (!crtc_it->active || crtc_it == crtc)
4031 if (other_active_crtc)
4034 other_active_crtc = crtc_it;
4036 if (!other_active_crtc)
4039 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4040 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4043 static void haswell_crtc_enable(struct drm_crtc *crtc)
4045 struct drm_device *dev = crtc->dev;
4046 struct drm_i915_private *dev_priv = dev->dev_private;
4047 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4048 struct intel_encoder *encoder;
4049 int pipe = intel_crtc->pipe;
4050 enum plane plane = intel_crtc->plane;
4052 WARN_ON(!crtc->enabled);
4054 if (intel_crtc->active)
4057 if (intel_crtc->config.has_dp_encoder)
4058 intel_dp_set_m_n(intel_crtc);
4060 intel_set_pipe_timings(intel_crtc);
4062 if (intel_crtc->config.has_pch_encoder) {
4063 intel_cpu_transcoder_set_m_n(intel_crtc,
4064 &intel_crtc->config.fdi_m_n);
4067 haswell_set_pipeconf(crtc);
4069 intel_set_pipe_csc(crtc);
4071 /* Set up the display plane register */
4072 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE | DISPPLANE_PIPE_CSC_ENABLE);
4073 POSTING_READ(DSPCNTR(plane));
4075 dev_priv->display.update_primary_plane(crtc, crtc->primary->fb,
4078 intel_crtc->active = true;
4080 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4081 if (intel_crtc->config.has_pch_encoder)
4082 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
4084 if (intel_crtc->config.has_pch_encoder)
4085 dev_priv->display.fdi_link_train(crtc);
4087 for_each_encoder_on_crtc(dev, crtc, encoder)
4088 if (encoder->pre_enable)
4089 encoder->pre_enable(encoder);
4091 intel_ddi_enable_pipe_clock(intel_crtc);
4093 ironlake_pfit_enable(intel_crtc);
4096 * On ILK+ LUT must be loaded before the pipe is running but with
4099 intel_crtc_load_lut(crtc);
4101 intel_ddi_set_pipe_settings(crtc);
4102 intel_ddi_enable_transcoder_func(crtc);
4104 intel_update_watermarks(crtc);
4105 intel_enable_pipe(intel_crtc);
4107 if (intel_crtc->config.has_pch_encoder)
4108 lpt_pch_enable(crtc);
4110 for_each_encoder_on_crtc(dev, crtc, encoder) {
4111 encoder->enable(encoder);
4112 intel_opregion_notify_encoder(encoder, true);
4115 /* If we change the relative order between pipe/planes enabling, we need
4116 * to change the workaround. */
4117 haswell_mode_set_planes_workaround(intel_crtc);
4118 intel_crtc_enable_planes(crtc);
4120 drm_crtc_vblank_on(crtc);
4123 static void ironlake_pfit_disable(struct intel_crtc *crtc)
4125 struct drm_device *dev = crtc->base.dev;
4126 struct drm_i915_private *dev_priv = dev->dev_private;
4127 int pipe = crtc->pipe;
4129 /* To avoid upsetting the power well on haswell only disable the pfit if
4130 * it's in use. The hw state code will make sure we get this right. */
4131 if (crtc->config.pch_pfit.enabled) {
4132 I915_WRITE(PF_CTL(pipe), 0);
4133 I915_WRITE(PF_WIN_POS(pipe), 0);
4134 I915_WRITE(PF_WIN_SZ(pipe), 0);
4138 static void ironlake_crtc_disable(struct drm_crtc *crtc)
4140 struct drm_device *dev = crtc->dev;
4141 struct drm_i915_private *dev_priv = dev->dev_private;
4142 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4143 struct intel_encoder *encoder;
4144 int pipe = intel_crtc->pipe;
4147 if (!intel_crtc->active)
4150 intel_crtc_disable_planes(crtc);
4152 for_each_encoder_on_crtc(dev, crtc, encoder)
4153 encoder->disable(encoder);
4155 if (intel_crtc->config.has_pch_encoder)
4156 intel_set_pch_fifo_underrun_reporting(dev, pipe, false);
4158 intel_disable_pipe(dev_priv, pipe);
4160 ironlake_pfit_disable(intel_crtc);
4162 for_each_encoder_on_crtc(dev, crtc, encoder)
4163 if (encoder->post_disable)
4164 encoder->post_disable(encoder);
4166 if (intel_crtc->config.has_pch_encoder) {
4167 ironlake_fdi_disable(crtc);
4169 ironlake_disable_pch_transcoder(dev_priv, pipe);
4170 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
4172 if (HAS_PCH_CPT(dev)) {
4173 /* disable TRANS_DP_CTL */
4174 reg = TRANS_DP_CTL(pipe);
4175 temp = I915_READ(reg);
4176 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
4177 TRANS_DP_PORT_SEL_MASK);
4178 temp |= TRANS_DP_PORT_SEL_NONE;
4179 I915_WRITE(reg, temp);
4181 /* disable DPLL_SEL */
4182 temp = I915_READ(PCH_DPLL_SEL);
4183 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
4184 I915_WRITE(PCH_DPLL_SEL, temp);
4187 /* disable PCH DPLL */
4188 intel_disable_shared_dpll(intel_crtc);
4190 ironlake_fdi_pll_disable(intel_crtc);
4193 intel_crtc->active = false;
4194 intel_update_watermarks(crtc);
4196 mutex_lock(&dev->struct_mutex);
4197 intel_update_fbc(dev);
4198 intel_edp_psr_update(dev);
4199 mutex_unlock(&dev->struct_mutex);
4202 static void haswell_crtc_disable(struct drm_crtc *crtc)
4204 struct drm_device *dev = crtc->dev;
4205 struct drm_i915_private *dev_priv = dev->dev_private;
4206 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4207 struct intel_encoder *encoder;
4208 int pipe = intel_crtc->pipe;
4209 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
4211 if (!intel_crtc->active)
4214 intel_crtc_disable_planes(crtc);
4216 for_each_encoder_on_crtc(dev, crtc, encoder) {
4217 intel_opregion_notify_encoder(encoder, false);
4218 encoder->disable(encoder);
4221 if (intel_crtc->config.has_pch_encoder)
4222 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, false);
4223 intel_disable_pipe(dev_priv, pipe);
4225 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
4227 ironlake_pfit_disable(intel_crtc);
4229 intel_ddi_disable_pipe_clock(intel_crtc);
4231 for_each_encoder_on_crtc(dev, crtc, encoder)
4232 if (encoder->post_disable)
4233 encoder->post_disable(encoder);
4235 if (intel_crtc->config.has_pch_encoder) {
4236 lpt_disable_pch_transcoder(dev_priv);
4237 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
4238 intel_ddi_fdi_disable(crtc);
4241 intel_crtc->active = false;
4242 intel_update_watermarks(crtc);
4244 mutex_lock(&dev->struct_mutex);
4245 intel_update_fbc(dev);
4246 intel_edp_psr_update(dev);
4247 mutex_unlock(&dev->struct_mutex);
4250 static void ironlake_crtc_off(struct drm_crtc *crtc)
4252 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4253 intel_put_shared_dpll(intel_crtc);
4256 static void haswell_crtc_off(struct drm_crtc *crtc)
4258 intel_ddi_put_crtc_pll(crtc);
4261 static void i9xx_pfit_enable(struct intel_crtc *crtc)
4263 struct drm_device *dev = crtc->base.dev;
4264 struct drm_i915_private *dev_priv = dev->dev_private;
4265 struct intel_crtc_config *pipe_config = &crtc->config;
4267 if (!crtc->config.gmch_pfit.control)
4271 * The panel fitter should only be adjusted whilst the pipe is disabled,
4272 * according to register description and PRM.
4274 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
4275 assert_pipe_disabled(dev_priv, crtc->pipe);
4277 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
4278 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
4280 /* Border color in case we don't scale up to the full screen. Black by
4281 * default, change to something else for debugging. */
4282 I915_WRITE(BCLRPAT(crtc->pipe), 0);
4285 #define for_each_power_domain(domain, mask) \
4286 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
4287 if ((1 << (domain)) & (mask))
4289 enum intel_display_power_domain
4290 intel_display_port_power_domain(struct intel_encoder *intel_encoder)
4292 struct drm_device *dev = intel_encoder->base.dev;
4293 struct intel_digital_port *intel_dig_port;
4295 switch (intel_encoder->type) {
4296 case INTEL_OUTPUT_UNKNOWN:
4297 /* Only DDI platforms should ever use this output type */
4298 WARN_ON_ONCE(!HAS_DDI(dev));
4299 case INTEL_OUTPUT_DISPLAYPORT:
4300 case INTEL_OUTPUT_HDMI:
4301 case INTEL_OUTPUT_EDP:
4302 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
4303 switch (intel_dig_port->port) {
4305 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
4307 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
4309 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
4311 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
4314 return POWER_DOMAIN_PORT_OTHER;
4316 case INTEL_OUTPUT_ANALOG:
4317 return POWER_DOMAIN_PORT_CRT;
4318 case INTEL_OUTPUT_DSI:
4319 return POWER_DOMAIN_PORT_DSI;
4321 return POWER_DOMAIN_PORT_OTHER;
4325 static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
4327 struct drm_device *dev = crtc->dev;
4328 struct intel_encoder *intel_encoder;
4329 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4330 enum i915_pipe pipe = intel_crtc->pipe;
4331 bool pfit_enabled = intel_crtc->config.pch_pfit.enabled;
4333 enum transcoder transcoder;
4335 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
4337 mask = BIT(POWER_DOMAIN_PIPE(pipe));
4338 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
4340 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
4342 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
4343 mask |= BIT(intel_display_port_power_domain(intel_encoder));
4348 void intel_display_set_init_power(struct drm_i915_private *dev_priv,
4351 if (dev_priv->power_domains.init_power_on == enable)
4355 intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
4357 intel_display_power_put(dev_priv, POWER_DOMAIN_INIT);
4359 dev_priv->power_domains.init_power_on = enable;
4362 static void modeset_update_crtc_power_domains(struct drm_device *dev)
4364 struct drm_i915_private *dev_priv = dev->dev_private;
4365 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
4366 struct intel_crtc *crtc;
4369 * First get all needed power domains, then put all unneeded, to avoid
4370 * any unnecessary toggling of the power wells.
4372 for_each_intel_crtc(dev, crtc) {
4373 enum intel_display_power_domain domain;
4375 if (!crtc->base.enabled)
4378 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
4380 for_each_power_domain(domain, pipe_domains[crtc->pipe])
4381 intel_display_power_get(dev_priv, domain);
4384 for_each_intel_crtc(dev, crtc) {
4385 enum intel_display_power_domain domain;
4387 for_each_power_domain(domain, crtc->enabled_power_domains)
4388 intel_display_power_put(dev_priv, domain);
4390 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
4393 intel_display_set_init_power(dev_priv, false);
4396 int valleyview_get_vco(struct drm_i915_private *dev_priv)
4398 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
4400 /* Obtain SKU information */
4401 mutex_lock(&dev_priv->dpio_lock);
4402 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
4403 CCK_FUSE_HPLL_FREQ_MASK;
4404 mutex_unlock(&dev_priv->dpio_lock);
4406 return vco_freq[hpll_freq];
4409 /* Adjust CDclk dividers to allow high res or save power if possible */
4410 static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
4412 struct drm_i915_private *dev_priv = dev->dev_private;
4415 WARN_ON(valleyview_cur_cdclk(dev_priv) != dev_priv->vlv_cdclk_freq);
4416 dev_priv->vlv_cdclk_freq = cdclk;
4418 if (cdclk >= 320) /* jump to highest voltage for 400MHz too */
4420 else if (cdclk == 266)
4425 mutex_lock(&dev_priv->rps.hw_lock);
4426 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
4427 val &= ~DSPFREQGUAR_MASK;
4428 val |= (cmd << DSPFREQGUAR_SHIFT);
4429 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
4430 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
4431 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
4433 DRM_ERROR("timed out waiting for CDclk change\n");
4435 mutex_unlock(&dev_priv->rps.hw_lock);
4440 vco = valleyview_get_vco(dev_priv);
4441 divider = ((vco << 1) / cdclk) - 1;
4443 mutex_lock(&dev_priv->dpio_lock);
4444 /* adjust cdclk divider */
4445 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
4448 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
4449 mutex_unlock(&dev_priv->dpio_lock);
4452 mutex_lock(&dev_priv->dpio_lock);
4453 /* adjust self-refresh exit latency value */
4454 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
4458 * For high bandwidth configs, we set a higher latency in the bunit
4459 * so that the core display fetch happens in time to avoid underruns.
4462 val |= 4500 / 250; /* 4.5 usec */
4464 val |= 3000 / 250; /* 3.0 usec */
4465 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
4466 mutex_unlock(&dev_priv->dpio_lock);
4468 /* Since we changed the CDclk, we need to update the GMBUSFREQ too */
4469 intel_i2c_reset(dev);
4472 int valleyview_cur_cdclk(struct drm_i915_private *dev_priv)
4477 vco = valleyview_get_vco(dev_priv);
4479 mutex_lock(&dev_priv->dpio_lock);
4480 divider = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
4481 mutex_unlock(&dev_priv->dpio_lock);
4485 cur_cdclk = (vco << 1) / (divider + 1);
4490 static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
4494 * Really only a few cases to deal with, as only 4 CDclks are supported:
4499 * So we check to see whether we're above 90% of the lower bin and
4502 if (max_pixclk > 288000) {
4504 } else if (max_pixclk > 240000) {
4508 /* Looks like the 200MHz CDclk freq doesn't work on some configs */
4511 /* compute the max pixel clock for new configuration */
4512 static int intel_mode_max_pixclk(struct drm_i915_private *dev_priv)
4514 struct drm_device *dev = dev_priv->dev;
4515 struct intel_crtc *intel_crtc;
4518 for_each_intel_crtc(dev, intel_crtc) {
4519 if (intel_crtc->new_enabled)
4520 max_pixclk = max(max_pixclk,
4521 intel_crtc->new_config->adjusted_mode.crtc_clock);
4527 static void valleyview_modeset_global_pipes(struct drm_device *dev,
4528 unsigned *prepare_pipes)
4530 struct drm_i915_private *dev_priv = dev->dev_private;
4531 struct intel_crtc *intel_crtc;
4532 int max_pixclk = intel_mode_max_pixclk(dev_priv);
4534 if (valleyview_calc_cdclk(dev_priv, max_pixclk) ==
4535 dev_priv->vlv_cdclk_freq)
4538 /* disable/enable all currently active pipes while we change cdclk */
4539 for_each_intel_crtc(dev, intel_crtc)
4540 if (intel_crtc->base.enabled)
4541 *prepare_pipes |= (1 << intel_crtc->pipe);
4544 static void valleyview_modeset_global_resources(struct drm_device *dev)
4546 struct drm_i915_private *dev_priv = dev->dev_private;
4547 int max_pixclk = intel_mode_max_pixclk(dev_priv);
4548 int req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
4550 if (req_cdclk != dev_priv->vlv_cdclk_freq)
4551 valleyview_set_cdclk(dev, req_cdclk);
4552 modeset_update_crtc_power_domains(dev);
4555 static void valleyview_crtc_enable(struct drm_crtc *crtc)
4557 struct drm_device *dev = crtc->dev;
4558 struct drm_i915_private *dev_priv = dev->dev_private;
4559 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4560 struct intel_encoder *encoder;
4561 int pipe = intel_crtc->pipe;
4562 int plane = intel_crtc->plane;
4566 WARN_ON(!crtc->enabled);
4568 if (intel_crtc->active)
4571 is_dsi = intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI);
4573 if (!is_dsi && !IS_CHERRYVIEW(dev))
4574 vlv_prepare_pll(intel_crtc);
4576 /* Set up the display plane register */
4577 dspcntr = DISPPLANE_GAMMA_ENABLE;
4579 if (intel_crtc->config.has_dp_encoder)
4580 intel_dp_set_m_n(intel_crtc);
4582 intel_set_pipe_timings(intel_crtc);
4584 /* pipesrc and dspsize control the size that is scaled from,
4585 * which should always be the user's requested size.
4587 I915_WRITE(DSPSIZE(plane),
4588 ((intel_crtc->config.pipe_src_h - 1) << 16) |
4589 (intel_crtc->config.pipe_src_w - 1));
4590 I915_WRITE(DSPPOS(plane), 0);
4592 i9xx_set_pipeconf(intel_crtc);
4594 I915_WRITE(DSPCNTR(plane), dspcntr);
4595 POSTING_READ(DSPCNTR(plane));
4597 dev_priv->display.update_primary_plane(crtc, crtc->primary->fb,
4600 intel_crtc->active = true;
4602 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4604 for_each_encoder_on_crtc(dev, crtc, encoder)
4605 if (encoder->pre_pll_enable)
4606 encoder->pre_pll_enable(encoder);
4609 if (IS_CHERRYVIEW(dev))
4610 chv_enable_pll(intel_crtc);
4612 vlv_enable_pll(intel_crtc);
4615 for_each_encoder_on_crtc(dev, crtc, encoder)
4616 if (encoder->pre_enable)
4617 encoder->pre_enable(encoder);
4619 i9xx_pfit_enable(intel_crtc);
4621 intel_crtc_load_lut(crtc);
4623 intel_update_watermarks(crtc);
4624 intel_enable_pipe(intel_crtc);
4626 for_each_encoder_on_crtc(dev, crtc, encoder)
4627 encoder->enable(encoder);
4629 intel_crtc_enable_planes(crtc);
4631 drm_crtc_vblank_on(crtc);
4633 /* Underruns don't raise interrupts, so check manually. */
4634 i9xx_check_fifo_underruns(dev);
4637 static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
4639 struct drm_device *dev = crtc->base.dev;
4640 struct drm_i915_private *dev_priv = dev->dev_private;
4642 I915_WRITE(FP0(crtc->pipe), crtc->config.dpll_hw_state.fp0);
4643 I915_WRITE(FP1(crtc->pipe), crtc->config.dpll_hw_state.fp1);
4646 static void i9xx_crtc_enable(struct drm_crtc *crtc)
4648 struct drm_device *dev = crtc->dev;
4649 struct drm_i915_private *dev_priv = dev->dev_private;
4650 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4651 struct intel_encoder *encoder;
4652 int pipe = intel_crtc->pipe;
4653 int plane = intel_crtc->plane;
4656 WARN_ON(!crtc->enabled);
4658 if (intel_crtc->active)
4661 i9xx_set_pll_dividers(intel_crtc);
4663 /* Set up the display plane register */
4664 dspcntr = DISPPLANE_GAMMA_ENABLE;
4667 dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
4669 dspcntr |= DISPPLANE_SEL_PIPE_B;
4671 if (intel_crtc->config.has_dp_encoder)
4672 intel_dp_set_m_n(intel_crtc);
4674 intel_set_pipe_timings(intel_crtc);
4676 /* pipesrc and dspsize control the size that is scaled from,
4677 * which should always be the user's requested size.
4679 I915_WRITE(DSPSIZE(plane),
4680 ((intel_crtc->config.pipe_src_h - 1) << 16) |
4681 (intel_crtc->config.pipe_src_w - 1));
4682 I915_WRITE(DSPPOS(plane), 0);
4684 i9xx_set_pipeconf(intel_crtc);
4686 I915_WRITE(DSPCNTR(plane), dspcntr);
4687 POSTING_READ(DSPCNTR(plane));
4689 dev_priv->display.update_primary_plane(crtc, crtc->primary->fb,
4692 intel_crtc->active = true;
4695 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4697 for_each_encoder_on_crtc(dev, crtc, encoder)
4698 if (encoder->pre_enable)
4699 encoder->pre_enable(encoder);
4701 i9xx_enable_pll(intel_crtc);
4703 i9xx_pfit_enable(intel_crtc);
4705 intel_crtc_load_lut(crtc);
4707 intel_update_watermarks(crtc);
4708 intel_enable_pipe(intel_crtc);
4710 for_each_encoder_on_crtc(dev, crtc, encoder)
4711 encoder->enable(encoder);
4713 intel_crtc_enable_planes(crtc);
4716 * Gen2 reports pipe underruns whenever all planes are disabled.
4717 * So don't enable underrun reporting before at least some planes
4719 * FIXME: Need to fix the logic to work when we turn off all planes
4720 * but leave the pipe running.
4723 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4725 drm_crtc_vblank_on(crtc);
4727 /* Underruns don't raise interrupts, so check manually. */
4728 i9xx_check_fifo_underruns(dev);
4731 static void i9xx_pfit_disable(struct intel_crtc *crtc)
4733 struct drm_device *dev = crtc->base.dev;
4734 struct drm_i915_private *dev_priv = dev->dev_private;
4736 if (!crtc->config.gmch_pfit.control)
4739 assert_pipe_disabled(dev_priv, crtc->pipe);
4741 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
4742 I915_READ(PFIT_CONTROL));
4743 I915_WRITE(PFIT_CONTROL, 0);
4746 static void i9xx_crtc_disable(struct drm_crtc *crtc)
4748 struct drm_device *dev = crtc->dev;
4749 struct drm_i915_private *dev_priv = dev->dev_private;
4750 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4751 struct intel_encoder *encoder;
4752 int pipe = intel_crtc->pipe;
4754 if (!intel_crtc->active)
4758 * Gen2 reports pipe underruns whenever all planes are disabled.
4759 * So diasble underrun reporting before all the planes get disabled.
4760 * FIXME: Need to fix the logic to work when we turn off all planes
4761 * but leave the pipe running.
4764 intel_set_cpu_fifo_underrun_reporting(dev, pipe, false);
4766 intel_crtc_disable_planes(crtc);
4768 for_each_encoder_on_crtc(dev, crtc, encoder)
4769 encoder->disable(encoder);
4772 * On gen2 planes are double buffered but the pipe isn't, so we must
4773 * wait for planes to fully turn off before disabling the pipe.
4776 intel_wait_for_vblank(dev, pipe);
4778 intel_disable_pipe(dev_priv, pipe);
4780 i9xx_pfit_disable(intel_crtc);
4782 for_each_encoder_on_crtc(dev, crtc, encoder)
4783 if (encoder->post_disable)
4784 encoder->post_disable(encoder);
4786 if (!intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI)) {
4787 if (IS_CHERRYVIEW(dev))
4788 chv_disable_pll(dev_priv, pipe);
4789 else if (IS_VALLEYVIEW(dev))
4790 vlv_disable_pll(dev_priv, pipe);
4792 i9xx_disable_pll(dev_priv, pipe);
4796 intel_set_cpu_fifo_underrun_reporting(dev, pipe, false);
4798 intel_crtc->active = false;
4799 intel_update_watermarks(crtc);
4801 mutex_lock(&dev->struct_mutex);
4802 intel_update_fbc(dev);
4803 intel_edp_psr_update(dev);
4804 mutex_unlock(&dev->struct_mutex);
4807 static void i9xx_crtc_off(struct drm_crtc *crtc)
4811 static void intel_crtc_update_sarea(struct drm_crtc *crtc,
4814 struct drm_device *dev = crtc->dev;
4815 struct drm_i915_master_private *master_priv = dev->dev_private;
4816 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4817 int pipe = intel_crtc->pipe;
4820 if (!dev->primary->master)
4823 master_priv = dev->primary->master->driver_priv;
4825 if (!master_priv->sarea_priv)
4830 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
4831 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
4834 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
4835 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
4838 DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
4844 * Sets the power management mode of the pipe and plane.
4846 void intel_crtc_update_dpms(struct drm_crtc *crtc)
4848 struct drm_device *dev = crtc->dev;
4849 struct drm_i915_private *dev_priv = dev->dev_private;
4850 struct intel_encoder *intel_encoder;
4851 bool enable = false;
4853 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
4854 enable |= intel_encoder->connectors_active;
4857 dev_priv->display.crtc_enable(crtc);
4859 dev_priv->display.crtc_disable(crtc);
4861 intel_crtc_update_sarea(crtc, enable);
4864 static void intel_crtc_disable(struct drm_crtc *crtc)
4866 struct drm_device *dev = crtc->dev;
4867 struct drm_connector *connector;
4868 struct drm_i915_private *dev_priv = dev->dev_private;
4870 /* crtc should still be enabled when we disable it. */
4871 WARN_ON(!crtc->enabled);
4873 dev_priv->display.crtc_disable(crtc);
4874 intel_crtc_update_sarea(crtc, false);
4875 dev_priv->display.off(crtc);
4877 assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
4878 assert_cursor_disabled(dev_priv, to_intel_crtc(crtc)->pipe);
4879 assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
4881 if (crtc->primary->fb) {
4882 mutex_lock(&dev->struct_mutex);
4883 intel_unpin_fb_obj(to_intel_framebuffer(crtc->primary->fb)->obj);
4884 mutex_unlock(&dev->struct_mutex);
4885 crtc->primary->fb = NULL;
4888 /* Update computed state. */
4889 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4890 if (!connector->encoder || !connector->encoder->crtc)
4893 if (connector->encoder->crtc != crtc)
4896 connector->dpms = DRM_MODE_DPMS_OFF;
4897 to_intel_encoder(connector->encoder)->connectors_active = false;
4901 void intel_encoder_destroy(struct drm_encoder *encoder)
4903 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
4905 drm_encoder_cleanup(encoder);
4906 kfree(intel_encoder);
4909 /* Simple dpms helper for encoders with just one connector, no cloning and only
4910 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
4911 * state of the entire output pipe. */
4912 static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
4914 if (mode == DRM_MODE_DPMS_ON) {
4915 encoder->connectors_active = true;
4917 intel_crtc_update_dpms(encoder->base.crtc);
4919 encoder->connectors_active = false;
4921 intel_crtc_update_dpms(encoder->base.crtc);
4925 /* Cross check the actual hw state with our own modeset state tracking (and it's
4926 * internal consistency). */
4927 static void intel_connector_check_state(struct intel_connector *connector)
4929 if (connector->get_hw_state(connector)) {
4930 struct intel_encoder *encoder = connector->encoder;
4931 struct drm_crtc *crtc;
4932 bool encoder_enabled;
4933 enum i915_pipe pipe;
4935 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
4936 connector->base.base.id,
4937 connector->base.name);
4939 WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
4940 "wrong connector dpms state\n");
4941 WARN(connector->base.encoder != &encoder->base,
4942 "active connector not linked to encoder\n");
4943 WARN(!encoder->connectors_active,
4944 "encoder->connectors_active not set\n");
4946 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
4947 WARN(!encoder_enabled, "encoder not enabled\n");
4948 if (WARN_ON(!encoder->base.crtc))
4951 crtc = encoder->base.crtc;
4953 WARN(!crtc->enabled, "crtc not enabled\n");
4954 WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
4955 WARN(pipe != to_intel_crtc(crtc)->pipe,
4956 "encoder active on the wrong pipe\n");
4960 /* Even simpler default implementation, if there's really no special case to
4962 void intel_connector_dpms(struct drm_connector *connector, int mode)
4964 /* All the simple cases only support two dpms states. */
4965 if (mode != DRM_MODE_DPMS_ON)
4966 mode = DRM_MODE_DPMS_OFF;
4968 if (mode == connector->dpms)
4971 connector->dpms = mode;
4973 /* Only need to change hw state when actually enabled */
4974 if (connector->encoder)
4975 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
4977 intel_modeset_check_state(connector->dev);
4980 /* Simple connector->get_hw_state implementation for encoders that support only
4981 * one connector and no cloning and hence the encoder state determines the state
4982 * of the connector. */
4983 bool intel_connector_get_hw_state(struct intel_connector *connector)
4985 enum i915_pipe pipe = 0;
4986 struct intel_encoder *encoder = connector->encoder;
4988 return encoder->get_hw_state(encoder, &pipe);
4991 static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum i915_pipe pipe,
4992 struct intel_crtc_config *pipe_config)
4994 struct drm_i915_private *dev_priv = dev->dev_private;
4995 struct intel_crtc *pipe_B_crtc =
4996 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
4998 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
4999 pipe_name(pipe), pipe_config->fdi_lanes);
5000 if (pipe_config->fdi_lanes > 4) {
5001 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
5002 pipe_name(pipe), pipe_config->fdi_lanes);
5006 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
5007 if (pipe_config->fdi_lanes > 2) {
5008 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
5009 pipe_config->fdi_lanes);
5016 if (INTEL_INFO(dev)->num_pipes == 2)
5019 /* Ivybridge 3 pipe is really complicated */
5024 if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
5025 pipe_config->fdi_lanes > 2) {
5026 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
5027 pipe_name(pipe), pipe_config->fdi_lanes);
5032 if (!pipe_has_enabled_pch(pipe_B_crtc) ||
5033 pipe_B_crtc->config.fdi_lanes <= 2) {
5034 if (pipe_config->fdi_lanes > 2) {
5035 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
5036 pipe_name(pipe), pipe_config->fdi_lanes);
5040 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
5050 static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5051 struct intel_crtc_config *pipe_config)
5053 struct drm_device *dev = intel_crtc->base.dev;
5054 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
5055 int lane, link_bw, fdi_dotclock;
5056 bool setup_ok, needs_recompute = false;
5059 /* FDI is a binary signal running at ~2.7GHz, encoding
5060 * each output octet as 10 bits. The actual frequency
5061 * is stored as a divider into a 100MHz clock, and the
5062 * mode pixel clock is stored in units of 1KHz.
5063 * Hence the bw of each lane in terms of the mode signal
5066 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
5068 fdi_dotclock = adjusted_mode->crtc_clock;
5070 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
5071 pipe_config->pipe_bpp);
5073 pipe_config->fdi_lanes = lane;
5075 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
5076 link_bw, &pipe_config->fdi_m_n);
5078 setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
5079 intel_crtc->pipe, pipe_config);
5080 if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
5081 pipe_config->pipe_bpp -= 2*3;
5082 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
5083 pipe_config->pipe_bpp);
5084 needs_recompute = true;
5085 pipe_config->bw_constrained = true;
5090 if (needs_recompute)
5093 return setup_ok ? 0 : -EINVAL;
5096 static void hsw_compute_ips_config(struct intel_crtc *crtc,
5097 struct intel_crtc_config *pipe_config)
5099 pipe_config->ips_enabled = i915.enable_ips &&
5100 hsw_crtc_supports_ips(crtc) &&
5101 pipe_config->pipe_bpp <= 24;
5104 static int intel_crtc_compute_config(struct intel_crtc *crtc,
5105 struct intel_crtc_config *pipe_config)
5107 struct drm_device *dev = crtc->base.dev;
5108 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
5110 /* FIXME should check pixel clock limits on all platforms */
5111 if (INTEL_INFO(dev)->gen < 4) {
5112 struct drm_i915_private *dev_priv = dev->dev_private;
5114 dev_priv->display.get_display_clock_speed(dev);
5117 * Enable pixel doubling when the dot clock
5118 * is > 90% of the (display) core speed.
5120 * GDG double wide on either pipe,
5121 * otherwise pipe A only.
5123 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
5124 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
5126 pipe_config->double_wide = true;
5129 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
5134 * Pipe horizontal size must be even in:
5136 * - LVDS dual channel mode
5137 * - Double wide pipe
5139 if ((intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
5140 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
5141 pipe_config->pipe_src_w &= ~1;
5143 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
5144 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
5146 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
5147 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
5150 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
5151 pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
5152 } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
5153 /* only a 8bpc pipe, with 6bpc dither through the panel fitter
5155 pipe_config->pipe_bpp = 8*3;
5159 hsw_compute_ips_config(crtc, pipe_config);
5161 /* XXX: PCH clock sharing is done in ->mode_set, so make sure the old
5162 * clock survives for now. */
5163 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
5164 pipe_config->shared_dpll = crtc->config.shared_dpll;
5166 if (pipe_config->has_pch_encoder)
5167 return ironlake_fdi_compute_config(crtc, pipe_config);
5172 static int valleyview_get_display_clock_speed(struct drm_device *dev)
5174 return 400000; /* FIXME */
5177 static int i945_get_display_clock_speed(struct drm_device *dev)
5182 static int i915_get_display_clock_speed(struct drm_device *dev)
5187 static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
5192 static int pnv_get_display_clock_speed(struct drm_device *dev)
5196 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
5198 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
5199 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
5201 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
5203 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
5205 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
5208 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
5209 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
5211 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
5216 static int i915gm_get_display_clock_speed(struct drm_device *dev)
5220 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
5222 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
5225 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
5226 case GC_DISPLAY_CLOCK_333_MHZ:
5229 case GC_DISPLAY_CLOCK_190_200_MHZ:
5235 static int i865_get_display_clock_speed(struct drm_device *dev)
5240 static int i855_get_display_clock_speed(struct drm_device *dev)
5243 /* Assume that the hardware is in the high speed state. This
5244 * should be the default.
5246 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
5247 case GC_CLOCK_133_200:
5248 case GC_CLOCK_100_200:
5250 case GC_CLOCK_166_250:
5252 case GC_CLOCK_100_133:
5256 /* Shouldn't happen */
5260 static int i830_get_display_clock_speed(struct drm_device *dev)
5266 intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
5268 while (*num > DATA_LINK_M_N_MASK ||
5269 *den > DATA_LINK_M_N_MASK) {
5275 static void compute_m_n(unsigned int m, unsigned int n,
5276 uint32_t *ret_m, uint32_t *ret_n)
5278 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
5279 *ret_m = div_u64((uint64_t) m * *ret_n, n);
5280 intel_reduce_m_n_ratio(ret_m, ret_n);
5284 intel_link_compute_m_n(int bits_per_pixel, int nlanes,
5285 int pixel_clock, int link_clock,
5286 struct intel_link_m_n *m_n)
5290 compute_m_n(bits_per_pixel * pixel_clock,
5291 link_clock * nlanes * 8,
5292 &m_n->gmch_m, &m_n->gmch_n);
5294 compute_m_n(pixel_clock, link_clock,
5295 &m_n->link_m, &m_n->link_n);
5298 static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
5300 if (i915.panel_use_ssc >= 0)
5301 return i915.panel_use_ssc != 0;
5302 return dev_priv->vbt.lvds_use_ssc
5303 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
5306 static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
5308 struct drm_device *dev = crtc->dev;
5309 struct drm_i915_private *dev_priv = dev->dev_private;
5312 if (IS_VALLEYVIEW(dev)) {
5314 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
5315 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
5316 refclk = dev_priv->vbt.lvds_ssc_freq;
5317 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
5318 } else if (!IS_GEN2(dev)) {
5327 static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
5329 return (1 << dpll->n) << 16 | dpll->m2;
5332 static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
5334 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
5337 static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
5338 intel_clock_t *reduced_clock)
5340 struct drm_device *dev = crtc->base.dev;
5343 if (IS_PINEVIEW(dev)) {
5344 fp = pnv_dpll_compute_fp(&crtc->config.dpll);
5346 fp2 = pnv_dpll_compute_fp(reduced_clock);
5348 fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
5350 fp2 = i9xx_dpll_compute_fp(reduced_clock);
5353 crtc->config.dpll_hw_state.fp0 = fp;
5355 crtc->lowfreq_avail = false;
5356 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
5357 reduced_clock && i915.powersave) {
5358 crtc->config.dpll_hw_state.fp1 = fp2;
5359 crtc->lowfreq_avail = true;
5361 crtc->config.dpll_hw_state.fp1 = fp;
5365 static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum i915_pipe
5371 * PLLB opamp always calibrates to max value of 0x3f, force enable it
5372 * and set it to a reasonable value instead.
5374 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
5375 reg_val &= 0xffffff00;
5376 reg_val |= 0x00000030;
5377 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
5379 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
5380 reg_val &= 0x8cffffff;
5381 reg_val = 0x8c000000;
5382 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
5384 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
5385 reg_val &= 0xffffff00;
5386 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
5388 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
5389 reg_val &= 0x00ffffff;
5390 reg_val |= 0xb0000000;
5391 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
5394 static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
5395 struct intel_link_m_n *m_n)
5397 struct drm_device *dev = crtc->base.dev;
5398 struct drm_i915_private *dev_priv = dev->dev_private;
5399 int pipe = crtc->pipe;
5401 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
5402 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
5403 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
5404 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
5407 static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
5408 struct intel_link_m_n *m_n)
5410 struct drm_device *dev = crtc->base.dev;
5411 struct drm_i915_private *dev_priv = dev->dev_private;
5412 int pipe = crtc->pipe;
5413 enum transcoder transcoder = crtc->config.cpu_transcoder;
5415 if (INTEL_INFO(dev)->gen >= 5) {
5416 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
5417 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
5418 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
5419 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
5421 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
5422 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
5423 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
5424 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
5428 static void intel_dp_set_m_n(struct intel_crtc *crtc)
5430 if (crtc->config.has_pch_encoder)
5431 intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
5433 intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
5436 static void vlv_update_pll(struct intel_crtc *crtc)
5441 * Enable DPIO clock input. We should never disable the reference
5442 * clock for pipe B, since VGA hotplug / manual detection depends
5445 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
5446 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
5447 /* We should never disable this, set it here for state tracking */
5448 if (crtc->pipe == PIPE_B)
5449 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
5450 dpll |= DPLL_VCO_ENABLE;
5451 crtc->config.dpll_hw_state.dpll = dpll;
5453 dpll_md = (crtc->config.pixel_multiplier - 1)
5454 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
5455 crtc->config.dpll_hw_state.dpll_md = dpll_md;
5458 static void vlv_prepare_pll(struct intel_crtc *crtc)
5460 struct drm_device *dev = crtc->base.dev;
5461 struct drm_i915_private *dev_priv = dev->dev_private;
5462 int pipe = crtc->pipe;
5464 u32 bestn, bestm1, bestm2, bestp1, bestp2;
5465 u32 coreclk, reg_val;
5467 mutex_lock(&dev_priv->dpio_lock);
5469 bestn = crtc->config.dpll.n;
5470 bestm1 = crtc->config.dpll.m1;
5471 bestm2 = crtc->config.dpll.m2;
5472 bestp1 = crtc->config.dpll.p1;
5473 bestp2 = crtc->config.dpll.p2;
5475 /* See eDP HDMI DPIO driver vbios notes doc */
5477 /* PLL B needs special handling */
5479 vlv_pllb_recal_opamp(dev_priv, pipe);
5481 /* Set up Tx target for periodic Rcomp update */
5482 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
5484 /* Disable target IRef on PLL */
5485 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
5486 reg_val &= 0x00ffffff;
5487 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
5489 /* Disable fast lock */
5490 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
5492 /* Set idtafcrecal before PLL is enabled */
5493 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
5494 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
5495 mdiv |= ((bestn << DPIO_N_SHIFT));
5496 mdiv |= (1 << DPIO_K_SHIFT);
5499 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
5500 * but we don't support that).
5501 * Note: don't use the DAC post divider as it seems unstable.
5503 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
5504 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
5506 mdiv |= DPIO_ENABLE_CALIBRATION;
5507 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
5509 /* Set HBR and RBR LPF coefficients */
5510 if (crtc->config.port_clock == 162000 ||
5511 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_ANALOG) ||
5512 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI))
5513 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
5516 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
5519 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP) ||
5520 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT)) {
5521 /* Use SSC source */
5523 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
5526 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
5528 } else { /* HDMI or VGA */
5529 /* Use bend source */
5531 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
5534 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
5538 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
5539 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
5540 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT) ||
5541 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))
5542 coreclk |= 0x01000000;
5543 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
5545 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
5546 mutex_unlock(&dev_priv->dpio_lock);
5549 static void chv_update_pll(struct intel_crtc *crtc)
5551 struct drm_device *dev = crtc->base.dev;
5552 struct drm_i915_private *dev_priv = dev->dev_private;
5553 int pipe = crtc->pipe;
5554 int dpll_reg = DPLL(crtc->pipe);
5555 enum dpio_channel port = vlv_pipe_to_channel(pipe);
5556 u32 loopfilter, intcoeff;
5557 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
5560 crtc->config.dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
5561 DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
5564 crtc->config.dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
5566 crtc->config.dpll_hw_state.dpll_md =
5567 (crtc->config.pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
5569 bestn = crtc->config.dpll.n;
5570 bestm2_frac = crtc->config.dpll.m2 & 0x3fffff;
5571 bestm1 = crtc->config.dpll.m1;
5572 bestm2 = crtc->config.dpll.m2 >> 22;
5573 bestp1 = crtc->config.dpll.p1;
5574 bestp2 = crtc->config.dpll.p2;
5577 * Enable Refclk and SSC
5579 I915_WRITE(dpll_reg,
5580 crtc->config.dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
5582 mutex_lock(&dev_priv->dpio_lock);
5584 /* p1 and p2 divider */
5585 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
5586 5 << DPIO_CHV_S1_DIV_SHIFT |
5587 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
5588 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
5589 1 << DPIO_CHV_K_DIV_SHIFT);
5591 /* Feedback post-divider - m2 */
5592 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
5594 /* Feedback refclk divider - n and m1 */
5595 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
5596 DPIO_CHV_M1_DIV_BY_2 |
5597 1 << DPIO_CHV_N_DIV_SHIFT);
5599 /* M2 fraction division */
5600 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
5602 /* M2 fraction division enable */
5603 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port),
5604 DPIO_CHV_FRAC_DIV_EN |
5605 (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT));
5608 refclk = i9xx_get_refclk(&crtc->base, 0);
5609 loopfilter = 5 << DPIO_CHV_PROP_COEFF_SHIFT |
5610 2 << DPIO_CHV_GAIN_CTRL_SHIFT;
5611 if (refclk == 100000)
5613 else if (refclk == 38400)
5617 loopfilter |= intcoeff << DPIO_CHV_INT_COEFF_SHIFT;
5618 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
5621 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
5622 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
5625 mutex_unlock(&dev_priv->dpio_lock);
5628 static void i9xx_update_pll(struct intel_crtc *crtc,
5629 intel_clock_t *reduced_clock,
5632 struct drm_device *dev = crtc->base.dev;
5633 struct drm_i915_private *dev_priv = dev->dev_private;
5636 struct dpll *clock = &crtc->config.dpll;
5638 i9xx_update_pll_dividers(crtc, reduced_clock);
5640 is_sdvo = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_SDVO) ||
5641 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
5643 dpll = DPLL_VGA_MODE_DIS;
5645 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS))
5646 dpll |= DPLLB_MODE_LVDS;
5648 dpll |= DPLLB_MODE_DAC_SERIAL;
5650 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
5651 dpll |= (crtc->config.pixel_multiplier - 1)
5652 << SDVO_MULTIPLIER_SHIFT_HIRES;
5656 dpll |= DPLL_SDVO_HIGH_SPEED;
5658 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
5659 dpll |= DPLL_SDVO_HIGH_SPEED;
5661 /* compute bitmask from p1 value */
5662 if (IS_PINEVIEW(dev))
5663 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
5665 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5666 if (IS_G4X(dev) && reduced_clock)
5667 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
5669 switch (clock->p2) {
5671 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
5674 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
5677 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
5680 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
5683 if (INTEL_INFO(dev)->gen >= 4)
5684 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
5686 if (crtc->config.sdvo_tv_clock)
5687 dpll |= PLL_REF_INPUT_TVCLKINBC;
5688 else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
5689 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5690 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5692 dpll |= PLL_REF_INPUT_DREFCLK;
5694 dpll |= DPLL_VCO_ENABLE;
5695 crtc->config.dpll_hw_state.dpll = dpll;
5697 if (INTEL_INFO(dev)->gen >= 4) {
5698 u32 dpll_md = (crtc->config.pixel_multiplier - 1)
5699 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
5700 crtc->config.dpll_hw_state.dpll_md = dpll_md;
5704 static void i8xx_update_pll(struct intel_crtc *crtc,
5705 intel_clock_t *reduced_clock,
5708 struct drm_device *dev = crtc->base.dev;
5709 struct drm_i915_private *dev_priv = dev->dev_private;
5711 struct dpll *clock = &crtc->config.dpll;
5713 i9xx_update_pll_dividers(crtc, reduced_clock);
5715 dpll = DPLL_VGA_MODE_DIS;
5717 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS)) {
5718 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5721 dpll |= PLL_P1_DIVIDE_BY_TWO;
5723 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5725 dpll |= PLL_P2_DIVIDE_BY_4;
5728 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DVO))
5729 dpll |= DPLL_DVO_2X_MODE;
5731 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
5732 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5733 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5735 dpll |= PLL_REF_INPUT_DREFCLK;
5737 dpll |= DPLL_VCO_ENABLE;
5738 crtc->config.dpll_hw_state.dpll = dpll;
5741 static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
5743 struct drm_device *dev = intel_crtc->base.dev;
5744 struct drm_i915_private *dev_priv = dev->dev_private;
5745 enum i915_pipe pipe = intel_crtc->pipe;
5746 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
5747 struct drm_display_mode *adjusted_mode =
5748 &intel_crtc->config.adjusted_mode;
5749 uint32_t crtc_vtotal, crtc_vblank_end;
5752 /* We need to be careful not to changed the adjusted mode, for otherwise
5753 * the hw state checker will get angry at the mismatch. */
5754 crtc_vtotal = adjusted_mode->crtc_vtotal;
5755 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
5757 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
5758 /* the chip adds 2 halflines automatically */
5760 crtc_vblank_end -= 1;
5762 if (intel_pipe_has_type(&intel_crtc->base, INTEL_OUTPUT_SDVO))
5763 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
5765 vsyncshift = adjusted_mode->crtc_hsync_start -
5766 adjusted_mode->crtc_htotal / 2;
5768 vsyncshift += adjusted_mode->crtc_htotal;
5771 if (INTEL_INFO(dev)->gen > 3)
5772 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
5774 I915_WRITE(HTOTAL(cpu_transcoder),
5775 (adjusted_mode->crtc_hdisplay - 1) |
5776 ((adjusted_mode->crtc_htotal - 1) << 16));
5777 I915_WRITE(HBLANK(cpu_transcoder),
5778 (adjusted_mode->crtc_hblank_start - 1) |
5779 ((adjusted_mode->crtc_hblank_end - 1) << 16));
5780 I915_WRITE(HSYNC(cpu_transcoder),
5781 (adjusted_mode->crtc_hsync_start - 1) |
5782 ((adjusted_mode->crtc_hsync_end - 1) << 16));
5784 I915_WRITE(VTOTAL(cpu_transcoder),
5785 (adjusted_mode->crtc_vdisplay - 1) |
5786 ((crtc_vtotal - 1) << 16));
5787 I915_WRITE(VBLANK(cpu_transcoder),
5788 (adjusted_mode->crtc_vblank_start - 1) |
5789 ((crtc_vblank_end - 1) << 16));
5790 I915_WRITE(VSYNC(cpu_transcoder),
5791 (adjusted_mode->crtc_vsync_start - 1) |
5792 ((adjusted_mode->crtc_vsync_end - 1) << 16));
5794 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
5795 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
5796 * documented on the DDI_FUNC_CTL register description, EDP Input Select
5798 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
5799 (pipe == PIPE_B || pipe == PIPE_C))
5800 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
5802 /* pipesrc controls the size that is scaled from, which should
5803 * always be the user's requested size.
5805 I915_WRITE(PIPESRC(pipe),
5806 ((intel_crtc->config.pipe_src_w - 1) << 16) |
5807 (intel_crtc->config.pipe_src_h - 1));
5810 static void intel_get_pipe_timings(struct intel_crtc *crtc,
5811 struct intel_crtc_config *pipe_config)
5813 struct drm_device *dev = crtc->base.dev;
5814 struct drm_i915_private *dev_priv = dev->dev_private;
5815 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
5818 tmp = I915_READ(HTOTAL(cpu_transcoder));
5819 pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
5820 pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
5821 tmp = I915_READ(HBLANK(cpu_transcoder));
5822 pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
5823 pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
5824 tmp = I915_READ(HSYNC(cpu_transcoder));
5825 pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
5826 pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
5828 tmp = I915_READ(VTOTAL(cpu_transcoder));
5829 pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
5830 pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
5831 tmp = I915_READ(VBLANK(cpu_transcoder));
5832 pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
5833 pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
5834 tmp = I915_READ(VSYNC(cpu_transcoder));
5835 pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
5836 pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
5838 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
5839 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
5840 pipe_config->adjusted_mode.crtc_vtotal += 1;
5841 pipe_config->adjusted_mode.crtc_vblank_end += 1;
5844 tmp = I915_READ(PIPESRC(crtc->pipe));
5845 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
5846 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
5848 pipe_config->requested_mode.vdisplay = pipe_config->pipe_src_h;
5849 pipe_config->requested_mode.hdisplay = pipe_config->pipe_src_w;
5852 void intel_mode_from_pipe_config(struct drm_display_mode *mode,
5853 struct intel_crtc_config *pipe_config)
5855 mode->hdisplay = pipe_config->adjusted_mode.crtc_hdisplay;
5856 mode->htotal = pipe_config->adjusted_mode.crtc_htotal;
5857 mode->hsync_start = pipe_config->adjusted_mode.crtc_hsync_start;
5858 mode->hsync_end = pipe_config->adjusted_mode.crtc_hsync_end;
5860 mode->vdisplay = pipe_config->adjusted_mode.crtc_vdisplay;
5861 mode->vtotal = pipe_config->adjusted_mode.crtc_vtotal;
5862 mode->vsync_start = pipe_config->adjusted_mode.crtc_vsync_start;
5863 mode->vsync_end = pipe_config->adjusted_mode.crtc_vsync_end;
5865 mode->flags = pipe_config->adjusted_mode.flags;
5867 mode->clock = pipe_config->adjusted_mode.crtc_clock;
5868 mode->flags |= pipe_config->adjusted_mode.flags;
5871 static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
5873 struct drm_device *dev = intel_crtc->base.dev;
5874 struct drm_i915_private *dev_priv = dev->dev_private;
5879 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
5880 I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE)
5881 pipeconf |= PIPECONF_ENABLE;
5883 if (intel_crtc->config.double_wide)
5884 pipeconf |= PIPECONF_DOUBLE_WIDE;
5886 /* only g4x and later have fancy bpc/dither controls */
5887 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
5888 /* Bspec claims that we can't use dithering for 30bpp pipes. */
5889 if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
5890 pipeconf |= PIPECONF_DITHER_EN |
5891 PIPECONF_DITHER_TYPE_SP;
5893 switch (intel_crtc->config.pipe_bpp) {
5895 pipeconf |= PIPECONF_6BPC;
5898 pipeconf |= PIPECONF_8BPC;
5901 pipeconf |= PIPECONF_10BPC;
5904 /* Case prevented by intel_choose_pipe_bpp_dither. */
5909 if (HAS_PIPE_CXSR(dev)) {
5910 if (intel_crtc->lowfreq_avail) {
5911 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
5912 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
5914 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
5918 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
5919 if (INTEL_INFO(dev)->gen < 4 ||
5920 intel_pipe_has_type(&intel_crtc->base, INTEL_OUTPUT_SDVO))
5921 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
5923 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
5925 pipeconf |= PIPECONF_PROGRESSIVE;
5927 if (IS_VALLEYVIEW(dev) && intel_crtc->config.limited_color_range)
5928 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
5930 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
5931 POSTING_READ(PIPECONF(intel_crtc->pipe));
5934 static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
5936 struct drm_framebuffer *fb)
5938 struct drm_device *dev = crtc->dev;
5939 struct drm_i915_private *dev_priv = dev->dev_private;
5940 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5941 int refclk, num_connectors = 0;
5942 intel_clock_t clock, reduced_clock;
5943 bool ok, has_reduced_clock = false;
5944 bool is_lvds = false, is_dsi = false;
5945 struct intel_encoder *encoder;
5946 const intel_limit_t *limit;
5948 for_each_encoder_on_crtc(dev, crtc, encoder) {
5949 switch (encoder->type) {
5950 case INTEL_OUTPUT_LVDS:
5953 case INTEL_OUTPUT_DSI:
5964 if (!intel_crtc->config.clock_set) {
5965 refclk = i9xx_get_refclk(crtc, num_connectors);
5968 * Returns a set of divisors for the desired target clock with
5969 * the given refclk, or FALSE. The returned values represent
5970 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
5973 limit = intel_limit(crtc, refclk);
5974 ok = dev_priv->display.find_dpll(limit, crtc,
5975 intel_crtc->config.port_clock,
5976 refclk, NULL, &clock);
5978 DRM_ERROR("Couldn't find PLL settings for mode!\n");
5982 if (is_lvds && dev_priv->lvds_downclock_avail) {
5984 * Ensure we match the reduced clock's P to the target
5985 * clock. If the clocks don't match, we can't switch
5986 * the display clock by using the FP0/FP1. In such case
5987 * we will disable the LVDS downclock feature.
5990 dev_priv->display.find_dpll(limit, crtc,
5991 dev_priv->lvds_downclock,
5995 /* Compat-code for transition, will disappear. */
5996 intel_crtc->config.dpll.n = clock.n;
5997 intel_crtc->config.dpll.m1 = clock.m1;
5998 intel_crtc->config.dpll.m2 = clock.m2;
5999 intel_crtc->config.dpll.p1 = clock.p1;
6000 intel_crtc->config.dpll.p2 = clock.p2;
6004 i8xx_update_pll(intel_crtc,
6005 has_reduced_clock ? &reduced_clock : NULL,
6007 } else if (IS_CHERRYVIEW(dev)) {
6008 chv_update_pll(intel_crtc);
6009 } else if (IS_VALLEYVIEW(dev)) {
6010 vlv_update_pll(intel_crtc);
6012 i9xx_update_pll(intel_crtc,
6013 has_reduced_clock ? &reduced_clock : NULL,
6020 static void i9xx_get_pfit_config(struct intel_crtc *crtc,
6021 struct intel_crtc_config *pipe_config)
6023 struct drm_device *dev = crtc->base.dev;
6024 struct drm_i915_private *dev_priv = dev->dev_private;
6027 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
6030 tmp = I915_READ(PFIT_CONTROL);
6031 if (!(tmp & PFIT_ENABLE))
6034 /* Check whether the pfit is attached to our pipe. */
6035 if (INTEL_INFO(dev)->gen < 4) {
6036 if (crtc->pipe != PIPE_B)
6039 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
6043 pipe_config->gmch_pfit.control = tmp;
6044 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
6045 if (INTEL_INFO(dev)->gen < 5)
6046 pipe_config->gmch_pfit.lvds_border_bits =
6047 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
6050 static void vlv_crtc_clock_get(struct intel_crtc *crtc,
6051 struct intel_crtc_config *pipe_config)
6053 struct drm_device *dev = crtc->base.dev;
6054 struct drm_i915_private *dev_priv = dev->dev_private;
6055 int pipe = pipe_config->cpu_transcoder;
6056 intel_clock_t clock;
6058 int refclk = 100000;
6060 mutex_lock(&dev_priv->dpio_lock);
6061 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
6062 mutex_unlock(&dev_priv->dpio_lock);
6064 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
6065 clock.m2 = mdiv & DPIO_M2DIV_MASK;
6066 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
6067 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
6068 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
6070 vlv_clock(refclk, &clock);
6072 /* clock.dot is the fast clock */
6073 pipe_config->port_clock = clock.dot / 5;
6076 static void i9xx_get_plane_config(struct intel_crtc *crtc,
6077 struct intel_plane_config *plane_config)
6079 struct drm_device *dev = crtc->base.dev;
6080 struct drm_i915_private *dev_priv = dev->dev_private;
6081 u32 val, base, offset;
6082 int pipe = crtc->pipe, plane = crtc->plane;
6083 int fourcc, pixel_format;
6086 crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
6087 if (!crtc->base.primary->fb) {
6088 DRM_DEBUG_KMS("failed to alloc fb\n");
6092 val = I915_READ(DSPCNTR(plane));
6094 if (INTEL_INFO(dev)->gen >= 4)
6095 if (val & DISPPLANE_TILED)
6096 plane_config->tiled = true;
6098 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
6099 fourcc = intel_format_to_fourcc(pixel_format);
6100 crtc->base.primary->fb->pixel_format = fourcc;
6101 crtc->base.primary->fb->bits_per_pixel =
6102 drm_format_plane_cpp(fourcc, 0) * 8;
6104 if (INTEL_INFO(dev)->gen >= 4) {
6105 if (plane_config->tiled)
6106 offset = I915_READ(DSPTILEOFF(plane));
6108 offset = I915_READ(DSPLINOFF(plane));
6109 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
6111 base = I915_READ(DSPADDR(plane));
6113 plane_config->base = base;
6115 val = I915_READ(PIPESRC(pipe));
6116 crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
6117 crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
6119 val = I915_READ(DSPSTRIDE(pipe));
6120 crtc->base.primary->fb->pitches[0] = val & 0xffffff80;
6122 aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
6123 plane_config->tiled);
6125 plane_config->size = ALIGN(crtc->base.primary->fb->pitches[0] *
6126 aligned_height, PAGE_SIZE);
6128 DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
6129 pipe, plane, crtc->base.primary->fb->width,
6130 crtc->base.primary->fb->height,
6131 crtc->base.primary->fb->bits_per_pixel, base,
6132 crtc->base.primary->fb->pitches[0],
6133 plane_config->size);
6137 static void chv_crtc_clock_get(struct intel_crtc *crtc,
6138 struct intel_crtc_config *pipe_config)
6140 struct drm_device *dev = crtc->base.dev;
6141 struct drm_i915_private *dev_priv = dev->dev_private;
6142 int pipe = pipe_config->cpu_transcoder;
6143 enum dpio_channel port = vlv_pipe_to_channel(pipe);
6144 intel_clock_t clock;
6145 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
6146 int refclk = 100000;
6148 mutex_lock(&dev_priv->dpio_lock);
6149 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
6150 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
6151 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
6152 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
6153 mutex_unlock(&dev_priv->dpio_lock);
6155 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
6156 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
6157 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
6158 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
6159 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
6161 chv_clock(refclk, &clock);
6163 /* clock.dot is the fast clock */
6164 pipe_config->port_clock = clock.dot / 5;
6167 static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
6168 struct intel_crtc_config *pipe_config)
6170 struct drm_device *dev = crtc->base.dev;
6171 struct drm_i915_private *dev_priv = dev->dev_private;
6174 if (!intel_display_power_enabled(dev_priv,
6175 POWER_DOMAIN_PIPE(crtc->pipe)))
6178 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
6179 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
6181 tmp = I915_READ(PIPECONF(crtc->pipe));
6182 if (!(tmp & PIPECONF_ENABLE))
6185 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
6186 switch (tmp & PIPECONF_BPC_MASK) {
6188 pipe_config->pipe_bpp = 18;
6191 pipe_config->pipe_bpp = 24;
6193 case PIPECONF_10BPC:
6194 pipe_config->pipe_bpp = 30;
6201 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
6202 pipe_config->limited_color_range = true;
6204 if (INTEL_INFO(dev)->gen < 4)
6205 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
6207 intel_get_pipe_timings(crtc, pipe_config);
6209 i9xx_get_pfit_config(crtc, pipe_config);
6211 if (INTEL_INFO(dev)->gen >= 4) {
6212 tmp = I915_READ(DPLL_MD(crtc->pipe));
6213 pipe_config->pixel_multiplier =
6214 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
6215 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
6216 pipe_config->dpll_hw_state.dpll_md = tmp;
6217 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
6218 tmp = I915_READ(DPLL(crtc->pipe));
6219 pipe_config->pixel_multiplier =
6220 ((tmp & SDVO_MULTIPLIER_MASK)
6221 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
6223 /* Note that on i915G/GM the pixel multiplier is in the sdvo
6224 * port and will be fixed up in the encoder->get_config
6226 pipe_config->pixel_multiplier = 1;
6228 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
6229 if (!IS_VALLEYVIEW(dev)) {
6230 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
6231 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
6233 /* Mask out read-only status bits. */
6234 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
6235 DPLL_PORTC_READY_MASK |
6236 DPLL_PORTB_READY_MASK);
6239 if (IS_CHERRYVIEW(dev))
6240 chv_crtc_clock_get(crtc, pipe_config);
6241 else if (IS_VALLEYVIEW(dev))
6242 vlv_crtc_clock_get(crtc, pipe_config);
6244 i9xx_crtc_clock_get(crtc, pipe_config);
6249 static void ironlake_init_pch_refclk(struct drm_device *dev)
6251 struct drm_i915_private *dev_priv = dev->dev_private;
6252 struct drm_mode_config *mode_config = &dev->mode_config;
6253 struct intel_encoder *encoder;
6255 bool has_lvds = false;
6256 bool has_cpu_edp = false;
6257 bool has_panel = false;
6258 bool has_ck505 = false;
6259 bool can_ssc = false;
6261 /* We need to take the global config into account */
6262 list_for_each_entry(encoder, &mode_config->encoder_list,
6264 switch (encoder->type) {
6265 case INTEL_OUTPUT_LVDS:
6269 case INTEL_OUTPUT_EDP:
6271 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
6277 if (HAS_PCH_IBX(dev)) {
6278 has_ck505 = dev_priv->vbt.display_clock_mode;
6279 can_ssc = has_ck505;
6285 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
6286 has_panel, has_lvds, has_ck505);
6288 /* Ironlake: try to setup display ref clock before DPLL
6289 * enabling. This is only under driver's control after
6290 * PCH B stepping, previous chipset stepping should be
6291 * ignoring this setting.
6293 val = I915_READ(PCH_DREF_CONTROL);
6295 /* As we must carefully and slowly disable/enable each source in turn,
6296 * compute the final state we want first and check if we need to
6297 * make any changes at all.
6300 final &= ~DREF_NONSPREAD_SOURCE_MASK;
6302 final |= DREF_NONSPREAD_CK505_ENABLE;
6304 final |= DREF_NONSPREAD_SOURCE_ENABLE;
6306 final &= ~DREF_SSC_SOURCE_MASK;
6307 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
6308 final &= ~DREF_SSC1_ENABLE;
6311 final |= DREF_SSC_SOURCE_ENABLE;
6313 if (intel_panel_use_ssc(dev_priv) && can_ssc)
6314 final |= DREF_SSC1_ENABLE;
6317 if (intel_panel_use_ssc(dev_priv) && can_ssc)
6318 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
6320 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
6322 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
6324 final |= DREF_SSC_SOURCE_DISABLE;
6325 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
6331 /* Always enable nonspread source */
6332 val &= ~DREF_NONSPREAD_SOURCE_MASK;
6335 val |= DREF_NONSPREAD_CK505_ENABLE;
6337 val |= DREF_NONSPREAD_SOURCE_ENABLE;
6340 val &= ~DREF_SSC_SOURCE_MASK;
6341 val |= DREF_SSC_SOURCE_ENABLE;
6343 /* SSC must be turned on before enabling the CPU output */
6344 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
6345 DRM_DEBUG_KMS("Using SSC on panel\n");
6346 val |= DREF_SSC1_ENABLE;
6348 val &= ~DREF_SSC1_ENABLE;
6350 /* Get SSC going before enabling the outputs */
6351 I915_WRITE(PCH_DREF_CONTROL, val);
6352 POSTING_READ(PCH_DREF_CONTROL);
6355 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
6357 /* Enable CPU source on CPU attached eDP */
6359 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
6360 DRM_DEBUG_KMS("Using SSC on eDP\n");
6361 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
6363 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
6365 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
6367 I915_WRITE(PCH_DREF_CONTROL, val);
6368 POSTING_READ(PCH_DREF_CONTROL);
6371 DRM_DEBUG_KMS("Disabling SSC entirely\n");
6373 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
6375 /* Turn off CPU output */
6376 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
6378 I915_WRITE(PCH_DREF_CONTROL, val);
6379 POSTING_READ(PCH_DREF_CONTROL);
6382 /* Turn off the SSC source */
6383 val &= ~DREF_SSC_SOURCE_MASK;
6384 val |= DREF_SSC_SOURCE_DISABLE;
6387 val &= ~DREF_SSC1_ENABLE;
6389 I915_WRITE(PCH_DREF_CONTROL, val);
6390 POSTING_READ(PCH_DREF_CONTROL);
6394 BUG_ON(val != final);
6397 static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
6401 tmp = I915_READ(SOUTH_CHICKEN2);
6402 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
6403 I915_WRITE(SOUTH_CHICKEN2, tmp);
6405 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
6406 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
6407 DRM_ERROR("FDI mPHY reset assert timeout\n");
6409 tmp = I915_READ(SOUTH_CHICKEN2);
6410 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
6411 I915_WRITE(SOUTH_CHICKEN2, tmp);
6413 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
6414 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
6415 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
6418 /* WaMPhyProgramming:hsw */
6419 static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
6423 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
6424 tmp &= ~(0xFF << 24);
6425 tmp |= (0x12 << 24);
6426 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
6428 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
6430 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
6432 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
6434 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
6436 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
6437 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
6438 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
6440 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
6441 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
6442 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
6444 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
6447 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
6449 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
6452 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
6454 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
6457 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
6459 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
6462 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
6464 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
6465 tmp &= ~(0xFF << 16);
6466 tmp |= (0x1C << 16);
6467 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
6469 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
6470 tmp &= ~(0xFF << 16);
6471 tmp |= (0x1C << 16);
6472 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
6474 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
6476 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
6478 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
6480 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
6482 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
6483 tmp &= ~(0xF << 28);
6485 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
6487 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
6488 tmp &= ~(0xF << 28);
6490 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
6493 /* Implements 3 different sequences from BSpec chapter "Display iCLK
6494 * Programming" based on the parameters passed:
6495 * - Sequence to enable CLKOUT_DP
6496 * - Sequence to enable CLKOUT_DP without spread
6497 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
6499 static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
6502 struct drm_i915_private *dev_priv = dev->dev_private;
6505 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
6507 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
6508 with_fdi, "LP PCH doesn't have FDI\n"))
6511 mutex_lock(&dev_priv->dpio_lock);
6513 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
6514 tmp &= ~SBI_SSCCTL_DISABLE;
6515 tmp |= SBI_SSCCTL_PATHALT;
6516 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6521 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
6522 tmp &= ~SBI_SSCCTL_PATHALT;
6523 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6526 lpt_reset_fdi_mphy(dev_priv);
6527 lpt_program_fdi_mphy(dev_priv);
6531 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
6532 SBI_GEN0 : SBI_DBUFF0;
6533 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
6534 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
6535 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
6537 mutex_unlock(&dev_priv->dpio_lock);
6540 /* Sequence to disable CLKOUT_DP */
6541 static void lpt_disable_clkout_dp(struct drm_device *dev)
6543 struct drm_i915_private *dev_priv = dev->dev_private;
6546 mutex_lock(&dev_priv->dpio_lock);
6548 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
6549 SBI_GEN0 : SBI_DBUFF0;
6550 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
6551 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
6552 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
6554 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
6555 if (!(tmp & SBI_SSCCTL_DISABLE)) {
6556 if (!(tmp & SBI_SSCCTL_PATHALT)) {
6557 tmp |= SBI_SSCCTL_PATHALT;
6558 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6561 tmp |= SBI_SSCCTL_DISABLE;
6562 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6565 mutex_unlock(&dev_priv->dpio_lock);
6568 static void lpt_init_pch_refclk(struct drm_device *dev)
6570 struct drm_mode_config *mode_config = &dev->mode_config;
6571 struct intel_encoder *encoder;
6572 bool has_vga = false;
6574 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
6575 switch (encoder->type) {
6576 case INTEL_OUTPUT_ANALOG:
6583 lpt_enable_clkout_dp(dev, true, true);
6585 lpt_disable_clkout_dp(dev);
6589 * Initialize reference clocks when the driver loads
6591 void intel_init_pch_refclk(struct drm_device *dev)
6593 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
6594 ironlake_init_pch_refclk(dev);
6595 else if (HAS_PCH_LPT(dev))
6596 lpt_init_pch_refclk(dev);
6599 static int ironlake_get_refclk(struct drm_crtc *crtc)
6601 struct drm_device *dev = crtc->dev;
6602 struct drm_i915_private *dev_priv = dev->dev_private;
6603 struct intel_encoder *encoder;
6604 int num_connectors = 0;
6605 bool is_lvds = false;
6607 for_each_encoder_on_crtc(dev, crtc, encoder) {
6608 switch (encoder->type) {
6609 case INTEL_OUTPUT_LVDS:
6616 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
6617 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
6618 dev_priv->vbt.lvds_ssc_freq);
6619 return dev_priv->vbt.lvds_ssc_freq;
6625 static void ironlake_set_pipeconf(struct drm_crtc *crtc)
6627 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
6628 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6629 int pipe = intel_crtc->pipe;
6634 switch (intel_crtc->config.pipe_bpp) {
6636 val |= PIPECONF_6BPC;
6639 val |= PIPECONF_8BPC;
6642 val |= PIPECONF_10BPC;
6645 val |= PIPECONF_12BPC;
6648 /* Case prevented by intel_choose_pipe_bpp_dither. */
6652 if (intel_crtc->config.dither)
6653 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
6655 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
6656 val |= PIPECONF_INTERLACED_ILK;
6658 val |= PIPECONF_PROGRESSIVE;
6660 if (intel_crtc->config.limited_color_range)
6661 val |= PIPECONF_COLOR_RANGE_SELECT;
6663 I915_WRITE(PIPECONF(pipe), val);
6664 POSTING_READ(PIPECONF(pipe));
6668 * Set up the pipe CSC unit.
6670 * Currently only full range RGB to limited range RGB conversion
6671 * is supported, but eventually this should handle various
6672 * RGB<->YCbCr scenarios as well.
6674 static void intel_set_pipe_csc(struct drm_crtc *crtc)
6676 struct drm_device *dev = crtc->dev;
6677 struct drm_i915_private *dev_priv = dev->dev_private;
6678 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6679 int pipe = intel_crtc->pipe;
6680 uint16_t coeff = 0x7800; /* 1.0 */
6683 * TODO: Check what kind of values actually come out of the pipe
6684 * with these coeff/postoff values and adjust to get the best
6685 * accuracy. Perhaps we even need to take the bpc value into
6689 if (intel_crtc->config.limited_color_range)
6690 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
6693 * GY/GU and RY/RU should be the other way around according
6694 * to BSpec, but reality doesn't agree. Just set them up in
6695 * a way that results in the correct picture.
6697 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
6698 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
6700 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
6701 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
6703 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
6704 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
6706 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
6707 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
6708 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
6710 if (INTEL_INFO(dev)->gen > 6) {
6711 uint16_t postoff = 0;
6713 if (intel_crtc->config.limited_color_range)
6714 postoff = (16 * (1 << 12) / 255) & 0x1fff;
6716 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
6717 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
6718 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
6720 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
6722 uint32_t mode = CSC_MODE_YUV_TO_RGB;
6724 if (intel_crtc->config.limited_color_range)
6725 mode |= CSC_BLACK_SCREEN_OFFSET;
6727 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
6731 static void haswell_set_pipeconf(struct drm_crtc *crtc)
6733 struct drm_device *dev = crtc->dev;
6734 struct drm_i915_private *dev_priv = dev->dev_private;
6735 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6736 enum i915_pipe pipe = intel_crtc->pipe;
6737 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
6742 if (IS_HASWELL(dev) && intel_crtc->config.dither)
6743 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
6745 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
6746 val |= PIPECONF_INTERLACED_ILK;
6748 val |= PIPECONF_PROGRESSIVE;
6750 I915_WRITE(PIPECONF(cpu_transcoder), val);
6751 POSTING_READ(PIPECONF(cpu_transcoder));
6753 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
6754 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
6756 if (IS_BROADWELL(dev)) {
6759 switch (intel_crtc->config.pipe_bpp) {
6761 val |= PIPEMISC_DITHER_6_BPC;
6764 val |= PIPEMISC_DITHER_8_BPC;
6767 val |= PIPEMISC_DITHER_10_BPC;
6770 val |= PIPEMISC_DITHER_12_BPC;
6773 /* Case prevented by pipe_config_set_bpp. */
6777 if (intel_crtc->config.dither)
6778 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
6780 I915_WRITE(PIPEMISC(pipe), val);
6784 static bool ironlake_compute_clocks(struct drm_crtc *crtc,
6785 intel_clock_t *clock,
6786 bool *has_reduced_clock,
6787 intel_clock_t *reduced_clock)
6789 struct drm_device *dev = crtc->dev;
6790 struct drm_i915_private *dev_priv = dev->dev_private;
6791 struct intel_encoder *intel_encoder;
6793 const intel_limit_t *limit;
6794 bool ret, is_lvds = false;
6796 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
6797 switch (intel_encoder->type) {
6798 case INTEL_OUTPUT_LVDS:
6804 refclk = ironlake_get_refclk(crtc);
6807 * Returns a set of divisors for the desired target clock with the given
6808 * refclk, or FALSE. The returned values represent the clock equation:
6809 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
6811 limit = intel_limit(crtc, refclk);
6812 ret = dev_priv->display.find_dpll(limit, crtc,
6813 to_intel_crtc(crtc)->config.port_clock,
6814 refclk, NULL, clock);
6818 if (is_lvds && dev_priv->lvds_downclock_avail) {
6820 * Ensure we match the reduced clock's P to the target clock.
6821 * If the clocks don't match, we can't switch the display clock
6822 * by using the FP0/FP1. In such case we will disable the LVDS
6823 * downclock feature.
6825 *has_reduced_clock =
6826 dev_priv->display.find_dpll(limit, crtc,
6827 dev_priv->lvds_downclock,
6835 int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
6838 * Account for spread spectrum to avoid
6839 * oversubscribing the link. Max center spread
6840 * is 2.5%; use 5% for safety's sake.
6842 u32 bps = target_clock * bpp * 21 / 20;
6843 return DIV_ROUND_UP(bps, link_bw * 8);
6846 static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
6848 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
6851 static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
6853 intel_clock_t *reduced_clock, u32 *fp2)
6855 struct drm_crtc *crtc = &intel_crtc->base;
6856 struct drm_device *dev = crtc->dev;
6857 struct drm_i915_private *dev_priv = dev->dev_private;
6858 struct intel_encoder *intel_encoder;
6860 int factor, num_connectors = 0;
6861 bool is_lvds = false, is_sdvo = false;
6863 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
6864 switch (intel_encoder->type) {
6865 case INTEL_OUTPUT_LVDS:
6868 case INTEL_OUTPUT_SDVO:
6869 case INTEL_OUTPUT_HDMI:
6877 /* Enable autotuning of the PLL clock (if permissible) */
6880 if ((intel_panel_use_ssc(dev_priv) &&
6881 dev_priv->vbt.lvds_ssc_freq == 100000) ||
6882 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
6884 } else if (intel_crtc->config.sdvo_tv_clock)
6887 if (ironlake_needs_fb_cb_tune(&intel_crtc->config.dpll, factor))
6890 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
6896 dpll |= DPLLB_MODE_LVDS;
6898 dpll |= DPLLB_MODE_DAC_SERIAL;
6900 dpll |= (intel_crtc->config.pixel_multiplier - 1)
6901 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
6904 dpll |= DPLL_SDVO_HIGH_SPEED;
6905 if (intel_crtc->config.has_dp_encoder)
6906 dpll |= DPLL_SDVO_HIGH_SPEED;
6908 /* compute bitmask from p1 value */
6909 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6911 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
6913 switch (intel_crtc->config.dpll.p2) {
6915 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
6918 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
6921 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
6924 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
6928 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
6929 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
6931 dpll |= PLL_REF_INPUT_DREFCLK;
6933 return dpll | DPLL_VCO_ENABLE;
6936 static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
6938 struct drm_framebuffer *fb)
6940 struct drm_device *dev = crtc->dev;
6941 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6942 int num_connectors = 0;
6943 intel_clock_t clock, reduced_clock;
6944 u32 dpll = 0, fp = 0, fp2 = 0;
6945 bool ok, has_reduced_clock = false;
6946 bool is_lvds = false;
6947 struct intel_encoder *encoder;
6948 struct intel_shared_dpll *pll;
6950 for_each_encoder_on_crtc(dev, crtc, encoder) {
6951 switch (encoder->type) {
6952 case INTEL_OUTPUT_LVDS:
6960 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
6961 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
6963 ok = ironlake_compute_clocks(crtc, &clock,
6964 &has_reduced_clock, &reduced_clock);
6965 if (!ok && !intel_crtc->config.clock_set) {
6966 DRM_ERROR("Couldn't find PLL settings for mode!\n");
6969 /* Compat-code for transition, will disappear. */
6970 if (!intel_crtc->config.clock_set) {
6971 intel_crtc->config.dpll.n = clock.n;
6972 intel_crtc->config.dpll.m1 = clock.m1;
6973 intel_crtc->config.dpll.m2 = clock.m2;
6974 intel_crtc->config.dpll.p1 = clock.p1;
6975 intel_crtc->config.dpll.p2 = clock.p2;
6978 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
6979 if (intel_crtc->config.has_pch_encoder) {
6980 fp = i9xx_dpll_compute_fp(&intel_crtc->config.dpll);
6981 if (has_reduced_clock)
6982 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
6984 dpll = ironlake_compute_dpll(intel_crtc,
6985 &fp, &reduced_clock,
6986 has_reduced_clock ? &fp2 : NULL);
6988 intel_crtc->config.dpll_hw_state.dpll = dpll;
6989 intel_crtc->config.dpll_hw_state.fp0 = fp;
6990 if (has_reduced_clock)
6991 intel_crtc->config.dpll_hw_state.fp1 = fp2;
6993 intel_crtc->config.dpll_hw_state.fp1 = fp;
6995 pll = intel_get_shared_dpll(intel_crtc);
6997 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
6998 pipe_name(intel_crtc->pipe));
7002 intel_put_shared_dpll(intel_crtc);
7004 if (is_lvds && has_reduced_clock && i915.powersave)
7005 intel_crtc->lowfreq_avail = true;
7007 intel_crtc->lowfreq_avail = false;
7012 static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
7013 struct intel_link_m_n *m_n)
7015 struct drm_device *dev = crtc->base.dev;
7016 struct drm_i915_private *dev_priv = dev->dev_private;
7017 enum i915_pipe pipe = crtc->pipe;
7019 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
7020 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
7021 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
7023 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
7024 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
7025 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7028 static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
7029 enum transcoder transcoder,
7030 struct intel_link_m_n *m_n)
7032 struct drm_device *dev = crtc->base.dev;
7033 struct drm_i915_private *dev_priv = dev->dev_private;
7034 enum i915_pipe pipe = crtc->pipe;
7036 if (INTEL_INFO(dev)->gen >= 5) {
7037 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
7038 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
7039 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
7041 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
7042 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
7043 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7045 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
7046 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
7047 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
7049 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
7050 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
7051 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7055 void intel_dp_get_m_n(struct intel_crtc *crtc,
7056 struct intel_crtc_config *pipe_config)
7058 if (crtc->config.has_pch_encoder)
7059 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
7061 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
7062 &pipe_config->dp_m_n);
7065 static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
7066 struct intel_crtc_config *pipe_config)
7068 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
7069 &pipe_config->fdi_m_n);
7072 static void ironlake_get_pfit_config(struct intel_crtc *crtc,
7073 struct intel_crtc_config *pipe_config)
7075 struct drm_device *dev = crtc->base.dev;
7076 struct drm_i915_private *dev_priv = dev->dev_private;
7079 tmp = I915_READ(PF_CTL(crtc->pipe));
7081 if (tmp & PF_ENABLE) {
7082 pipe_config->pch_pfit.enabled = true;
7083 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
7084 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
7086 /* We currently do not free assignements of panel fitters on
7087 * ivb/hsw (since we don't use the higher upscaling modes which
7088 * differentiates them) so just WARN about this case for now. */
7090 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
7091 PF_PIPE_SEL_IVB(crtc->pipe));
7096 static void ironlake_get_plane_config(struct intel_crtc *crtc,
7097 struct intel_plane_config *plane_config)
7099 struct drm_device *dev = crtc->base.dev;
7100 struct drm_i915_private *dev_priv = dev->dev_private;
7101 u32 val, base, offset;
7102 int pipe = crtc->pipe, plane = crtc->plane;
7103 int fourcc, pixel_format;
7106 crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
7107 if (!crtc->base.primary->fb) {
7108 DRM_DEBUG_KMS("failed to alloc fb\n");
7112 val = I915_READ(DSPCNTR(plane));
7114 if (INTEL_INFO(dev)->gen >= 4)
7115 if (val & DISPPLANE_TILED)
7116 plane_config->tiled = true;
7118 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
7119 fourcc = intel_format_to_fourcc(pixel_format);
7120 crtc->base.primary->fb->pixel_format = fourcc;
7121 crtc->base.primary->fb->bits_per_pixel =
7122 drm_format_plane_cpp(fourcc, 0) * 8;
7124 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
7125 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
7126 offset = I915_READ(DSPOFFSET(plane));
7128 if (plane_config->tiled)
7129 offset = I915_READ(DSPTILEOFF(plane));
7131 offset = I915_READ(DSPLINOFF(plane));
7133 plane_config->base = base;
7135 val = I915_READ(PIPESRC(pipe));
7136 crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
7137 crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
7139 val = I915_READ(DSPSTRIDE(pipe));
7140 crtc->base.primary->fb->pitches[0] = val & 0xffffff80;
7142 aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
7143 plane_config->tiled);
7145 plane_config->size = ALIGN(crtc->base.primary->fb->pitches[0] *
7146 aligned_height, PAGE_SIZE);
7148 DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
7149 pipe, plane, crtc->base.primary->fb->width,
7150 crtc->base.primary->fb->height,
7151 crtc->base.primary->fb->bits_per_pixel, base,
7152 crtc->base.primary->fb->pitches[0],
7153 plane_config->size);
7156 static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
7157 struct intel_crtc_config *pipe_config)
7159 struct drm_device *dev = crtc->base.dev;
7160 struct drm_i915_private *dev_priv = dev->dev_private;
7163 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
7164 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
7166 tmp = I915_READ(PIPECONF(crtc->pipe));
7167 if (!(tmp & PIPECONF_ENABLE))
7170 switch (tmp & PIPECONF_BPC_MASK) {
7172 pipe_config->pipe_bpp = 18;
7175 pipe_config->pipe_bpp = 24;
7177 case PIPECONF_10BPC:
7178 pipe_config->pipe_bpp = 30;
7180 case PIPECONF_12BPC:
7181 pipe_config->pipe_bpp = 36;
7187 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
7188 pipe_config->limited_color_range = true;
7190 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
7191 struct intel_shared_dpll *pll;
7193 pipe_config->has_pch_encoder = true;
7195 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
7196 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
7197 FDI_DP_PORT_WIDTH_SHIFT) + 1;
7199 ironlake_get_fdi_m_n_config(crtc, pipe_config);
7201 if (HAS_PCH_IBX(dev_priv->dev)) {
7202 pipe_config->shared_dpll =
7203 (enum intel_dpll_id) crtc->pipe;
7205 tmp = I915_READ(PCH_DPLL_SEL);
7206 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
7207 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
7209 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
7212 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
7214 WARN_ON(!pll->get_hw_state(dev_priv, pll,
7215 &pipe_config->dpll_hw_state));
7217 tmp = pipe_config->dpll_hw_state.dpll;
7218 pipe_config->pixel_multiplier =
7219 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
7220 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
7222 ironlake_pch_clock_get(crtc, pipe_config);
7224 pipe_config->pixel_multiplier = 1;
7227 intel_get_pipe_timings(crtc, pipe_config);
7229 ironlake_get_pfit_config(crtc, pipe_config);
7234 static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
7236 struct drm_device *dev = dev_priv->dev;
7237 struct intel_ddi_plls *plls = &dev_priv->ddi_plls;
7238 struct intel_crtc *crtc;
7240 for_each_intel_crtc(dev, crtc)
7241 WARN(crtc->active, "CRTC for pipe %c enabled\n",
7242 pipe_name(crtc->pipe));
7244 WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
7245 WARN(plls->spll_refcount, "SPLL enabled\n");
7246 WARN(plls->wrpll1_refcount, "WRPLL1 enabled\n");
7247 WARN(plls->wrpll2_refcount, "WRPLL2 enabled\n");
7248 WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
7249 WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
7250 "CPU PWM1 enabled\n");
7251 WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
7252 "CPU PWM2 enabled\n");
7253 WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
7254 "PCH PWM1 enabled\n");
7255 WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
7256 "Utility pin enabled\n");
7257 WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
7260 * In theory we can still leave IRQs enabled, as long as only the HPD
7261 * interrupts remain enabled. We used to check for that, but since it's
7262 * gen-specific and since we only disable LCPLL after we fully disable
7263 * the interrupts, the check below should be enough.
7265 WARN(!dev_priv->pm.irqs_disabled, "IRQs enabled\n");
7268 static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
7270 struct drm_device *dev = dev_priv->dev;
7272 if (IS_HASWELL(dev)) {
7273 mutex_lock(&dev_priv->rps.hw_lock);
7274 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
7276 DRM_ERROR("Failed to disable D_COMP\n");
7277 mutex_unlock(&dev_priv->rps.hw_lock);
7279 I915_WRITE(D_COMP, val);
7281 POSTING_READ(D_COMP);
7285 * This function implements pieces of two sequences from BSpec:
7286 * - Sequence for display software to disable LCPLL
7287 * - Sequence for display software to allow package C8+
7288 * The steps implemented here are just the steps that actually touch the LCPLL
7289 * register. Callers should take care of disabling all the display engine
7290 * functions, doing the mode unset, fixing interrupts, etc.
7292 static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
7293 bool switch_to_fclk, bool allow_power_down)
7297 assert_can_disable_lcpll(dev_priv);
7299 val = I915_READ(LCPLL_CTL);
7301 if (switch_to_fclk) {
7302 val |= LCPLL_CD_SOURCE_FCLK;
7303 I915_WRITE(LCPLL_CTL, val);
7305 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
7306 LCPLL_CD_SOURCE_FCLK_DONE, 1))
7307 DRM_ERROR("Switching to FCLK failed\n");
7309 val = I915_READ(LCPLL_CTL);
7312 val |= LCPLL_PLL_DISABLE;
7313 I915_WRITE(LCPLL_CTL, val);
7314 POSTING_READ(LCPLL_CTL);
7316 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
7317 DRM_ERROR("LCPLL still locked\n");
7319 val = I915_READ(D_COMP);
7320 val |= D_COMP_COMP_DISABLE;
7321 hsw_write_dcomp(dev_priv, val);
7324 if (wait_for((I915_READ(D_COMP) & D_COMP_RCOMP_IN_PROGRESS) == 0, 1))
7325 DRM_ERROR("D_COMP RCOMP still in progress\n");
7327 if (allow_power_down) {
7328 val = I915_READ(LCPLL_CTL);
7329 val |= LCPLL_POWER_DOWN_ALLOW;
7330 I915_WRITE(LCPLL_CTL, val);
7331 POSTING_READ(LCPLL_CTL);
7336 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
7339 static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
7343 val = I915_READ(LCPLL_CTL);
7345 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
7346 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
7350 * Make sure we're not on PC8 state before disabling PC8, otherwise
7351 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
7353 * The other problem is that hsw_restore_lcpll() is called as part of
7354 * the runtime PM resume sequence, so we can't just call
7355 * gen6_gt_force_wake_get() because that function calls
7356 * intel_runtime_pm_get(), and we can't change the runtime PM refcount
7357 * while we are on the resume sequence. So to solve this problem we have
7358 * to call special forcewake code that doesn't touch runtime PM and
7359 * doesn't enable the forcewake delayed work.
7361 lockmgr(&dev_priv->uncore.lock, LK_EXCLUSIVE);
7362 if (dev_priv->uncore.forcewake_count++ == 0)
7363 dev_priv->uncore.funcs.force_wake_get(dev_priv, FORCEWAKE_ALL);
7364 lockmgr(&dev_priv->uncore.lock, LK_RELEASE);
7366 if (val & LCPLL_POWER_DOWN_ALLOW) {
7367 val &= ~LCPLL_POWER_DOWN_ALLOW;
7368 I915_WRITE(LCPLL_CTL, val);
7369 POSTING_READ(LCPLL_CTL);
7372 val = I915_READ(D_COMP);
7373 val |= D_COMP_COMP_FORCE;
7374 val &= ~D_COMP_COMP_DISABLE;
7375 hsw_write_dcomp(dev_priv, val);
7377 val = I915_READ(LCPLL_CTL);
7378 val &= ~LCPLL_PLL_DISABLE;
7379 I915_WRITE(LCPLL_CTL, val);
7381 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
7382 DRM_ERROR("LCPLL not locked yet\n");
7384 if (val & LCPLL_CD_SOURCE_FCLK) {
7385 val = I915_READ(LCPLL_CTL);
7386 val &= ~LCPLL_CD_SOURCE_FCLK;
7387 I915_WRITE(LCPLL_CTL, val);
7389 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
7390 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
7391 DRM_ERROR("Switching back to LCPLL failed\n");
7394 /* See the big comment above. */
7395 lockmgr(&dev_priv->uncore.lock, LK_EXCLUSIVE);
7396 if (--dev_priv->uncore.forcewake_count == 0)
7397 dev_priv->uncore.funcs.force_wake_put(dev_priv, FORCEWAKE_ALL);
7398 lockmgr(&dev_priv->uncore.lock, LK_RELEASE);
7402 * Package states C8 and deeper are really deep PC states that can only be
7403 * reached when all the devices on the system allow it, so even if the graphics
7404 * device allows PC8+, it doesn't mean the system will actually get to these
7405 * states. Our driver only allows PC8+ when going into runtime PM.
7407 * The requirements for PC8+ are that all the outputs are disabled, the power
7408 * well is disabled and most interrupts are disabled, and these are also
7409 * requirements for runtime PM. When these conditions are met, we manually do
7410 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
7411 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
7414 * When we really reach PC8 or deeper states (not just when we allow it) we lose
7415 * the state of some registers, so when we come back from PC8+ we need to
7416 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
7417 * need to take care of the registers kept by RC6. Notice that this happens even
7418 * if we don't put the device in PCI D3 state (which is what currently happens
7419 * because of the runtime PM support).
7421 * For more, read "Display Sequences for Package C8" on the hardware
7424 void hsw_enable_pc8(struct drm_i915_private *dev_priv)
7426 struct drm_device *dev = dev_priv->dev;
7429 DRM_DEBUG_KMS("Enabling package C8+\n");
7431 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
7432 val = I915_READ(SOUTH_DSPCLK_GATE_D);
7433 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
7434 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
7437 lpt_disable_clkout_dp(dev);
7438 hsw_disable_lcpll(dev_priv, true, true);
7441 void hsw_disable_pc8(struct drm_i915_private *dev_priv)
7443 struct drm_device *dev = dev_priv->dev;
7446 DRM_DEBUG_KMS("Disabling package C8+\n");
7448 hsw_restore_lcpll(dev_priv);
7449 lpt_init_pch_refclk(dev);
7451 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
7452 val = I915_READ(SOUTH_DSPCLK_GATE_D);
7453 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
7454 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
7457 intel_prepare_ddi(dev);
7460 static void snb_modeset_global_resources(struct drm_device *dev)
7462 modeset_update_crtc_power_domains(dev);
7465 static void haswell_modeset_global_resources(struct drm_device *dev)
7467 modeset_update_crtc_power_domains(dev);
7470 static int haswell_crtc_mode_set(struct drm_crtc *crtc,
7472 struct drm_framebuffer *fb)
7474 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7476 if (!intel_ddi_pll_select(intel_crtc))
7478 intel_ddi_pll_enable(intel_crtc);
7480 intel_crtc->lowfreq_avail = false;
7485 static bool haswell_get_pipe_config(struct intel_crtc *crtc,
7486 struct intel_crtc_config *pipe_config)
7488 struct drm_device *dev = crtc->base.dev;
7489 struct drm_i915_private *dev_priv = dev->dev_private;
7490 enum intel_display_power_domain pfit_domain;
7493 if (!intel_display_power_enabled(dev_priv,
7494 POWER_DOMAIN_PIPE(crtc->pipe)))
7497 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
7498 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
7500 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
7501 if (tmp & TRANS_DDI_FUNC_ENABLE) {
7502 enum i915_pipe trans_edp_pipe;
7503 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
7505 WARN(1, "unknown pipe linked to edp transcoder\n");
7506 case TRANS_DDI_EDP_INPUT_A_ONOFF:
7507 case TRANS_DDI_EDP_INPUT_A_ON:
7508 trans_edp_pipe = PIPE_A;
7510 case TRANS_DDI_EDP_INPUT_B_ONOFF:
7511 trans_edp_pipe = PIPE_B;
7513 case TRANS_DDI_EDP_INPUT_C_ONOFF:
7514 trans_edp_pipe = PIPE_C;
7518 if (trans_edp_pipe == crtc->pipe)
7519 pipe_config->cpu_transcoder = TRANSCODER_EDP;
7522 if (!intel_display_power_enabled(dev_priv,
7523 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
7526 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
7527 if (!(tmp & PIPECONF_ENABLE))
7531 * Haswell has only FDI/PCH transcoder A. It is which is connected to
7532 * DDI E. So just check whether this pipe is wired to DDI E and whether
7533 * the PCH transcoder is on.
7535 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
7536 if ((tmp & TRANS_DDI_PORT_MASK) == TRANS_DDI_SELECT_PORT(PORT_E) &&
7537 I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
7538 pipe_config->has_pch_encoder = true;
7540 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
7541 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
7542 FDI_DP_PORT_WIDTH_SHIFT) + 1;
7544 ironlake_get_fdi_m_n_config(crtc, pipe_config);
7547 intel_get_pipe_timings(crtc, pipe_config);
7549 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
7550 if (intel_display_power_enabled(dev_priv, pfit_domain))
7551 ironlake_get_pfit_config(crtc, pipe_config);
7553 if (IS_HASWELL(dev))
7554 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
7555 (I915_READ(IPS_CTL) & IPS_ENABLE);
7557 pipe_config->pixel_multiplier = 1;
7565 } hdmi_audio_clock[] = {
7566 { DIV_ROUND_UP(25200 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_25175 },
7567 { 25200, AUD_CONFIG_PIXEL_CLOCK_HDMI_25200 }, /* default per bspec */
7568 { 27000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27000 },
7569 { 27000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27027 },
7570 { 54000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54000 },
7571 { 54000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54054 },
7572 { DIV_ROUND_UP(74250 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_74176 },
7573 { 74250, AUD_CONFIG_PIXEL_CLOCK_HDMI_74250 },
7574 { DIV_ROUND_UP(148500 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_148352 },
7575 { 148500, AUD_CONFIG_PIXEL_CLOCK_HDMI_148500 },
7578 /* get AUD_CONFIG_PIXEL_CLOCK_HDMI_* value for mode */
7579 static u32 audio_config_hdmi_pixel_clock(struct drm_display_mode *mode)
7583 for (i = 0; i < ARRAY_SIZE(hdmi_audio_clock); i++) {
7584 if (mode->clock == hdmi_audio_clock[i].clock)
7588 if (i == ARRAY_SIZE(hdmi_audio_clock)) {
7589 DRM_DEBUG_KMS("HDMI audio pixel clock setting for %d not found, falling back to defaults\n", mode->clock);
7593 DRM_DEBUG_KMS("Configuring HDMI audio for pixel clock %d (0x%08x)\n",
7594 hdmi_audio_clock[i].clock,
7595 hdmi_audio_clock[i].config);
7597 return hdmi_audio_clock[i].config;
7600 static bool intel_eld_uptodate(struct drm_connector *connector,
7601 int reg_eldv, uint32_t bits_eldv,
7602 int reg_elda, uint32_t bits_elda,
7605 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7606 uint8_t *eld = connector->eld;
7609 i = I915_READ(reg_eldv);
7618 i = I915_READ(reg_elda);
7620 I915_WRITE(reg_elda, i);
7622 for (i = 0; i < eld[2]; i++)
7623 if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
7629 static void g4x_write_eld(struct drm_connector *connector,
7630 struct drm_crtc *crtc,
7631 struct drm_display_mode *mode)
7633 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7634 uint8_t *eld = connector->eld;
7639 i = I915_READ(G4X_AUD_VID_DID);
7641 if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
7642 eldv = G4X_ELDV_DEVCL_DEVBLC;
7644 eldv = G4X_ELDV_DEVCTG;
7646 if (intel_eld_uptodate(connector,
7647 G4X_AUD_CNTL_ST, eldv,
7648 G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
7649 G4X_HDMIW_HDMIEDID))
7652 i = I915_READ(G4X_AUD_CNTL_ST);
7653 i &= ~(eldv | G4X_ELD_ADDR);
7654 len = (i >> 9) & 0x1f; /* ELD buffer size */
7655 I915_WRITE(G4X_AUD_CNTL_ST, i);
7660 len = min_t(uint8_t, eld[2], len);
7661 DRM_DEBUG_DRIVER("ELD size %d\n", len);
7662 for (i = 0; i < len; i++)
7663 I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
7665 i = I915_READ(G4X_AUD_CNTL_ST);
7667 I915_WRITE(G4X_AUD_CNTL_ST, i);
7670 static void haswell_write_eld(struct drm_connector *connector,
7671 struct drm_crtc *crtc,
7672 struct drm_display_mode *mode)
7674 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7675 uint8_t *eld = connector->eld;
7679 int pipe = to_intel_crtc(crtc)->pipe;
7682 int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
7683 int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
7684 int aud_config = HSW_AUD_CFG(pipe);
7685 int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
7687 /* Audio output enable */
7688 DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
7689 tmp = I915_READ(aud_cntrl_st2);
7690 tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
7691 I915_WRITE(aud_cntrl_st2, tmp);
7692 POSTING_READ(aud_cntrl_st2);
7694 assert_pipe_disabled(dev_priv, to_intel_crtc(crtc)->pipe);
7696 /* Set ELD valid state */
7697 tmp = I915_READ(aud_cntrl_st2);
7698 DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%08x\n", tmp);
7699 tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
7700 I915_WRITE(aud_cntrl_st2, tmp);
7701 tmp = I915_READ(aud_cntrl_st2);
7702 DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%08x\n", tmp);
7704 /* Enable HDMI mode */
7705 tmp = I915_READ(aud_config);
7706 DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%08x\n", tmp);
7707 /* clear N_programing_enable and N_value_index */
7708 tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
7709 I915_WRITE(aud_config, tmp);
7711 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
7713 eldv = AUDIO_ELD_VALID_A << (pipe * 4);
7715 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
7716 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
7717 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
7718 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
7720 I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
7723 if (intel_eld_uptodate(connector,
7724 aud_cntrl_st2, eldv,
7725 aud_cntl_st, IBX_ELD_ADDRESS,
7729 i = I915_READ(aud_cntrl_st2);
7731 I915_WRITE(aud_cntrl_st2, i);
7736 i = I915_READ(aud_cntl_st);
7737 i &= ~IBX_ELD_ADDRESS;
7738 I915_WRITE(aud_cntl_st, i);
7739 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
7740 DRM_DEBUG_DRIVER("port num:%d\n", i);
7742 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
7743 DRM_DEBUG_DRIVER("ELD size %d\n", len);
7744 for (i = 0; i < len; i++)
7745 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
7747 i = I915_READ(aud_cntrl_st2);
7749 I915_WRITE(aud_cntrl_st2, i);
7753 static void ironlake_write_eld(struct drm_connector *connector,
7754 struct drm_crtc *crtc,
7755 struct drm_display_mode *mode)
7757 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7758 uint8_t *eld = connector->eld;
7766 int pipe = to_intel_crtc(crtc)->pipe;
7768 if (HAS_PCH_IBX(connector->dev)) {
7769 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
7770 aud_config = IBX_AUD_CFG(pipe);
7771 aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
7772 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
7773 } else if (IS_VALLEYVIEW(connector->dev)) {
7774 hdmiw_hdmiedid = VLV_HDMIW_HDMIEDID(pipe);
7775 aud_config = VLV_AUD_CFG(pipe);
7776 aud_cntl_st = VLV_AUD_CNTL_ST(pipe);
7777 aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
7779 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
7780 aud_config = CPT_AUD_CFG(pipe);
7781 aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
7782 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
7785 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
7787 if (IS_VALLEYVIEW(connector->dev)) {
7788 struct intel_encoder *intel_encoder;
7789 struct intel_digital_port *intel_dig_port;
7791 intel_encoder = intel_attached_encoder(connector);
7792 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
7793 i = intel_dig_port->port;
7795 i = I915_READ(aud_cntl_st);
7796 i = (i >> 29) & DIP_PORT_SEL_MASK;
7797 /* DIP_Port_Select, 0x1 = PortB */
7801 DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
7802 /* operate blindly on all ports */
7803 eldv = IBX_ELD_VALIDB;
7804 eldv |= IBX_ELD_VALIDB << 4;
7805 eldv |= IBX_ELD_VALIDB << 8;
7807 DRM_DEBUG_DRIVER("ELD on port %c\n", port_name(i));
7808 eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
7811 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
7812 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
7813 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
7814 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
7816 I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
7819 if (intel_eld_uptodate(connector,
7820 aud_cntrl_st2, eldv,
7821 aud_cntl_st, IBX_ELD_ADDRESS,
7825 i = I915_READ(aud_cntrl_st2);
7827 I915_WRITE(aud_cntrl_st2, i);
7832 i = I915_READ(aud_cntl_st);
7833 i &= ~IBX_ELD_ADDRESS;
7834 I915_WRITE(aud_cntl_st, i);
7836 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
7837 DRM_DEBUG_DRIVER("ELD size %d\n", len);
7838 for (i = 0; i < len; i++)
7839 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
7841 i = I915_READ(aud_cntrl_st2);
7843 I915_WRITE(aud_cntrl_st2, i);
7846 void intel_write_eld(struct drm_encoder *encoder,
7847 struct drm_display_mode *mode)
7849 struct drm_crtc *crtc = encoder->crtc;
7850 struct drm_connector *connector;
7851 struct drm_device *dev = encoder->dev;
7852 struct drm_i915_private *dev_priv = dev->dev_private;
7854 connector = drm_select_eld(encoder, mode);
7858 DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
7861 connector->encoder->base.id,
7862 connector->encoder->name);
7864 connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
7866 if (dev_priv->display.write_eld)
7867 dev_priv->display.write_eld(connector, crtc, mode);
7870 static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
7872 struct drm_device *dev = crtc->dev;
7873 struct drm_i915_private *dev_priv = dev->dev_private;
7874 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7877 if (base != intel_crtc->cursor_base) {
7878 /* On these chipsets we can only modify the base whilst
7879 * the cursor is disabled.
7881 if (intel_crtc->cursor_cntl) {
7882 I915_WRITE(_CURACNTR, 0);
7883 POSTING_READ(_CURACNTR);
7884 intel_crtc->cursor_cntl = 0;
7887 I915_WRITE(_CURABASE, base);
7888 POSTING_READ(_CURABASE);
7891 /* XXX width must be 64, stride 256 => 0x00 << 28 */
7894 cntl = (CURSOR_ENABLE |
7895 CURSOR_GAMMA_ENABLE |
7896 CURSOR_FORMAT_ARGB);
7897 if (intel_crtc->cursor_cntl != cntl) {
7898 I915_WRITE(_CURACNTR, cntl);
7899 POSTING_READ(_CURACNTR);
7900 intel_crtc->cursor_cntl = cntl;
7904 static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
7906 struct drm_device *dev = crtc->dev;
7907 struct drm_i915_private *dev_priv = dev->dev_private;
7908 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7909 int pipe = intel_crtc->pipe;
7914 cntl = MCURSOR_GAMMA_ENABLE;
7915 switch (intel_crtc->cursor_width) {
7917 cntl |= CURSOR_MODE_64_ARGB_AX;
7920 cntl |= CURSOR_MODE_128_ARGB_AX;
7923 cntl |= CURSOR_MODE_256_ARGB_AX;
7929 cntl |= pipe << 28; /* Connect to correct pipe */
7931 if (intel_crtc->cursor_cntl != cntl) {
7932 I915_WRITE(CURCNTR(pipe), cntl);
7933 POSTING_READ(CURCNTR(pipe));
7934 intel_crtc->cursor_cntl = cntl;
7937 /* and commit changes on next vblank */
7938 I915_WRITE(CURBASE(pipe), base);
7939 POSTING_READ(CURBASE(pipe));
7942 static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
7944 struct drm_device *dev = crtc->dev;
7945 struct drm_i915_private *dev_priv = dev->dev_private;
7946 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7947 int pipe = intel_crtc->pipe;
7952 cntl = MCURSOR_GAMMA_ENABLE;
7953 switch (intel_crtc->cursor_width) {
7955 cntl |= CURSOR_MODE_64_ARGB_AX;
7958 cntl |= CURSOR_MODE_128_ARGB_AX;
7961 cntl |= CURSOR_MODE_256_ARGB_AX;
7968 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
7969 cntl |= CURSOR_PIPE_CSC_ENABLE;
7971 if (intel_crtc->cursor_cntl != cntl) {
7972 I915_WRITE(CURCNTR(pipe), cntl);
7973 POSTING_READ(CURCNTR(pipe));
7974 intel_crtc->cursor_cntl = cntl;
7977 /* and commit changes on next vblank */
7978 I915_WRITE(CURBASE(pipe), base);
7979 POSTING_READ(CURBASE(pipe));
7982 /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
7983 static void intel_crtc_update_cursor(struct drm_crtc *crtc,
7986 struct drm_device *dev = crtc->dev;
7987 struct drm_i915_private *dev_priv = dev->dev_private;
7988 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7989 int pipe = intel_crtc->pipe;
7990 int x = intel_crtc->cursor_x;
7991 int y = intel_crtc->cursor_y;
7992 u32 base = 0, pos = 0;
7995 base = intel_crtc->cursor_addr;
7997 if (x >= intel_crtc->config.pipe_src_w)
8000 if (y >= intel_crtc->config.pipe_src_h)
8004 if (x + intel_crtc->cursor_width <= 0)
8007 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
8010 pos |= x << CURSOR_X_SHIFT;
8013 if (y + intel_crtc->cursor_height <= 0)
8016 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
8019 pos |= y << CURSOR_Y_SHIFT;
8021 if (base == 0 && intel_crtc->cursor_base == 0)
8024 I915_WRITE(CURPOS(pipe), pos);
8026 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev) || IS_BROADWELL(dev))
8027 ivb_update_cursor(crtc, base);
8028 else if (IS_845G(dev) || IS_I865G(dev))
8029 i845_update_cursor(crtc, base);
8031 i9xx_update_cursor(crtc, base);
8032 intel_crtc->cursor_base = base;
8035 static int intel_crtc_cursor_set(struct drm_crtc *crtc,
8036 struct drm_file *file,
8038 uint32_t width, uint32_t height)
8040 struct drm_device *dev = crtc->dev;
8041 struct drm_i915_private *dev_priv = dev->dev_private;
8042 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8043 struct drm_i915_gem_object *obj;
8048 /* if we want to turn off the cursor ignore width and height */
8050 DRM_DEBUG_KMS("cursor off\n");
8053 mutex_lock(&dev->struct_mutex);
8057 /* Check for which cursor types we support */
8058 if (!((width == 64 && height == 64) ||
8059 (width == 128 && height == 128 && !IS_GEN2(dev)) ||
8060 (width == 256 && height == 256 && !IS_GEN2(dev)))) {
8061 DRM_DEBUG("Cursor dimension not supported\n");
8065 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
8066 if (&obj->base == NULL)
8069 if (obj->base.size < width * height * 4) {
8070 DRM_DEBUG_KMS("buffer is to small\n");
8075 /* we only need to pin inside GTT if cursor is non-phy */
8076 mutex_lock(&dev->struct_mutex);
8077 if (!INTEL_INFO(dev)->cursor_needs_physical) {
8080 if (obj->tiling_mode) {
8081 DRM_DEBUG_KMS("cursor cannot be tiled\n");
8086 /* Note that the w/a also requires 2 PTE of padding following
8087 * the bo. We currently fill all unused PTE with the shadow
8088 * page and so we should always have valid PTE following the
8089 * cursor preventing the VT-d warning.
8092 if (need_vtd_wa(dev))
8093 alignment = 64*1024;
8095 ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
8097 DRM_DEBUG_KMS("failed to move cursor bo into the GTT\n");
8101 ret = i915_gem_object_put_fence(obj);
8103 DRM_DEBUG_KMS("failed to release fence for cursor");
8107 addr = i915_gem_obj_ggtt_offset(obj);
8109 int align = IS_I830(dev) ? 16 * 1024 : 256;
8110 ret = i915_gem_object_attach_phys(obj, align);
8112 DRM_DEBUG_KMS("failed to attach phys object\n");
8115 addr = obj->phys_handle->busaddr;
8119 I915_WRITE(CURSIZE, (height << 12) | width);
8122 if (intel_crtc->cursor_bo) {
8123 if (!INTEL_INFO(dev)->cursor_needs_physical)
8124 i915_gem_object_unpin_from_display_plane(intel_crtc->cursor_bo);
8125 drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
8128 mutex_unlock(&dev->struct_mutex);
8130 old_width = intel_crtc->cursor_width;
8132 intel_crtc->cursor_addr = addr;
8133 intel_crtc->cursor_bo = obj;
8134 intel_crtc->cursor_width = width;
8135 intel_crtc->cursor_height = height;
8137 if (intel_crtc->active) {
8138 if (old_width != width)
8139 intel_update_watermarks(crtc);
8140 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
8145 i915_gem_object_unpin_from_display_plane(obj);
8147 mutex_unlock(&dev->struct_mutex);
8149 drm_gem_object_unreference_unlocked(&obj->base);
8153 static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
8155 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8157 intel_crtc->cursor_x = clamp_t(int, x, SHRT_MIN, SHRT_MAX);
8158 intel_crtc->cursor_y = clamp_t(int, y, SHRT_MIN, SHRT_MAX);
8160 if (intel_crtc->active)
8161 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
8166 static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
8167 u16 *blue, uint32_t start, uint32_t size)
8169 int end = (start + size > 256) ? 256 : start + size, i;
8170 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8172 for (i = start; i < end; i++) {
8173 intel_crtc->lut_r[i] = red[i] >> 8;
8174 intel_crtc->lut_g[i] = green[i] >> 8;
8175 intel_crtc->lut_b[i] = blue[i] >> 8;
8178 intel_crtc_load_lut(crtc);
8181 /* VESA 640x480x72Hz mode to set on the pipe */
8182 static struct drm_display_mode load_detect_mode = {
8183 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
8184 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
8187 struct drm_framebuffer *
8188 __intel_framebuffer_create(struct drm_device *dev,
8189 struct drm_mode_fb_cmd2 *mode_cmd,
8190 struct drm_i915_gem_object *obj)
8192 struct intel_framebuffer *intel_fb;
8195 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
8197 drm_gem_object_unreference_unlocked(&obj->base);
8198 return ERR_PTR(-ENOMEM);
8201 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
8205 return &intel_fb->base;
8207 drm_gem_object_unreference_unlocked(&obj->base);
8210 return ERR_PTR(ret);
8213 static struct drm_framebuffer *
8214 intel_framebuffer_create(struct drm_device *dev,
8215 struct drm_mode_fb_cmd2 *mode_cmd,
8216 struct drm_i915_gem_object *obj)
8218 struct drm_framebuffer *fb;
8221 ret = i915_mutex_lock_interruptible(dev);
8223 return ERR_PTR(ret);
8224 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
8225 mutex_unlock(&dev->struct_mutex);
8231 intel_framebuffer_pitch_for_width(int width, int bpp)
8233 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
8234 return ALIGN(pitch, 64);
8238 intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
8240 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
8241 return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
8244 static struct drm_framebuffer *
8245 intel_framebuffer_create_for_mode(struct drm_device *dev,
8246 struct drm_display_mode *mode,
8249 struct drm_i915_gem_object *obj;
8250 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
8252 obj = i915_gem_alloc_object(dev,
8253 intel_framebuffer_size_for_mode(mode, bpp));
8255 return ERR_PTR(-ENOMEM);
8257 mode_cmd.width = mode->hdisplay;
8258 mode_cmd.height = mode->vdisplay;
8259 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
8261 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
8263 return intel_framebuffer_create(dev, &mode_cmd, obj);
8266 static struct drm_framebuffer *
8267 mode_fits_in_fbdev(struct drm_device *dev,
8268 struct drm_display_mode *mode)
8270 #ifdef CONFIG_DRM_I915_FBDEV
8271 struct drm_i915_private *dev_priv = dev->dev_private;
8272 struct drm_i915_gem_object *obj;
8273 struct drm_framebuffer *fb;
8275 if (!dev_priv->fbdev)
8278 if (!dev_priv->fbdev->fb)
8281 obj = dev_priv->fbdev->fb->obj;
8284 fb = &dev_priv->fbdev->fb->base;
8285 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
8286 fb->bits_per_pixel))
8289 if (obj->base.size < mode->vdisplay * fb->pitches[0])
8298 bool intel_get_load_detect_pipe(struct drm_connector *connector,
8299 struct drm_display_mode *mode,
8300 struct intel_load_detect_pipe *old,
8301 struct drm_modeset_acquire_ctx *ctx)
8303 struct intel_crtc *intel_crtc;
8304 struct intel_encoder *intel_encoder =
8305 intel_attached_encoder(connector);
8306 struct drm_crtc *possible_crtc;
8307 struct drm_encoder *encoder = &intel_encoder->base;
8308 struct drm_crtc *crtc = NULL;
8309 struct drm_device *dev = encoder->dev;
8310 struct drm_framebuffer *fb;
8311 struct drm_mode_config *config = &dev->mode_config;
8314 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
8315 connector->base.id, connector->name,
8316 encoder->base.id, encoder->name);
8318 drm_modeset_acquire_init(ctx, 0);
8321 ret = drm_modeset_lock(&config->connection_mutex, ctx);
8326 * Algorithm gets a little messy:
8328 * - if the connector already has an assigned crtc, use it (but make
8329 * sure it's on first)
8331 * - try to find the first unused crtc that can drive this connector,
8332 * and use that if we find one
8335 /* See if we already have a CRTC for this connector */
8336 if (encoder->crtc) {
8337 crtc = encoder->crtc;
8339 ret = drm_modeset_lock(&crtc->mutex, ctx);
8343 old->dpms_mode = connector->dpms;
8344 old->load_detect_temp = false;
8346 /* Make sure the crtc and connector are running */
8347 if (connector->dpms != DRM_MODE_DPMS_ON)
8348 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
8353 /* Find an unused one (if possible) */
8354 for_each_crtc(dev, possible_crtc) {
8356 if (!(encoder->possible_crtcs & (1 << i)))
8358 if (!possible_crtc->enabled) {
8359 crtc = possible_crtc;
8365 * If we didn't find an unused CRTC, don't use any.
8368 DRM_DEBUG_KMS("no pipe available for load-detect\n");
8372 ret = drm_modeset_lock(&crtc->mutex, ctx);
8375 intel_encoder->new_crtc = to_intel_crtc(crtc);
8376 to_intel_connector(connector)->new_encoder = intel_encoder;
8378 intel_crtc = to_intel_crtc(crtc);
8379 intel_crtc->new_enabled = true;
8380 intel_crtc->new_config = &intel_crtc->config;
8381 old->dpms_mode = connector->dpms;
8382 old->load_detect_temp = true;
8383 old->release_fb = NULL;
8386 mode = &load_detect_mode;
8388 /* We need a framebuffer large enough to accommodate all accesses
8389 * that the plane may generate whilst we perform load detection.
8390 * We can not rely on the fbcon either being present (we get called
8391 * during its initialisation to detect all boot displays, or it may
8392 * not even exist) or that it is large enough to satisfy the
8395 fb = mode_fits_in_fbdev(dev, mode);
8397 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
8398 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
8399 old->release_fb = fb;
8401 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
8403 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
8407 if (intel_set_mode(crtc, mode, 0, 0, fb)) {
8408 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
8409 if (old->release_fb)
8410 old->release_fb->funcs->destroy(old->release_fb);
8414 /* let the connector get through one full cycle before testing */
8415 intel_wait_for_vblank(dev, intel_crtc->pipe);
8419 intel_crtc->new_enabled = crtc->enabled;
8420 if (intel_crtc->new_enabled)
8421 intel_crtc->new_config = &intel_crtc->config;
8423 intel_crtc->new_config = NULL;
8425 if (ret == -EDEADLK) {
8426 drm_modeset_backoff(ctx);
8430 drm_modeset_drop_locks(ctx);
8431 drm_modeset_acquire_fini(ctx);
8436 void intel_release_load_detect_pipe(struct drm_connector *connector,
8437 struct intel_load_detect_pipe *old,
8438 struct drm_modeset_acquire_ctx *ctx)
8440 struct intel_encoder *intel_encoder =
8441 intel_attached_encoder(connector);
8442 struct drm_encoder *encoder = &intel_encoder->base;
8443 struct drm_crtc *crtc = encoder->crtc;
8444 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8446 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
8447 connector->base.id, connector->name,
8448 encoder->base.id, encoder->name);
8450 if (old->load_detect_temp) {
8451 to_intel_connector(connector)->new_encoder = NULL;
8452 intel_encoder->new_crtc = NULL;
8453 intel_crtc->new_enabled = false;
8454 intel_crtc->new_config = NULL;
8455 intel_set_mode(crtc, NULL, 0, 0, NULL);
8457 if (old->release_fb) {
8458 drm_framebuffer_unregister_private(old->release_fb);
8459 drm_framebuffer_unreference(old->release_fb);
8466 /* Switch crtc and encoder back off if necessary */
8467 if (old->dpms_mode != DRM_MODE_DPMS_ON)
8468 connector->funcs->dpms(connector, old->dpms_mode);
8471 drm_modeset_drop_locks(ctx);
8472 drm_modeset_acquire_fini(ctx);
8475 static int i9xx_pll_refclk(struct drm_device *dev,
8476 const struct intel_crtc_config *pipe_config)
8478 struct drm_i915_private *dev_priv = dev->dev_private;
8479 u32 dpll = pipe_config->dpll_hw_state.dpll;
8481 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
8482 return dev_priv->vbt.lvds_ssc_freq;
8483 else if (HAS_PCH_SPLIT(dev))
8485 else if (!IS_GEN2(dev))
8491 /* Returns the clock of the currently programmed mode of the given pipe. */
8492 static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
8493 struct intel_crtc_config *pipe_config)
8495 struct drm_device *dev = crtc->base.dev;
8496 struct drm_i915_private *dev_priv = dev->dev_private;
8497 int pipe = pipe_config->cpu_transcoder;
8498 u32 dpll = pipe_config->dpll_hw_state.dpll;
8500 intel_clock_t clock;
8501 int refclk = i9xx_pll_refclk(dev, pipe_config);
8503 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
8504 fp = pipe_config->dpll_hw_state.fp0;
8506 fp = pipe_config->dpll_hw_state.fp1;
8508 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
8509 if (IS_PINEVIEW(dev)) {
8510 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
8511 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
8513 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
8514 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
8517 if (!IS_GEN2(dev)) {
8518 if (IS_PINEVIEW(dev))
8519 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
8520 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
8522 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
8523 DPLL_FPA01_P1_POST_DIV_SHIFT);
8525 switch (dpll & DPLL_MODE_MASK) {
8526 case DPLLB_MODE_DAC_SERIAL:
8527 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
8530 case DPLLB_MODE_LVDS:
8531 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
8535 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
8536 "mode\n", (int)(dpll & DPLL_MODE_MASK));
8540 if (IS_PINEVIEW(dev))
8541 pineview_clock(refclk, &clock);
8543 i9xx_clock(refclk, &clock);
8545 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
8546 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
8549 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
8550 DPLL_FPA01_P1_POST_DIV_SHIFT);
8552 if (lvds & LVDS_CLKB_POWER_UP)
8557 if (dpll & PLL_P1_DIVIDE_BY_TWO)
8560 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
8561 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
8563 if (dpll & PLL_P2_DIVIDE_BY_4)
8569 i9xx_clock(refclk, &clock);
8573 * This value includes pixel_multiplier. We will use
8574 * port_clock to compute adjusted_mode.crtc_clock in the
8575 * encoder's get_config() function.
8577 pipe_config->port_clock = clock.dot;
8580 int intel_dotclock_calculate(int link_freq,
8581 const struct intel_link_m_n *m_n)
8584 * The calculation for the data clock is:
8585 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
8586 * But we want to avoid losing precison if possible, so:
8587 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
8589 * and the link clock is simpler:
8590 * link_clock = (m * link_clock) / n
8596 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
8599 static void ironlake_pch_clock_get(struct intel_crtc *crtc,
8600 struct intel_crtc_config *pipe_config)
8602 struct drm_device *dev = crtc->base.dev;
8604 /* read out port_clock from the DPLL */
8605 i9xx_crtc_clock_get(crtc, pipe_config);
8608 * This value does not include pixel_multiplier.
8609 * We will check that port_clock and adjusted_mode.crtc_clock
8610 * agree once we know their relationship in the encoder's
8611 * get_config() function.
8613 pipe_config->adjusted_mode.crtc_clock =
8614 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
8615 &pipe_config->fdi_m_n);
8618 /** Returns the currently programmed mode of the given pipe. */
8619 struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
8620 struct drm_crtc *crtc)
8622 struct drm_i915_private *dev_priv = dev->dev_private;
8623 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8624 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
8625 struct drm_display_mode *mode;
8626 struct intel_crtc_config pipe_config;
8627 int htot = I915_READ(HTOTAL(cpu_transcoder));
8628 int hsync = I915_READ(HSYNC(cpu_transcoder));
8629 int vtot = I915_READ(VTOTAL(cpu_transcoder));
8630 int vsync = I915_READ(VSYNC(cpu_transcoder));
8631 enum i915_pipe pipe = intel_crtc->pipe;
8633 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
8638 * Construct a pipe_config sufficient for getting the clock info
8639 * back out of crtc_clock_get.
8641 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
8642 * to use a real value here instead.
8644 pipe_config.cpu_transcoder = (enum transcoder) pipe;
8645 pipe_config.pixel_multiplier = 1;
8646 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
8647 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
8648 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
8649 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
8651 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
8652 mode->hdisplay = (htot & 0xffff) + 1;
8653 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
8654 mode->hsync_start = (hsync & 0xffff) + 1;
8655 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
8656 mode->vdisplay = (vtot & 0xffff) + 1;
8657 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
8658 mode->vsync_start = (vsync & 0xffff) + 1;
8659 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
8661 drm_mode_set_name(mode);
8666 static void intel_increase_pllclock(struct drm_crtc *crtc)
8668 struct drm_device *dev = crtc->dev;
8669 struct drm_i915_private *dev_priv = dev->dev_private;
8670 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8671 int pipe = intel_crtc->pipe;
8672 int dpll_reg = DPLL(pipe);
8675 if (HAS_PCH_SPLIT(dev))
8678 if (!dev_priv->lvds_downclock_avail)
8681 dpll = I915_READ(dpll_reg);
8682 if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
8683 DRM_DEBUG_DRIVER("upclocking LVDS\n");
8685 assert_panel_unlocked(dev_priv, pipe);
8687 dpll &= ~DISPLAY_RATE_SELECT_FPA1;
8688 I915_WRITE(dpll_reg, dpll);
8689 intel_wait_for_vblank(dev, pipe);
8691 dpll = I915_READ(dpll_reg);
8692 if (dpll & DISPLAY_RATE_SELECT_FPA1)
8693 DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
8697 static void intel_decrease_pllclock(struct drm_crtc *crtc)
8699 struct drm_device *dev = crtc->dev;
8700 struct drm_i915_private *dev_priv = dev->dev_private;
8701 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8703 if (HAS_PCH_SPLIT(dev))
8706 if (!dev_priv->lvds_downclock_avail)
8710 * Since this is called by a timer, we should never get here in
8713 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
8714 int pipe = intel_crtc->pipe;
8715 int dpll_reg = DPLL(pipe);
8718 DRM_DEBUG_DRIVER("downclocking LVDS\n");
8720 assert_panel_unlocked(dev_priv, pipe);
8722 dpll = I915_READ(dpll_reg);
8723 dpll |= DISPLAY_RATE_SELECT_FPA1;
8724 I915_WRITE(dpll_reg, dpll);
8725 intel_wait_for_vblank(dev, pipe);
8726 dpll = I915_READ(dpll_reg);
8727 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
8728 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
8733 void intel_mark_busy(struct drm_device *dev)
8735 struct drm_i915_private *dev_priv = dev->dev_private;
8737 if (dev_priv->mm.busy)
8740 intel_runtime_pm_get(dev_priv);
8741 i915_update_gfx_val(dev_priv);
8742 dev_priv->mm.busy = true;
8745 void intel_mark_idle(struct drm_device *dev)
8747 struct drm_i915_private *dev_priv = dev->dev_private;
8748 struct drm_crtc *crtc;
8750 if (!dev_priv->mm.busy)
8753 dev_priv->mm.busy = false;
8755 if (!i915.powersave)
8758 for_each_crtc(dev, crtc) {
8759 if (!crtc->primary->fb)
8762 intel_decrease_pllclock(crtc);
8765 if (INTEL_INFO(dev)->gen >= 6)
8766 gen6_rps_idle(dev->dev_private);
8769 intel_runtime_pm_put(dev_priv);
8772 void intel_mark_fb_busy(struct drm_i915_gem_object *obj,
8773 struct intel_engine_cs *ring)
8775 struct drm_device *dev = obj->base.dev;
8776 struct drm_crtc *crtc;
8778 if (!i915.powersave)
8781 for_each_crtc(dev, crtc) {
8782 if (!crtc->primary->fb)
8785 if (to_intel_framebuffer(crtc->primary->fb)->obj != obj)
8788 intel_increase_pllclock(crtc);
8789 if (ring && intel_fbc_enabled(dev))
8790 ring->fbc_dirty = true;
8794 static void intel_crtc_destroy(struct drm_crtc *crtc)
8796 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8797 struct drm_device *dev = crtc->dev;
8798 struct intel_unpin_work *work;
8800 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
8801 work = intel_crtc->unpin_work;
8802 intel_crtc->unpin_work = NULL;
8803 lockmgr(&dev->event_lock, LK_RELEASE);
8806 cancel_work_sync(&work->work);
8810 intel_crtc_cursor_set(crtc, NULL, 0, 0, 0);
8812 drm_crtc_cleanup(crtc);
8817 static void intel_unpin_work_fn(struct work_struct *__work)
8819 struct intel_unpin_work *work =
8820 container_of(__work, struct intel_unpin_work, work);
8821 struct drm_device *dev = work->crtc->dev;
8823 mutex_lock(&dev->struct_mutex);
8824 intel_unpin_fb_obj(work->old_fb_obj);
8825 drm_gem_object_unreference(&work->pending_flip_obj->base);
8826 drm_gem_object_unreference(&work->old_fb_obj->base);
8828 intel_update_fbc(dev);
8829 mutex_unlock(&dev->struct_mutex);
8831 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
8832 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
8837 static void do_intel_finish_page_flip(struct drm_device *dev,
8838 struct drm_crtc *crtc)
8840 struct drm_i915_private *dev_priv = dev->dev_private;
8841 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8842 struct intel_unpin_work *work;
8844 /* Ignore early vblank irqs */
8845 if (intel_crtc == NULL)
8848 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
8849 work = intel_crtc->unpin_work;
8851 /* Ensure we don't miss a work->pending update ... */
8854 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
8855 lockmgr(&dev->event_lock, LK_RELEASE);
8859 /* and that the unpin work is consistent wrt ->pending. */
8862 intel_crtc->unpin_work = NULL;
8865 drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
8867 drm_crtc_vblank_put(crtc);
8869 lockmgr(&dev->event_lock, LK_RELEASE);
8871 wake_up_all(&dev_priv->pending_flip_queue);
8873 queue_work(dev_priv->wq, &work->work);
8875 trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
8878 void intel_finish_page_flip(struct drm_device *dev, int pipe)
8880 struct drm_i915_private *dev_priv = dev->dev_private;
8881 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
8883 do_intel_finish_page_flip(dev, crtc);
8886 void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
8888 struct drm_i915_private *dev_priv = dev->dev_private;
8889 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
8891 do_intel_finish_page_flip(dev, crtc);
8894 /* Is 'a' after or equal to 'b'? */
8895 static bool g4x_flip_count_after_eq(u32 a, u32 b)
8897 return !((a - b) & 0x80000000);
8900 static bool page_flip_finished(struct intel_crtc *crtc)
8902 struct drm_device *dev = crtc->base.dev;
8903 struct drm_i915_private *dev_priv = dev->dev_private;
8906 * The relevant registers doen't exist on pre-ctg.
8907 * As the flip done interrupt doesn't trigger for mmio
8908 * flips on gmch platforms, a flip count check isn't
8909 * really needed there. But since ctg has the registers,
8910 * include it in the check anyway.
8912 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
8916 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
8917 * used the same base address. In that case the mmio flip might
8918 * have completed, but the CS hasn't even executed the flip yet.
8920 * A flip count check isn't enough as the CS might have updated
8921 * the base address just after start of vblank, but before we
8922 * managed to process the interrupt. This means we'd complete the
8925 * Combining both checks should get us a good enough result. It may
8926 * still happen that the CS flip has been executed, but has not
8927 * yet actually completed. But in case the base address is the same
8928 * anyway, we don't really care.
8930 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
8931 crtc->unpin_work->gtt_offset &&
8932 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
8933 crtc->unpin_work->flip_count);
8936 void intel_prepare_page_flip(struct drm_device *dev, int plane)
8938 struct drm_i915_private *dev_priv = dev->dev_private;
8939 struct intel_crtc *intel_crtc =
8940 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
8942 /* NB: An MMIO update of the plane base pointer will also
8943 * generate a page-flip completion irq, i.e. every modeset
8944 * is also accompanied by a spurious intel_prepare_page_flip().
8946 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
8947 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
8948 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
8949 lockmgr(&dev->event_lock, LK_RELEASE);
8952 static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
8954 /* Ensure that the work item is consistent when activating it ... */
8956 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
8957 /* and that it is marked active as soon as the irq could fire. */
8961 static int intel_gen2_queue_flip(struct drm_device *dev,
8962 struct drm_crtc *crtc,
8963 struct drm_framebuffer *fb,
8964 struct drm_i915_gem_object *obj,
8965 struct intel_engine_cs *ring,
8968 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8972 ret = intel_ring_begin(ring, 6);
8976 /* Can't queue multiple flips, so wait for the previous
8977 * one to finish before executing the next.
8979 if (intel_crtc->plane)
8980 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
8982 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
8983 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
8984 intel_ring_emit(ring, MI_NOOP);
8985 intel_ring_emit(ring, MI_DISPLAY_FLIP |
8986 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
8987 intel_ring_emit(ring, fb->pitches[0]);
8988 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
8989 intel_ring_emit(ring, 0); /* aux display base address, unused */
8991 intel_mark_page_flip_active(intel_crtc);
8992 __intel_ring_advance(ring);
8996 static int intel_gen3_queue_flip(struct drm_device *dev,
8997 struct drm_crtc *crtc,
8998 struct drm_framebuffer *fb,
8999 struct drm_i915_gem_object *obj,
9000 struct intel_engine_cs *ring,
9003 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9007 ret = intel_ring_begin(ring, 6);
9011 if (intel_crtc->plane)
9012 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
9014 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
9015 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
9016 intel_ring_emit(ring, MI_NOOP);
9017 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
9018 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9019 intel_ring_emit(ring, fb->pitches[0]);
9020 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
9021 intel_ring_emit(ring, MI_NOOP);
9023 intel_mark_page_flip_active(intel_crtc);
9024 __intel_ring_advance(ring);
9028 static int intel_gen4_queue_flip(struct drm_device *dev,
9029 struct drm_crtc *crtc,
9030 struct drm_framebuffer *fb,
9031 struct drm_i915_gem_object *obj,
9032 struct intel_engine_cs *ring,
9035 struct drm_i915_private *dev_priv = dev->dev_private;
9036 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9037 uint32_t pf, pipesrc;
9040 ret = intel_ring_begin(ring, 4);
9044 /* i965+ uses the linear or tiled offsets from the
9045 * Display Registers (which do not change across a page-flip)
9046 * so we need only reprogram the base address.
9048 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9049 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9050 intel_ring_emit(ring, fb->pitches[0]);
9051 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
9054 /* XXX Enabling the panel-fitter across page-flip is so far
9055 * untested on non-native modes, so ignore it for now.
9056 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
9059 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
9060 intel_ring_emit(ring, pf | pipesrc);
9062 intel_mark_page_flip_active(intel_crtc);
9063 __intel_ring_advance(ring);
9067 static int intel_gen6_queue_flip(struct drm_device *dev,
9068 struct drm_crtc *crtc,
9069 struct drm_framebuffer *fb,
9070 struct drm_i915_gem_object *obj,
9071 struct intel_engine_cs *ring,
9074 struct drm_i915_private *dev_priv = dev->dev_private;
9075 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9076 uint32_t pf, pipesrc;
9079 ret = intel_ring_begin(ring, 4);
9083 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9084 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9085 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
9086 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
9088 /* Contrary to the suggestions in the documentation,
9089 * "Enable Panel Fitter" does not seem to be required when page
9090 * flipping with a non-native mode, and worse causes a normal
9092 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
9095 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
9096 intel_ring_emit(ring, pf | pipesrc);
9098 intel_mark_page_flip_active(intel_crtc);
9099 __intel_ring_advance(ring);
9103 static int intel_gen7_queue_flip(struct drm_device *dev,
9104 struct drm_crtc *crtc,
9105 struct drm_framebuffer *fb,
9106 struct drm_i915_gem_object *obj,
9107 struct intel_engine_cs *ring,
9110 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9111 uint32_t plane_bit = 0;
9114 switch (intel_crtc->plane) {
9116 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
9119 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
9122 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
9125 WARN_ONCE(1, "unknown plane in flip command\n");
9130 if (ring->id == RCS) {
9133 * On Gen 8, SRM is now taking an extra dword to accommodate
9134 * 48bits addresses, and we need a NOOP for the batch size to
9142 * BSpec MI_DISPLAY_FLIP for IVB:
9143 * "The full packet must be contained within the same cache line."
9145 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
9146 * cacheline, if we ever start emitting more commands before
9147 * the MI_DISPLAY_FLIP we may need to first emit everything else,
9148 * then do the cacheline alignment, and finally emit the
9151 ret = intel_ring_cacheline_align(ring);
9155 ret = intel_ring_begin(ring, len);
9159 /* Unmask the flip-done completion message. Note that the bspec says that
9160 * we should do this for both the BCS and RCS, and that we must not unmask
9161 * more than one flip event at any time (or ensure that one flip message
9162 * can be sent by waiting for flip-done prior to queueing new flips).
9163 * Experimentation says that BCS works despite DERRMR masking all
9164 * flip-done completion events and that unmasking all planes at once
9165 * for the RCS also doesn't appear to drop events. Setting the DERRMR
9166 * to zero does lead to lockups within MI_DISPLAY_FLIP.
9168 if (ring->id == RCS) {
9169 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
9170 intel_ring_emit(ring, DERRMR);
9171 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
9172 DERRMR_PIPEB_PRI_FLIP_DONE |
9173 DERRMR_PIPEC_PRI_FLIP_DONE));
9175 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
9176 MI_SRM_LRM_GLOBAL_GTT);
9178 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
9179 MI_SRM_LRM_GLOBAL_GTT);
9180 intel_ring_emit(ring, DERRMR);
9181 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
9183 intel_ring_emit(ring, 0);
9184 intel_ring_emit(ring, MI_NOOP);
9188 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
9189 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
9190 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
9191 intel_ring_emit(ring, (MI_NOOP));
9193 intel_mark_page_flip_active(intel_crtc);
9194 __intel_ring_advance(ring);
9198 static int intel_default_queue_flip(struct drm_device *dev,
9199 struct drm_crtc *crtc,
9200 struct drm_framebuffer *fb,
9201 struct drm_i915_gem_object *obj,
9202 struct intel_engine_cs *ring,
9208 static int intel_crtc_page_flip(struct drm_crtc *crtc,
9209 struct drm_framebuffer *fb,
9210 struct drm_pending_vblank_event *event,
9211 uint32_t page_flip_flags)
9213 struct drm_device *dev = crtc->dev;
9214 struct drm_i915_private *dev_priv = dev->dev_private;
9215 struct drm_framebuffer *old_fb = crtc->primary->fb;
9216 struct drm_i915_gem_object *obj = to_intel_framebuffer(fb)->obj;
9217 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9218 struct intel_unpin_work *work;
9219 struct intel_engine_cs *ring;
9222 /* Can't change pixel format via MI display flips. */
9223 if (fb->pixel_format != crtc->primary->fb->pixel_format)
9227 * TILEOFF/LINOFF registers can't be changed via MI display flips.
9228 * Note that pitch changes could also affect these register.
9230 if (INTEL_INFO(dev)->gen > 3 &&
9231 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
9232 fb->pitches[0] != crtc->primary->fb->pitches[0]))
9235 if (i915_terminally_wedged(&dev_priv->gpu_error))
9238 work = kzalloc(sizeof(*work), GFP_KERNEL);
9242 work->event = event;
9244 work->old_fb_obj = to_intel_framebuffer(old_fb)->obj;
9245 INIT_WORK(&work->work, intel_unpin_work_fn);
9247 ret = drm_crtc_vblank_get(crtc);
9251 /* We borrow the event spin lock for protecting unpin_work */
9252 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
9253 if (intel_crtc->unpin_work) {
9254 lockmgr(&dev->event_lock, LK_RELEASE);
9256 drm_crtc_vblank_put(crtc);
9258 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
9261 intel_crtc->unpin_work = work;
9262 lockmgr(&dev->event_lock, LK_RELEASE);
9264 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
9265 flush_workqueue(dev_priv->wq);
9267 ret = i915_mutex_lock_interruptible(dev);
9271 /* Reference the objects for the scheduled work. */
9272 drm_gem_object_reference(&work->old_fb_obj->base);
9273 drm_gem_object_reference(&obj->base);
9275 crtc->primary->fb = fb;
9277 work->pending_flip_obj = obj;
9279 work->enable_stall_check = true;
9281 atomic_inc(&intel_crtc->unpin_work_count);
9282 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
9284 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
9285 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(intel_crtc->pipe)) + 1;
9287 if (IS_VALLEYVIEW(dev)) {
9288 ring = &dev_priv->ring[BCS];
9289 } else if (INTEL_INFO(dev)->gen >= 7) {
9291 if (ring == NULL || ring->id != RCS)
9292 ring = &dev_priv->ring[BCS];
9294 ring = &dev_priv->ring[RCS];
9297 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
9299 goto cleanup_pending;
9302 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset;
9304 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring, page_flip_flags);
9308 intel_disable_fbc(dev);
9309 intel_mark_fb_busy(obj, NULL);
9310 mutex_unlock(&dev->struct_mutex);
9312 trace_i915_flip_request(intel_crtc->plane, obj);
9317 intel_unpin_fb_obj(obj);
9319 atomic_dec(&intel_crtc->unpin_work_count);
9320 crtc->primary->fb = old_fb;
9321 drm_gem_object_unreference(&work->old_fb_obj->base);
9322 drm_gem_object_unreference(&obj->base);
9323 mutex_unlock(&dev->struct_mutex);
9326 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
9327 intel_crtc->unpin_work = NULL;
9328 lockmgr(&dev->event_lock, LK_RELEASE);
9330 drm_crtc_vblank_put(crtc);
9336 intel_crtc_wait_for_pending_flips(crtc);
9337 ret = intel_pipe_set_base(crtc, crtc->x, crtc->y, fb);
9338 if (ret == 0 && event)
9339 drm_send_vblank_event(dev, intel_crtc->pipe, event);
9344 static struct drm_crtc_helper_funcs intel_helper_funcs = {
9345 .mode_set_base_atomic = intel_pipe_set_base_atomic,
9346 .load_lut = intel_crtc_load_lut,
9350 * intel_modeset_update_staged_output_state
9352 * Updates the staged output configuration state, e.g. after we've read out the
9355 static void intel_modeset_update_staged_output_state(struct drm_device *dev)
9357 struct intel_crtc *crtc;
9358 struct intel_encoder *encoder;
9359 struct intel_connector *connector;
9361 list_for_each_entry(connector, &dev->mode_config.connector_list,
9363 connector->new_encoder =
9364 to_intel_encoder(connector->base.encoder);
9367 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9370 to_intel_crtc(encoder->base.crtc);
9373 for_each_intel_crtc(dev, crtc) {
9374 crtc->new_enabled = crtc->base.enabled;
9376 if (crtc->new_enabled)
9377 crtc->new_config = &crtc->config;
9379 crtc->new_config = NULL;
9384 * intel_modeset_commit_output_state
9386 * This function copies the stage display pipe configuration to the real one.
9388 static void intel_modeset_commit_output_state(struct drm_device *dev)
9390 struct intel_crtc *crtc;
9391 struct intel_encoder *encoder;
9392 struct intel_connector *connector;
9394 list_for_each_entry(connector, &dev->mode_config.connector_list,
9396 connector->base.encoder = &connector->new_encoder->base;
9399 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9401 encoder->base.crtc = &encoder->new_crtc->base;
9404 for_each_intel_crtc(dev, crtc) {
9405 crtc->base.enabled = crtc->new_enabled;
9410 connected_sink_compute_bpp(struct intel_connector *connector,
9411 struct intel_crtc_config *pipe_config)
9413 int bpp = pipe_config->pipe_bpp;
9415 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
9416 connector->base.base.id,
9417 connector->base.name);
9419 /* Don't use an invalid EDID bpc value */
9420 if (connector->base.display_info.bpc &&
9421 connector->base.display_info.bpc * 3 < bpp) {
9422 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
9423 bpp, connector->base.display_info.bpc*3);
9424 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
9427 /* Clamp bpp to 8 on screens without EDID 1.4 */
9428 if (connector->base.display_info.bpc == 0 && bpp > 24) {
9429 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
9431 pipe_config->pipe_bpp = 24;
9436 compute_baseline_pipe_bpp(struct intel_crtc *crtc,
9437 struct drm_framebuffer *fb,
9438 struct intel_crtc_config *pipe_config)
9440 struct drm_device *dev = crtc->base.dev;
9441 struct intel_connector *connector;
9444 switch (fb->pixel_format) {
9446 bpp = 8*3; /* since we go through a colormap */
9448 case DRM_FORMAT_XRGB1555:
9449 case DRM_FORMAT_ARGB1555:
9450 /* checked in intel_framebuffer_init already */
9451 if (WARN_ON(INTEL_INFO(dev)->gen > 3))
9453 case DRM_FORMAT_RGB565:
9454 bpp = 6*3; /* min is 18bpp */
9456 case DRM_FORMAT_XBGR8888:
9457 case DRM_FORMAT_ABGR8888:
9458 /* checked in intel_framebuffer_init already */
9459 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
9461 case DRM_FORMAT_XRGB8888:
9462 case DRM_FORMAT_ARGB8888:
9465 case DRM_FORMAT_XRGB2101010:
9466 case DRM_FORMAT_ARGB2101010:
9467 case DRM_FORMAT_XBGR2101010:
9468 case DRM_FORMAT_ABGR2101010:
9469 /* checked in intel_framebuffer_init already */
9470 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
9474 /* TODO: gen4+ supports 16 bpc floating point, too. */
9476 DRM_DEBUG_KMS("unsupported depth\n");
9480 pipe_config->pipe_bpp = bpp;
9482 /* Clamp display bpp to EDID value */
9483 list_for_each_entry(connector, &dev->mode_config.connector_list,
9485 if (!connector->new_encoder ||
9486 connector->new_encoder->new_crtc != crtc)
9489 connected_sink_compute_bpp(connector, pipe_config);
9495 static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
9497 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
9498 "type: 0x%x flags: 0x%x\n",
9500 mode->crtc_hdisplay, mode->crtc_hsync_start,
9501 mode->crtc_hsync_end, mode->crtc_htotal,
9502 mode->crtc_vdisplay, mode->crtc_vsync_start,
9503 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
9506 static void intel_dump_pipe_config(struct intel_crtc *crtc,
9507 struct intel_crtc_config *pipe_config,
9508 const char *context)
9510 DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
9511 context, pipe_name(crtc->pipe));
9513 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
9514 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
9515 pipe_config->pipe_bpp, pipe_config->dither);
9516 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
9517 pipe_config->has_pch_encoder,
9518 pipe_config->fdi_lanes,
9519 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
9520 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
9521 pipe_config->fdi_m_n.tu);
9522 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
9523 pipe_config->has_dp_encoder,
9524 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
9525 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
9526 pipe_config->dp_m_n.tu);
9527 DRM_DEBUG_KMS("requested mode:\n");
9528 drm_mode_debug_printmodeline(&pipe_config->requested_mode);
9529 DRM_DEBUG_KMS("adjusted mode:\n");
9530 drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
9531 intel_dump_crtc_timings(&pipe_config->adjusted_mode);
9532 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
9533 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
9534 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
9535 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
9536 pipe_config->gmch_pfit.control,
9537 pipe_config->gmch_pfit.pgm_ratios,
9538 pipe_config->gmch_pfit.lvds_border_bits);
9539 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
9540 pipe_config->pch_pfit.pos,
9541 pipe_config->pch_pfit.size,
9542 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
9543 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
9544 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
9547 static bool encoders_cloneable(const struct intel_encoder *a,
9548 const struct intel_encoder *b)
9550 /* masks could be asymmetric, so check both ways */
9551 return a == b || (a->cloneable & (1 << b->type) &&
9552 b->cloneable & (1 << a->type));
9555 static bool check_single_encoder_cloning(struct intel_crtc *crtc,
9556 struct intel_encoder *encoder)
9558 struct drm_device *dev = crtc->base.dev;
9559 struct intel_encoder *source_encoder;
9561 list_for_each_entry(source_encoder,
9562 &dev->mode_config.encoder_list, base.head) {
9563 if (source_encoder->new_crtc != crtc)
9566 if (!encoders_cloneable(encoder, source_encoder))
9573 static bool check_encoder_cloning(struct intel_crtc *crtc)
9575 struct drm_device *dev = crtc->base.dev;
9576 struct intel_encoder *encoder;
9578 list_for_each_entry(encoder,
9579 &dev->mode_config.encoder_list, base.head) {
9580 if (encoder->new_crtc != crtc)
9583 if (!check_single_encoder_cloning(crtc, encoder))
9590 static struct intel_crtc_config *
9591 intel_modeset_pipe_config(struct drm_crtc *crtc,
9592 struct drm_framebuffer *fb,
9593 struct drm_display_mode *mode)
9595 struct drm_device *dev = crtc->dev;
9596 struct intel_encoder *encoder;
9597 struct intel_crtc_config *pipe_config;
9598 int plane_bpp, ret = -EINVAL;
9601 if (!check_encoder_cloning(to_intel_crtc(crtc))) {
9602 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
9603 return ERR_PTR(-EINVAL);
9606 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
9608 return ERR_PTR(-ENOMEM);
9610 drm_mode_copy(&pipe_config->adjusted_mode, mode);
9611 drm_mode_copy(&pipe_config->requested_mode, mode);
9613 pipe_config->cpu_transcoder =
9614 (enum transcoder) to_intel_crtc(crtc)->pipe;
9615 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
9618 * Sanitize sync polarity flags based on requested ones. If neither
9619 * positive or negative polarity is requested, treat this as meaning
9620 * negative polarity.
9622 if (!(pipe_config->adjusted_mode.flags &
9623 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
9624 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
9626 if (!(pipe_config->adjusted_mode.flags &
9627 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
9628 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
9630 /* Compute a starting value for pipe_config->pipe_bpp taking the source
9631 * plane pixel format and any sink constraints into account. Returns the
9632 * source plane bpp so that dithering can be selected on mismatches
9633 * after encoders and crtc also have had their say. */
9634 plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
9640 * Determine the real pipe dimensions. Note that stereo modes can
9641 * increase the actual pipe size due to the frame doubling and
9642 * insertion of additional space for blanks between the frame. This
9643 * is stored in the crtc timings. We use the requested mode to do this
9644 * computation to clearly distinguish it from the adjusted mode, which
9645 * can be changed by the connectors in the below retry loop.
9647 drm_mode_set_crtcinfo(&pipe_config->requested_mode, CRTC_STEREO_DOUBLE);
9648 pipe_config->pipe_src_w = pipe_config->requested_mode.crtc_hdisplay;
9649 pipe_config->pipe_src_h = pipe_config->requested_mode.crtc_vdisplay;
9652 /* Ensure the port clock defaults are reset when retrying. */
9653 pipe_config->port_clock = 0;
9654 pipe_config->pixel_multiplier = 1;
9656 /* Fill in default crtc timings, allow encoders to overwrite them. */
9657 drm_mode_set_crtcinfo(&pipe_config->adjusted_mode, CRTC_STEREO_DOUBLE);
9659 /* Pass our mode to the connectors and the CRTC to give them a chance to
9660 * adjust it according to limitations or connector properties, and also
9661 * a chance to reject the mode entirely.
9663 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9666 if (&encoder->new_crtc->base != crtc)
9669 if (!(encoder->compute_config(encoder, pipe_config))) {
9670 DRM_DEBUG_KMS("Encoder config failure\n");
9675 /* Set default port clock if not overwritten by the encoder. Needs to be
9676 * done afterwards in case the encoder adjusts the mode. */
9677 if (!pipe_config->port_clock)
9678 pipe_config->port_clock = pipe_config->adjusted_mode.crtc_clock
9679 * pipe_config->pixel_multiplier;
9681 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
9683 DRM_DEBUG_KMS("CRTC fixup failed\n");
9688 if (WARN(!retry, "loop in pipe configuration computation\n")) {
9693 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
9698 pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
9699 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
9700 plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
9705 return ERR_PTR(ret);
9708 /* Computes which crtcs are affected and sets the relevant bits in the mask. For
9709 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
9711 intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
9712 unsigned *prepare_pipes, unsigned *disable_pipes)
9714 struct intel_crtc *intel_crtc;
9715 struct drm_device *dev = crtc->dev;
9716 struct intel_encoder *encoder;
9717 struct intel_connector *connector;
9718 struct drm_crtc *tmp_crtc;
9720 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
9722 /* Check which crtcs have changed outputs connected to them, these need
9723 * to be part of the prepare_pipes mask. We don't (yet) support global
9724 * modeset across multiple crtcs, so modeset_pipes will only have one
9725 * bit set at most. */
9726 list_for_each_entry(connector, &dev->mode_config.connector_list,
9728 if (connector->base.encoder == &connector->new_encoder->base)
9731 if (connector->base.encoder) {
9732 tmp_crtc = connector->base.encoder->crtc;
9734 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
9737 if (connector->new_encoder)
9739 1 << connector->new_encoder->new_crtc->pipe;
9742 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9744 if (encoder->base.crtc == &encoder->new_crtc->base)
9747 if (encoder->base.crtc) {
9748 tmp_crtc = encoder->base.crtc;
9750 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
9753 if (encoder->new_crtc)
9754 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
9757 /* Check for pipes that will be enabled/disabled ... */
9758 for_each_intel_crtc(dev, intel_crtc) {
9759 if (intel_crtc->base.enabled == intel_crtc->new_enabled)
9762 if (!intel_crtc->new_enabled)
9763 *disable_pipes |= 1 << intel_crtc->pipe;
9765 *prepare_pipes |= 1 << intel_crtc->pipe;
9769 /* set_mode is also used to update properties on life display pipes. */
9770 intel_crtc = to_intel_crtc(crtc);
9771 if (intel_crtc->new_enabled)
9772 *prepare_pipes |= 1 << intel_crtc->pipe;
9775 * For simplicity do a full modeset on any pipe where the output routing
9776 * changed. We could be more clever, but that would require us to be
9777 * more careful with calling the relevant encoder->mode_set functions.
9780 *modeset_pipes = *prepare_pipes;
9782 /* ... and mask these out. */
9783 *modeset_pipes &= ~(*disable_pipes);
9784 *prepare_pipes &= ~(*disable_pipes);
9787 * HACK: We don't (yet) fully support global modesets. intel_set_config
9788 * obies this rule, but the modeset restore mode of
9789 * intel_modeset_setup_hw_state does not.
9791 *modeset_pipes &= 1 << intel_crtc->pipe;
9792 *prepare_pipes &= 1 << intel_crtc->pipe;
9794 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
9795 *modeset_pipes, *prepare_pipes, *disable_pipes);
9798 static bool intel_crtc_in_use(struct drm_crtc *crtc)
9800 struct drm_encoder *encoder;
9801 struct drm_device *dev = crtc->dev;
9803 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
9804 if (encoder->crtc == crtc)
9811 intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
9813 struct intel_encoder *intel_encoder;
9814 struct intel_crtc *intel_crtc;
9815 struct drm_connector *connector;
9817 list_for_each_entry(intel_encoder, &dev->mode_config.encoder_list,
9819 if (!intel_encoder->base.crtc)
9822 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
9824 if (prepare_pipes & (1 << intel_crtc->pipe))
9825 intel_encoder->connectors_active = false;
9828 intel_modeset_commit_output_state(dev);
9830 /* Double check state. */
9831 for_each_intel_crtc(dev, intel_crtc) {
9832 WARN_ON(intel_crtc->base.enabled != intel_crtc_in_use(&intel_crtc->base));
9833 WARN_ON(intel_crtc->new_config &&
9834 intel_crtc->new_config != &intel_crtc->config);
9835 WARN_ON(intel_crtc->base.enabled != !!intel_crtc->new_config);
9838 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
9839 if (!connector->encoder || !connector->encoder->crtc)
9842 intel_crtc = to_intel_crtc(connector->encoder->crtc);
9844 if (prepare_pipes & (1 << intel_crtc->pipe)) {
9845 struct drm_property *dpms_property =
9846 dev->mode_config.dpms_property;
9848 connector->dpms = DRM_MODE_DPMS_ON;
9849 drm_object_property_set_value(&connector->base,
9853 intel_encoder = to_intel_encoder(connector->encoder);
9854 intel_encoder->connectors_active = true;
9860 static bool intel_fuzzy_clock_check(int clock1, int clock2)
9864 if (clock1 == clock2)
9867 if (!clock1 || !clock2)
9870 diff = abs(clock1 - clock2);
9872 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
9878 #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
9879 list_for_each_entry((intel_crtc), \
9880 &(dev)->mode_config.crtc_list, \
9882 if (mask & (1 <<(intel_crtc)->pipe))
9885 intel_pipe_config_compare(struct drm_device *dev,
9886 struct intel_crtc_config *current_config,
9887 struct intel_crtc_config *pipe_config)
9889 #define PIPE_CONF_CHECK_X(name) \
9890 if (current_config->name != pipe_config->name) { \
9891 DRM_ERROR("mismatch in " #name " " \
9892 "(expected 0x%08x, found 0x%08x)\n", \
9893 current_config->name, \
9894 pipe_config->name); \
9898 #define PIPE_CONF_CHECK_I(name) \
9899 if (current_config->name != pipe_config->name) { \
9900 DRM_ERROR("mismatch in " #name " " \
9901 "(expected %i, found %i)\n", \
9902 current_config->name, \
9903 pipe_config->name); \
9907 #define PIPE_CONF_CHECK_FLAGS(name, mask) \
9908 if ((current_config->name ^ pipe_config->name) & (mask)) { \
9909 DRM_ERROR("mismatch in " #name "(" #mask ") " \
9910 "(expected %i, found %i)\n", \
9911 current_config->name & (mask), \
9912 pipe_config->name & (mask)); \
9916 #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
9917 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
9918 DRM_ERROR("mismatch in " #name " " \
9919 "(expected %i, found %i)\n", \
9920 current_config->name, \
9921 pipe_config->name); \
9925 #define PIPE_CONF_QUIRK(quirk) \
9926 ((current_config->quirks | pipe_config->quirks) & (quirk))
9928 PIPE_CONF_CHECK_I(cpu_transcoder);
9930 PIPE_CONF_CHECK_I(has_pch_encoder);
9931 PIPE_CONF_CHECK_I(fdi_lanes);
9932 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
9933 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
9934 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
9935 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
9936 PIPE_CONF_CHECK_I(fdi_m_n.tu);
9938 PIPE_CONF_CHECK_I(has_dp_encoder);
9939 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
9940 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
9941 PIPE_CONF_CHECK_I(dp_m_n.link_m);
9942 PIPE_CONF_CHECK_I(dp_m_n.link_n);
9943 PIPE_CONF_CHECK_I(dp_m_n.tu);
9945 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
9946 PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
9947 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
9948 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
9949 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
9950 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
9952 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
9953 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
9954 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
9955 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
9956 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
9957 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
9959 PIPE_CONF_CHECK_I(pixel_multiplier);
9960 PIPE_CONF_CHECK_I(has_hdmi_sink);
9961 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
9963 PIPE_CONF_CHECK_I(limited_color_range);
9965 PIPE_CONF_CHECK_I(has_audio);
9967 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
9968 DRM_MODE_FLAG_INTERLACE);
9970 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
9971 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
9972 DRM_MODE_FLAG_PHSYNC);
9973 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
9974 DRM_MODE_FLAG_NHSYNC);
9975 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
9976 DRM_MODE_FLAG_PVSYNC);
9977 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
9978 DRM_MODE_FLAG_NVSYNC);
9981 PIPE_CONF_CHECK_I(pipe_src_w);
9982 PIPE_CONF_CHECK_I(pipe_src_h);
9985 * FIXME: BIOS likes to set up a cloned config with lvds+external
9986 * screen. Since we don't yet re-compute the pipe config when moving
9987 * just the lvds port away to another pipe the sw tracking won't match.
9989 * Proper atomic modesets with recomputed global state will fix this.
9990 * Until then just don't check gmch state for inherited modes.
9992 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
9993 PIPE_CONF_CHECK_I(gmch_pfit.control);
9994 /* pfit ratios are autocomputed by the hw on gen4+ */
9995 if (INTEL_INFO(dev)->gen < 4)
9996 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
9997 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
10000 PIPE_CONF_CHECK_I(pch_pfit.enabled);
10001 if (current_config->pch_pfit.enabled) {
10002 PIPE_CONF_CHECK_I(pch_pfit.pos);
10003 PIPE_CONF_CHECK_I(pch_pfit.size);
10006 /* BDW+ don't expose a synchronous way to read the state */
10007 if (IS_HASWELL(dev))
10008 PIPE_CONF_CHECK_I(ips_enabled);
10010 PIPE_CONF_CHECK_I(double_wide);
10012 PIPE_CONF_CHECK_I(shared_dpll);
10013 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
10014 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
10015 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
10016 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
10018 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
10019 PIPE_CONF_CHECK_I(pipe_bpp);
10021 PIPE_CONF_CHECK_CLOCK_FUZZY(adjusted_mode.crtc_clock);
10022 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
10024 #undef PIPE_CONF_CHECK_X
10025 #undef PIPE_CONF_CHECK_I
10026 #undef PIPE_CONF_CHECK_FLAGS
10027 #undef PIPE_CONF_CHECK_CLOCK_FUZZY
10028 #undef PIPE_CONF_QUIRK
10034 check_connector_state(struct drm_device *dev)
10036 struct intel_connector *connector;
10038 list_for_each_entry(connector, &dev->mode_config.connector_list,
10040 /* This also checks the encoder/connector hw state with the
10041 * ->get_hw_state callbacks. */
10042 intel_connector_check_state(connector);
10044 WARN(&connector->new_encoder->base != connector->base.encoder,
10045 "connector's staged encoder doesn't match current encoder\n");
10050 check_encoder_state(struct drm_device *dev)
10052 struct intel_encoder *encoder;
10053 struct intel_connector *connector;
10055 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
10057 bool enabled = false;
10058 bool active = false;
10059 enum i915_pipe pipe, tracked_pipe;
10061 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
10062 encoder->base.base.id,
10063 encoder->base.name);
10065 WARN(&encoder->new_crtc->base != encoder->base.crtc,
10066 "encoder's stage crtc doesn't match current crtc\n");
10067 WARN(encoder->connectors_active && !encoder->base.crtc,
10068 "encoder's active_connectors set, but no crtc\n");
10070 list_for_each_entry(connector, &dev->mode_config.connector_list,
10072 if (connector->base.encoder != &encoder->base)
10075 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
10078 WARN(!!encoder->base.crtc != enabled,
10079 "encoder's enabled state mismatch "
10080 "(expected %i, found %i)\n",
10081 !!encoder->base.crtc, enabled);
10082 WARN(active && !encoder->base.crtc,
10083 "active encoder with no crtc\n");
10085 WARN(encoder->connectors_active != active,
10086 "encoder's computed active state doesn't match tracked active state "
10087 "(expected %i, found %i)\n", active, encoder->connectors_active);
10089 active = encoder->get_hw_state(encoder, &pipe);
10090 WARN(active != encoder->connectors_active,
10091 "encoder's hw state doesn't match sw tracking "
10092 "(expected %i, found %i)\n",
10093 encoder->connectors_active, active);
10095 if (!encoder->base.crtc)
10098 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
10099 WARN(active && pipe != tracked_pipe,
10100 "active encoder's pipe doesn't match"
10101 "(expected %i, found %i)\n",
10102 tracked_pipe, pipe);
10108 check_crtc_state(struct drm_device *dev)
10110 struct drm_i915_private *dev_priv = dev->dev_private;
10111 struct intel_crtc *crtc;
10112 struct intel_encoder *encoder;
10113 struct intel_crtc_config pipe_config;
10115 for_each_intel_crtc(dev, crtc) {
10116 bool enabled = false;
10117 bool active = false;
10119 memset(&pipe_config, 0, sizeof(pipe_config));
10121 DRM_DEBUG_KMS("[CRTC:%d]\n",
10122 crtc->base.base.id);
10124 WARN(crtc->active && !crtc->base.enabled,
10125 "active crtc, but not enabled in sw tracking\n");
10127 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
10129 if (encoder->base.crtc != &crtc->base)
10132 if (encoder->connectors_active)
10136 WARN(active != crtc->active,
10137 "crtc's computed active state doesn't match tracked active state "
10138 "(expected %i, found %i)\n", active, crtc->active);
10139 WARN(enabled != crtc->base.enabled,
10140 "crtc's computed enabled state doesn't match tracked enabled state "
10141 "(expected %i, found %i)\n", enabled, crtc->base.enabled);
10143 active = dev_priv->display.get_pipe_config(crtc,
10146 /* hw state is inconsistent with the pipe A quirk */
10147 if (crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
10148 active = crtc->active;
10150 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
10152 enum i915_pipe pipe;
10153 if (encoder->base.crtc != &crtc->base)
10155 if (encoder->get_hw_state(encoder, &pipe))
10156 encoder->get_config(encoder, &pipe_config);
10159 WARN(crtc->active != active,
10160 "crtc active state doesn't match with hw state "
10161 "(expected %i, found %i)\n", crtc->active, active);
10164 !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
10165 WARN(1, "pipe state doesn't match!\n");
10166 intel_dump_pipe_config(crtc, &pipe_config,
10168 intel_dump_pipe_config(crtc, &crtc->config,
10175 check_shared_dpll_state(struct drm_device *dev)
10177 struct drm_i915_private *dev_priv = dev->dev_private;
10178 struct intel_crtc *crtc;
10179 struct intel_dpll_hw_state dpll_hw_state;
10182 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
10183 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
10184 int enabled_crtcs = 0, active_crtcs = 0;
10187 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
10189 DRM_DEBUG_KMS("%s\n", pll->name);
10191 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
10193 WARN(pll->active > pll->refcount,
10194 "more active pll users than references: %i vs %i\n",
10195 pll->active, pll->refcount);
10196 WARN(pll->active && !pll->on,
10197 "pll in active use but not on in sw tracking\n");
10198 WARN(pll->on && !pll->active,
10199 "pll in on but not on in use in sw tracking\n");
10200 WARN(pll->on != active,
10201 "pll on state mismatch (expected %i, found %i)\n",
10204 for_each_intel_crtc(dev, crtc) {
10205 if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
10207 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
10210 WARN(pll->active != active_crtcs,
10211 "pll active crtcs mismatch (expected %i, found %i)\n",
10212 pll->active, active_crtcs);
10213 WARN(pll->refcount != enabled_crtcs,
10214 "pll enabled crtcs mismatch (expected %i, found %i)\n",
10215 pll->refcount, enabled_crtcs);
10217 WARN(pll->on && memcmp(&pll->hw_state, &dpll_hw_state,
10218 sizeof(dpll_hw_state)),
10219 "pll hw state mismatch\n");
10224 intel_modeset_check_state(struct drm_device *dev)
10226 check_connector_state(dev);
10227 check_encoder_state(dev);
10228 check_crtc_state(dev);
10229 check_shared_dpll_state(dev);
10232 void ironlake_check_encoder_dotclock(const struct intel_crtc_config *pipe_config,
10236 * FDI already provided one idea for the dotclock.
10237 * Yell if the encoder disagrees.
10239 WARN(!intel_fuzzy_clock_check(pipe_config->adjusted_mode.crtc_clock, dotclock),
10240 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
10241 pipe_config->adjusted_mode.crtc_clock, dotclock);
10244 static void update_scanline_offset(struct intel_crtc *crtc)
10246 struct drm_device *dev = crtc->base.dev;
10249 * The scanline counter increments at the leading edge of hsync.
10251 * On most platforms it starts counting from vtotal-1 on the
10252 * first active line. That means the scanline counter value is
10253 * always one less than what we would expect. Ie. just after
10254 * start of vblank, which also occurs at start of hsync (on the
10255 * last active line), the scanline counter will read vblank_start-1.
10257 * On gen2 the scanline counter starts counting from 1 instead
10258 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
10259 * to keep the value positive), instead of adding one.
10261 * On HSW+ the behaviour of the scanline counter depends on the output
10262 * type. For DP ports it behaves like most other platforms, but on HDMI
10263 * there's an extra 1 line difference. So we need to add two instead of
10264 * one to the value.
10266 if (IS_GEN2(dev)) {
10267 const struct drm_display_mode *mode = &crtc->config.adjusted_mode;
10270 vtotal = mode->crtc_vtotal;
10271 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
10274 crtc->scanline_offset = vtotal - 1;
10275 } else if (HAS_DDI(dev) &&
10276 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI)) {
10277 crtc->scanline_offset = 2;
10279 crtc->scanline_offset = 1;
10282 static int __intel_set_mode(struct drm_crtc *crtc,
10283 struct drm_display_mode *mode,
10284 int x, int y, struct drm_framebuffer *fb)
10286 struct drm_device *dev = crtc->dev;
10287 struct drm_i915_private *dev_priv = dev->dev_private;
10288 struct drm_display_mode *saved_mode;
10289 struct intel_crtc_config *pipe_config = NULL;
10290 struct intel_crtc *intel_crtc;
10291 unsigned disable_pipes, prepare_pipes, modeset_pipes;
10294 saved_mode = kmalloc(sizeof(*saved_mode), M_DRM, M_WAITOK);
10298 intel_modeset_affected_pipes(crtc, &modeset_pipes,
10299 &prepare_pipes, &disable_pipes);
10301 *saved_mode = crtc->mode;
10303 /* Hack: Because we don't (yet) support global modeset on multiple
10304 * crtcs, we don't keep track of the new mode for more than one crtc.
10305 * Hence simply check whether any bit is set in modeset_pipes in all the
10306 * pieces of code that are not yet converted to deal with mutliple crtcs
10307 * changing their mode at the same time. */
10308 if (modeset_pipes) {
10309 pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
10310 if (IS_ERR(pipe_config)) {
10311 ret = PTR_ERR(pipe_config);
10312 pipe_config = NULL;
10316 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
10318 to_intel_crtc(crtc)->new_config = pipe_config;
10322 * See if the config requires any additional preparation, e.g.
10323 * to adjust global state with pipes off. We need to do this
10324 * here so we can get the modeset_pipe updated config for the new
10325 * mode set on this crtc. For other crtcs we need to use the
10326 * adjusted_mode bits in the crtc directly.
10328 if (IS_VALLEYVIEW(dev)) {
10329 valleyview_modeset_global_pipes(dev, &prepare_pipes);
10331 /* may have added more to prepare_pipes than we should */
10332 prepare_pipes &= ~disable_pipes;
10335 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
10336 intel_crtc_disable(&intel_crtc->base);
10338 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
10339 if (intel_crtc->base.enabled)
10340 dev_priv->display.crtc_disable(&intel_crtc->base);
10343 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
10344 * to set it here already despite that we pass it down the callchain.
10346 if (modeset_pipes) {
10347 crtc->mode = *mode;
10348 /* mode_set/enable/disable functions rely on a correct pipe
10350 to_intel_crtc(crtc)->config = *pipe_config;
10351 to_intel_crtc(crtc)->new_config = &to_intel_crtc(crtc)->config;
10354 * Calculate and store various constants which
10355 * are later needed by vblank and swap-completion
10356 * timestamping. They are derived from true hwmode.
10358 drm_calc_timestamping_constants(crtc,
10359 &pipe_config->adjusted_mode);
10362 /* Only after disabling all output pipelines that will be changed can we
10363 * update the the output configuration. */
10364 intel_modeset_update_state(dev, prepare_pipes);
10366 if (dev_priv->display.modeset_global_resources)
10367 dev_priv->display.modeset_global_resources(dev);
10369 /* Set up the DPLL and any encoders state that needs to adjust or depend
10372 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
10373 struct drm_framebuffer *old_fb;
10375 mutex_lock(&dev->struct_mutex);
10376 ret = intel_pin_and_fence_fb_obj(dev,
10377 to_intel_framebuffer(fb)->obj,
10380 DRM_ERROR("pin & fence failed\n");
10381 mutex_unlock(&dev->struct_mutex);
10384 old_fb = crtc->primary->fb;
10386 intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
10387 mutex_unlock(&dev->struct_mutex);
10389 crtc->primary->fb = fb;
10393 ret = dev_priv->display.crtc_mode_set(&intel_crtc->base,
10399 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
10400 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
10401 update_scanline_offset(intel_crtc);
10403 dev_priv->display.crtc_enable(&intel_crtc->base);
10406 /* FIXME: add subpixel order */
10408 if (ret && crtc->enabled)
10409 crtc->mode = *saved_mode;
10412 kfree(pipe_config);
10417 static int intel_set_mode(struct drm_crtc *crtc,
10418 struct drm_display_mode *mode,
10419 int x, int y, struct drm_framebuffer *fb)
10423 ret = __intel_set_mode(crtc, mode, x, y, fb);
10426 intel_modeset_check_state(crtc->dev);
10431 void intel_crtc_restore_mode(struct drm_crtc *crtc)
10433 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb);
10436 #undef for_each_intel_crtc_masked
10438 static void intel_set_config_free(struct intel_set_config *config)
10443 kfree(config->save_connector_encoders);
10444 kfree(config->save_encoder_crtcs);
10445 kfree(config->save_crtc_enabled);
10449 static int intel_set_config_save_state(struct drm_device *dev,
10450 struct intel_set_config *config)
10452 struct drm_crtc *crtc;
10453 struct drm_encoder *encoder;
10454 struct drm_connector *connector;
10457 config->save_crtc_enabled =
10458 kcalloc(dev->mode_config.num_crtc,
10459 sizeof(bool), GFP_KERNEL);
10460 if (!config->save_crtc_enabled)
10463 config->save_encoder_crtcs =
10464 kcalloc(dev->mode_config.num_encoder,
10465 sizeof(struct drm_crtc *), GFP_KERNEL);
10466 if (!config->save_encoder_crtcs)
10469 config->save_connector_encoders =
10470 kcalloc(dev->mode_config.num_connector,
10471 sizeof(struct drm_encoder *), GFP_KERNEL);
10472 if (!config->save_connector_encoders)
10475 /* Copy data. Note that driver private data is not affected.
10476 * Should anything bad happen only the expected state is
10477 * restored, not the drivers personal bookkeeping.
10480 for_each_crtc(dev, crtc) {
10481 config->save_crtc_enabled[count++] = crtc->enabled;
10485 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
10486 config->save_encoder_crtcs[count++] = encoder->crtc;
10490 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
10491 config->save_connector_encoders[count++] = connector->encoder;
10497 static void intel_set_config_restore_state(struct drm_device *dev,
10498 struct intel_set_config *config)
10500 struct intel_crtc *crtc;
10501 struct intel_encoder *encoder;
10502 struct intel_connector *connector;
10506 for_each_intel_crtc(dev, crtc) {
10507 crtc->new_enabled = config->save_crtc_enabled[count++];
10509 if (crtc->new_enabled)
10510 crtc->new_config = &crtc->config;
10512 crtc->new_config = NULL;
10516 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
10517 encoder->new_crtc =
10518 to_intel_crtc(config->save_encoder_crtcs[count++]);
10522 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
10523 connector->new_encoder =
10524 to_intel_encoder(config->save_connector_encoders[count++]);
10529 is_crtc_connector_off(struct drm_mode_set *set)
10533 if (set->num_connectors == 0)
10536 if (WARN_ON(set->connectors == NULL))
10539 for (i = 0; i < set->num_connectors; i++)
10540 if (set->connectors[i]->encoder &&
10541 set->connectors[i]->encoder->crtc == set->crtc &&
10542 set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
10549 intel_set_config_compute_mode_changes(struct drm_mode_set *set,
10550 struct intel_set_config *config)
10553 /* We should be able to check here if the fb has the same properties
10554 * and then just flip_or_move it */
10555 if (is_crtc_connector_off(set)) {
10556 config->mode_changed = true;
10557 } else if (set->crtc->primary->fb != set->fb) {
10558 /* If we have no fb then treat it as a full mode set */
10559 if (set->crtc->primary->fb == NULL) {
10560 struct intel_crtc *intel_crtc =
10561 to_intel_crtc(set->crtc);
10563 if (intel_crtc->active && i915.fastboot) {
10564 DRM_DEBUG_KMS("crtc has no fb, will flip\n");
10565 config->fb_changed = true;
10567 DRM_DEBUG_KMS("inactive crtc, full mode set\n");
10568 config->mode_changed = true;
10570 } else if (set->fb == NULL) {
10571 config->mode_changed = true;
10572 } else if (set->fb->pixel_format !=
10573 set->crtc->primary->fb->pixel_format) {
10574 config->mode_changed = true;
10576 config->fb_changed = true;
10580 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
10581 config->fb_changed = true;
10583 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
10584 DRM_DEBUG_KMS("modes are different, full mode set\n");
10585 drm_mode_debug_printmodeline(&set->crtc->mode);
10586 drm_mode_debug_printmodeline(set->mode);
10587 config->mode_changed = true;
10590 DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
10591 set->crtc->base.id, config->mode_changed, config->fb_changed);
10595 intel_modeset_stage_output_state(struct drm_device *dev,
10596 struct drm_mode_set *set,
10597 struct intel_set_config *config)
10599 struct intel_connector *connector;
10600 struct intel_encoder *encoder;
10601 struct intel_crtc *crtc;
10604 /* The upper layers ensure that we either disable a crtc or have a list
10605 * of connectors. For paranoia, double-check this. */
10606 WARN_ON(!set->fb && (set->num_connectors != 0));
10607 WARN_ON(set->fb && (set->num_connectors == 0));
10609 list_for_each_entry(connector, &dev->mode_config.connector_list,
10611 /* Otherwise traverse passed in connector list and get encoders
10613 for (ro = 0; ro < set->num_connectors; ro++) {
10614 if (set->connectors[ro] == &connector->base) {
10615 connector->new_encoder = connector->encoder;
10620 /* If we disable the crtc, disable all its connectors. Also, if
10621 * the connector is on the changing crtc but not on the new
10622 * connector list, disable it. */
10623 if ((!set->fb || ro == set->num_connectors) &&
10624 connector->base.encoder &&
10625 connector->base.encoder->crtc == set->crtc) {
10626 connector->new_encoder = NULL;
10628 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
10629 connector->base.base.id,
10630 connector->base.name);
10634 if (&connector->new_encoder->base != connector->base.encoder) {
10635 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
10636 config->mode_changed = true;
10639 /* connector->new_encoder is now updated for all connectors. */
10641 /* Update crtc of enabled connectors. */
10642 list_for_each_entry(connector, &dev->mode_config.connector_list,
10644 struct drm_crtc *new_crtc;
10646 if (!connector->new_encoder)
10649 new_crtc = connector->new_encoder->base.crtc;
10651 for (ro = 0; ro < set->num_connectors; ro++) {
10652 if (set->connectors[ro] == &connector->base)
10653 new_crtc = set->crtc;
10656 /* Make sure the new CRTC will work with the encoder */
10657 if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
10661 connector->encoder->new_crtc = to_intel_crtc(new_crtc);
10663 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
10664 connector->base.base.id,
10665 connector->base.name,
10666 new_crtc->base.id);
10669 /* Check for any encoders that needs to be disabled. */
10670 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
10672 int num_connectors = 0;
10673 list_for_each_entry(connector,
10674 &dev->mode_config.connector_list,
10676 if (connector->new_encoder == encoder) {
10677 WARN_ON(!connector->new_encoder->new_crtc);
10682 if (num_connectors == 0)
10683 encoder->new_crtc = NULL;
10684 else if (num_connectors > 1)
10687 /* Only now check for crtc changes so we don't miss encoders
10688 * that will be disabled. */
10689 if (&encoder->new_crtc->base != encoder->base.crtc) {
10690 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
10691 config->mode_changed = true;
10694 /* Now we've also updated encoder->new_crtc for all encoders. */
10696 for_each_intel_crtc(dev, crtc) {
10697 crtc->new_enabled = false;
10699 list_for_each_entry(encoder,
10700 &dev->mode_config.encoder_list,
10702 if (encoder->new_crtc == crtc) {
10703 crtc->new_enabled = true;
10708 if (crtc->new_enabled != crtc->base.enabled) {
10709 DRM_DEBUG_KMS("crtc %sabled, full mode switch\n",
10710 crtc->new_enabled ? "en" : "dis");
10711 config->mode_changed = true;
10714 if (crtc->new_enabled)
10715 crtc->new_config = &crtc->config;
10717 crtc->new_config = NULL;
10723 static void disable_crtc_nofb(struct intel_crtc *crtc)
10725 struct drm_device *dev = crtc->base.dev;
10726 struct intel_encoder *encoder;
10727 struct intel_connector *connector;
10729 DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
10730 pipe_name(crtc->pipe));
10732 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
10733 if (connector->new_encoder &&
10734 connector->new_encoder->new_crtc == crtc)
10735 connector->new_encoder = NULL;
10738 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
10739 if (encoder->new_crtc == crtc)
10740 encoder->new_crtc = NULL;
10743 crtc->new_enabled = false;
10744 crtc->new_config = NULL;
10747 static int intel_crtc_set_config(struct drm_mode_set *set)
10749 struct drm_device *dev;
10750 struct drm_mode_set save_set;
10751 struct intel_set_config *config;
10755 BUG_ON(!set->crtc);
10756 BUG_ON(!set->crtc->helper_private);
10758 /* Enforce sane interface api - has been abused by the fb helper. */
10759 BUG_ON(!set->mode && set->fb);
10760 BUG_ON(set->fb && set->num_connectors == 0);
10763 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
10764 set->crtc->base.id, set->fb->base.id,
10765 (int)set->num_connectors, set->x, set->y);
10767 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
10770 dev = set->crtc->dev;
10773 config = kzalloc(sizeof(*config), GFP_KERNEL);
10777 ret = intel_set_config_save_state(dev, config);
10781 save_set.crtc = set->crtc;
10782 save_set.mode = &set->crtc->mode;
10783 save_set.x = set->crtc->x;
10784 save_set.y = set->crtc->y;
10785 save_set.fb = set->crtc->primary->fb;
10787 /* Compute whether we need a full modeset, only an fb base update or no
10788 * change at all. In the future we might also check whether only the
10789 * mode changed, e.g. for LVDS where we only change the panel fitter in
10791 intel_set_config_compute_mode_changes(set, config);
10793 ret = intel_modeset_stage_output_state(dev, set, config);
10797 if (config->mode_changed) {
10798 ret = intel_set_mode(set->crtc, set->mode,
10799 set->x, set->y, set->fb);
10800 } else if (config->fb_changed) {
10801 intel_crtc_wait_for_pending_flips(set->crtc);
10803 ret = intel_pipe_set_base(set->crtc,
10804 set->x, set->y, set->fb);
10806 * In the fastboot case this may be our only check of the
10807 * state after boot. It would be better to only do it on
10808 * the first update, but we don't have a nice way of doing that
10809 * (and really, set_config isn't used much for high freq page
10810 * flipping, so increasing its cost here shouldn't be a big
10813 if (i915.fastboot && ret == 0)
10814 intel_modeset_check_state(set->crtc->dev);
10818 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
10819 set->crtc->base.id, ret);
10821 intel_set_config_restore_state(dev, config);
10824 * HACK: if the pipe was on, but we didn't have a framebuffer,
10825 * force the pipe off to avoid oopsing in the modeset code
10826 * due to fb==NULL. This should only happen during boot since
10827 * we don't yet reconstruct the FB from the hardware state.
10829 if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
10830 disable_crtc_nofb(to_intel_crtc(save_set.crtc));
10832 /* Try to restore the config */
10833 if (config->mode_changed &&
10834 intel_set_mode(save_set.crtc, save_set.mode,
10835 save_set.x, save_set.y, save_set.fb))
10836 DRM_ERROR("failed to restore config after modeset failure\n");
10840 intel_set_config_free(config);
10844 static const struct drm_crtc_funcs intel_crtc_funcs = {
10845 .cursor_set = intel_crtc_cursor_set,
10846 .cursor_move = intel_crtc_cursor_move,
10847 .gamma_set = intel_crtc_gamma_set,
10848 .set_config = intel_crtc_set_config,
10849 .destroy = intel_crtc_destroy,
10850 .page_flip = intel_crtc_page_flip,
10853 static void intel_cpu_pll_init(struct drm_device *dev)
10856 intel_ddi_pll_init(dev);
10859 static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
10860 struct intel_shared_dpll *pll,
10861 struct intel_dpll_hw_state *hw_state)
10865 val = I915_READ(PCH_DPLL(pll->id));
10866 hw_state->dpll = val;
10867 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
10868 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
10870 return val & DPLL_VCO_ENABLE;
10873 static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
10874 struct intel_shared_dpll *pll)
10876 I915_WRITE(PCH_FP0(pll->id), pll->hw_state.fp0);
10877 I915_WRITE(PCH_FP1(pll->id), pll->hw_state.fp1);
10880 static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
10881 struct intel_shared_dpll *pll)
10883 /* PCH refclock must be enabled first */
10884 ibx_assert_pch_refclk_enabled(dev_priv);
10886 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
10888 /* Wait for the clocks to stabilize. */
10889 POSTING_READ(PCH_DPLL(pll->id));
10892 /* The pixel multiplier can only be updated once the
10893 * DPLL is enabled and the clocks are stable.
10895 * So write it again.
10897 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
10898 POSTING_READ(PCH_DPLL(pll->id));
10902 static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
10903 struct intel_shared_dpll *pll)
10905 struct drm_device *dev = dev_priv->dev;
10906 struct intel_crtc *crtc;
10908 /* Make sure no transcoder isn't still depending on us. */
10909 for_each_intel_crtc(dev, crtc) {
10910 if (intel_crtc_to_shared_dpll(crtc) == pll)
10911 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
10914 I915_WRITE(PCH_DPLL(pll->id), 0);
10915 POSTING_READ(PCH_DPLL(pll->id));
10919 static char *ibx_pch_dpll_names[] = {
10924 static void ibx_pch_dpll_init(struct drm_device *dev)
10926 struct drm_i915_private *dev_priv = dev->dev_private;
10929 dev_priv->num_shared_dpll = 2;
10931 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
10932 dev_priv->shared_dplls[i].id = i;
10933 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
10934 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
10935 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
10936 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
10937 dev_priv->shared_dplls[i].get_hw_state =
10938 ibx_pch_dpll_get_hw_state;
10942 static void intel_shared_dpll_init(struct drm_device *dev)
10944 struct drm_i915_private *dev_priv = dev->dev_private;
10946 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
10947 ibx_pch_dpll_init(dev);
10949 dev_priv->num_shared_dpll = 0;
10951 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
10954 static void intel_crtc_init(struct drm_device *dev, int pipe)
10956 struct drm_i915_private *dev_priv = dev->dev_private;
10957 struct intel_crtc *intel_crtc;
10960 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
10961 if (intel_crtc == NULL)
10964 drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
10966 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
10967 for (i = 0; i < 256; i++) {
10968 intel_crtc->lut_r[i] = i;
10969 intel_crtc->lut_g[i] = i;
10970 intel_crtc->lut_b[i] = i;
10974 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
10975 * is hooked to plane B. Hence we want plane A feeding pipe B.
10977 intel_crtc->pipe = pipe;
10978 intel_crtc->plane = pipe;
10979 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
10980 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
10981 intel_crtc->plane = !pipe;
10984 intel_crtc->cursor_base = ~0;
10985 intel_crtc->cursor_cntl = ~0;
10987 init_waitqueue_head(&intel_crtc->vbl_wait);
10989 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
10990 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
10991 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
10992 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
10994 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
10996 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
10999 enum i915_pipe intel_get_pipe_from_connector(struct intel_connector *connector)
11001 struct drm_encoder *encoder = connector->base.encoder;
11002 struct drm_device *dev = connector->base.dev;
11004 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
11007 return INVALID_PIPE;
11009 return to_intel_crtc(encoder->crtc)->pipe;
11012 int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
11013 struct drm_file *file)
11015 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
11016 struct drm_mode_object *drmmode_obj;
11017 struct intel_crtc *crtc;
11019 if (!drm_core_check_feature(dev, DRIVER_MODESET))
11022 drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
11023 DRM_MODE_OBJECT_CRTC);
11025 if (!drmmode_obj) {
11026 DRM_ERROR("no such CRTC id\n");
11030 crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
11031 pipe_from_crtc_id->pipe = crtc->pipe;
11036 static int intel_encoder_clones(struct intel_encoder *encoder)
11038 struct drm_device *dev = encoder->base.dev;
11039 struct intel_encoder *source_encoder;
11040 int index_mask = 0;
11043 list_for_each_entry(source_encoder,
11044 &dev->mode_config.encoder_list, base.head) {
11045 if (encoders_cloneable(encoder, source_encoder))
11046 index_mask |= (1 << entry);
11054 static bool has_edp_a(struct drm_device *dev)
11056 struct drm_i915_private *dev_priv = dev->dev_private;
11058 if (!IS_MOBILE(dev))
11061 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
11064 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
11070 const char *intel_output_name(int output)
11072 static const char *names[] = {
11073 [INTEL_OUTPUT_UNUSED] = "Unused",
11074 [INTEL_OUTPUT_ANALOG] = "Analog",
11075 [INTEL_OUTPUT_DVO] = "DVO",
11076 [INTEL_OUTPUT_SDVO] = "SDVO",
11077 [INTEL_OUTPUT_LVDS] = "LVDS",
11078 [INTEL_OUTPUT_TVOUT] = "TV",
11079 [INTEL_OUTPUT_HDMI] = "HDMI",
11080 [INTEL_OUTPUT_DISPLAYPORT] = "DisplayPort",
11081 [INTEL_OUTPUT_EDP] = "eDP",
11082 [INTEL_OUTPUT_DSI] = "DSI",
11083 [INTEL_OUTPUT_UNKNOWN] = "Unknown",
11086 if (output < 0 || output >= ARRAY_SIZE(names) || !names[output])
11089 return names[output];
11092 static bool intel_crt_present(struct drm_device *dev)
11094 struct drm_i915_private *dev_priv = dev->dev_private;
11099 if (IS_CHERRYVIEW(dev))
11102 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
11108 static void intel_setup_outputs(struct drm_device *dev)
11110 struct drm_i915_private *dev_priv = dev->dev_private;
11111 struct intel_encoder *encoder;
11112 bool dpd_is_edp = false;
11114 intel_lvds_init(dev);
11116 if (intel_crt_present(dev))
11117 intel_crt_init(dev);
11119 if (HAS_DDI(dev)) {
11122 /* Haswell uses DDI functions to detect digital outputs */
11123 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
11124 /* DDI A only supports eDP */
11126 intel_ddi_init(dev, PORT_A);
11128 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
11130 found = I915_READ(SFUSE_STRAP);
11132 if (found & SFUSE_STRAP_DDIB_DETECTED)
11133 intel_ddi_init(dev, PORT_B);
11134 if (found & SFUSE_STRAP_DDIC_DETECTED)
11135 intel_ddi_init(dev, PORT_C);
11136 if (found & SFUSE_STRAP_DDID_DETECTED)
11137 intel_ddi_init(dev, PORT_D);
11138 } else if (HAS_PCH_SPLIT(dev)) {
11140 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
11142 if (has_edp_a(dev))
11143 intel_dp_init(dev, DP_A, PORT_A);
11145 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
11146 /* PCH SDVOB multiplex with HDMIB */
11147 found = intel_sdvo_init(dev, PCH_SDVOB, true);
11149 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
11150 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
11151 intel_dp_init(dev, PCH_DP_B, PORT_B);
11154 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
11155 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
11157 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
11158 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
11160 if (I915_READ(PCH_DP_C) & DP_DETECTED)
11161 intel_dp_init(dev, PCH_DP_C, PORT_C);
11163 if (I915_READ(PCH_DP_D) & DP_DETECTED)
11164 intel_dp_init(dev, PCH_DP_D, PORT_D);
11165 } else if (IS_VALLEYVIEW(dev)) {
11166 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED) {
11167 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
11169 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED)
11170 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
11173 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED) {
11174 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
11176 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED)
11177 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
11180 if (IS_CHERRYVIEW(dev)) {
11181 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED) {
11182 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
11184 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
11185 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
11189 intel_dsi_init(dev);
11190 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
11191 bool found = false;
11193 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
11194 DRM_DEBUG_KMS("probing SDVOB\n");
11195 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
11196 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
11197 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
11198 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
11201 if (!found && SUPPORTS_INTEGRATED_DP(dev))
11202 intel_dp_init(dev, DP_B, PORT_B);
11205 /* Before G4X SDVOC doesn't have its own detect register */
11207 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
11208 DRM_DEBUG_KMS("probing SDVOC\n");
11209 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
11212 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
11214 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
11215 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
11216 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
11218 if (SUPPORTS_INTEGRATED_DP(dev))
11219 intel_dp_init(dev, DP_C, PORT_C);
11222 if (SUPPORTS_INTEGRATED_DP(dev) &&
11223 (I915_READ(DP_D) & DP_DETECTED))
11224 intel_dp_init(dev, DP_D, PORT_D);
11226 } else if (IS_GEN2(dev))
11227 intel_dvo_init(dev);
11231 if (SUPPORTS_TV(dev))
11232 intel_tv_init(dev);
11234 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
11235 encoder->base.possible_crtcs = encoder->crtc_mask;
11236 encoder->base.possible_clones =
11237 intel_encoder_clones(encoder);
11240 intel_init_pch_refclk(dev);
11242 drm_helper_move_panel_connectors_to_head(dev);
11245 static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
11247 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
11249 drm_framebuffer_cleanup(fb);
11250 WARN_ON(!intel_fb->obj->framebuffer_references--);
11251 drm_gem_object_unreference_unlocked(&intel_fb->obj->base);
11255 static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
11256 struct drm_file *file,
11257 unsigned int *handle)
11259 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
11260 struct drm_i915_gem_object *obj = intel_fb->obj;
11262 return drm_gem_handle_create(file, &obj->base, handle);
11265 static const struct drm_framebuffer_funcs intel_fb_funcs = {
11266 .destroy = intel_user_framebuffer_destroy,
11267 .create_handle = intel_user_framebuffer_create_handle,
11270 static int intel_framebuffer_init(struct drm_device *dev,
11271 struct intel_framebuffer *intel_fb,
11272 struct drm_mode_fb_cmd2 *mode_cmd,
11273 struct drm_i915_gem_object *obj)
11275 int aligned_height;
11279 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
11281 if (obj->tiling_mode == I915_TILING_Y) {
11282 DRM_DEBUG("hardware does not support tiling Y\n");
11286 if (mode_cmd->pitches[0] & 63) {
11287 DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
11288 mode_cmd->pitches[0]);
11292 if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
11293 pitch_limit = 32*1024;
11294 } else if (INTEL_INFO(dev)->gen >= 4) {
11295 if (obj->tiling_mode)
11296 pitch_limit = 16*1024;
11298 pitch_limit = 32*1024;
11299 } else if (INTEL_INFO(dev)->gen >= 3) {
11300 if (obj->tiling_mode)
11301 pitch_limit = 8*1024;
11303 pitch_limit = 16*1024;
11305 /* XXX DSPC is limited to 4k tiled */
11306 pitch_limit = 8*1024;
11308 if (mode_cmd->pitches[0] > pitch_limit) {
11309 DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
11310 obj->tiling_mode ? "tiled" : "linear",
11311 mode_cmd->pitches[0], pitch_limit);
11315 if (obj->tiling_mode != I915_TILING_NONE &&
11316 mode_cmd->pitches[0] != obj->stride) {
11317 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
11318 mode_cmd->pitches[0], obj->stride);
11322 /* Reject formats not supported by any plane early. */
11323 switch (mode_cmd->pixel_format) {
11324 case DRM_FORMAT_C8:
11325 case DRM_FORMAT_RGB565:
11326 case DRM_FORMAT_XRGB8888:
11327 case DRM_FORMAT_ARGB8888:
11329 case DRM_FORMAT_XRGB1555:
11330 case DRM_FORMAT_ARGB1555:
11331 if (INTEL_INFO(dev)->gen > 3) {
11332 DRM_DEBUG("unsupported pixel format: %s\n",
11333 drm_get_format_name(mode_cmd->pixel_format));
11337 case DRM_FORMAT_XBGR8888:
11338 case DRM_FORMAT_ABGR8888:
11339 case DRM_FORMAT_XRGB2101010:
11340 case DRM_FORMAT_ARGB2101010:
11341 case DRM_FORMAT_XBGR2101010:
11342 case DRM_FORMAT_ABGR2101010:
11343 if (INTEL_INFO(dev)->gen < 4) {
11344 DRM_DEBUG("unsupported pixel format: %s\n",
11345 drm_get_format_name(mode_cmd->pixel_format));
11349 case DRM_FORMAT_YUYV:
11350 case DRM_FORMAT_UYVY:
11351 case DRM_FORMAT_YVYU:
11352 case DRM_FORMAT_VYUY:
11353 if (INTEL_INFO(dev)->gen < 5) {
11354 DRM_DEBUG("unsupported pixel format: %s\n",
11355 drm_get_format_name(mode_cmd->pixel_format));
11360 DRM_DEBUG("unsupported pixel format: %s\n",
11361 drm_get_format_name(mode_cmd->pixel_format));
11365 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
11366 if (mode_cmd->offsets[0] != 0)
11369 aligned_height = intel_align_height(dev, mode_cmd->height,
11371 /* FIXME drm helper for size checks (especially planar formats)? */
11372 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
11375 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
11376 intel_fb->obj = obj;
11377 intel_fb->obj->framebuffer_references++;
11379 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
11381 DRM_ERROR("framebuffer init failed %d\n", ret);
11388 static struct drm_framebuffer *
11389 intel_user_framebuffer_create(struct drm_device *dev,
11390 struct drm_file *filp,
11391 struct drm_mode_fb_cmd2 *mode_cmd)
11393 struct drm_i915_gem_object *obj;
11395 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
11396 mode_cmd->handles[0]));
11397 if (&obj->base == NULL)
11398 return ERR_PTR(-ENOENT);
11400 return intel_framebuffer_create(dev, mode_cmd, obj);
11403 #ifndef CONFIG_DRM_I915_FBDEV
11404 static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
11409 static const struct drm_mode_config_funcs intel_mode_funcs = {
11410 .fb_create = intel_user_framebuffer_create,
11411 .output_poll_changed = intel_fbdev_output_poll_changed,
11414 /* Set up chip specific display functions */
11415 static void intel_init_display(struct drm_device *dev)
11417 struct drm_i915_private *dev_priv = dev->dev_private;
11419 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
11420 dev_priv->display.find_dpll = g4x_find_best_dpll;
11421 else if (IS_CHERRYVIEW(dev))
11422 dev_priv->display.find_dpll = chv_find_best_dpll;
11423 else if (IS_VALLEYVIEW(dev))
11424 dev_priv->display.find_dpll = vlv_find_best_dpll;
11425 else if (IS_PINEVIEW(dev))
11426 dev_priv->display.find_dpll = pnv_find_best_dpll;
11428 dev_priv->display.find_dpll = i9xx_find_best_dpll;
11430 if (HAS_DDI(dev)) {
11431 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
11432 dev_priv->display.get_plane_config = ironlake_get_plane_config;
11433 dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
11434 dev_priv->display.crtc_enable = haswell_crtc_enable;
11435 dev_priv->display.crtc_disable = haswell_crtc_disable;
11436 dev_priv->display.off = haswell_crtc_off;
11437 dev_priv->display.update_primary_plane =
11438 ironlake_update_primary_plane;
11439 } else if (HAS_PCH_SPLIT(dev)) {
11440 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
11441 dev_priv->display.get_plane_config = ironlake_get_plane_config;
11442 dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
11443 dev_priv->display.crtc_enable = ironlake_crtc_enable;
11444 dev_priv->display.crtc_disable = ironlake_crtc_disable;
11445 dev_priv->display.off = ironlake_crtc_off;
11446 dev_priv->display.update_primary_plane =
11447 ironlake_update_primary_plane;
11448 } else if (IS_VALLEYVIEW(dev)) {
11449 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
11450 dev_priv->display.get_plane_config = i9xx_get_plane_config;
11451 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
11452 dev_priv->display.crtc_enable = valleyview_crtc_enable;
11453 dev_priv->display.crtc_disable = i9xx_crtc_disable;
11454 dev_priv->display.off = i9xx_crtc_off;
11455 dev_priv->display.update_primary_plane =
11456 i9xx_update_primary_plane;
11458 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
11459 dev_priv->display.get_plane_config = i9xx_get_plane_config;
11460 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
11461 dev_priv->display.crtc_enable = i9xx_crtc_enable;
11462 dev_priv->display.crtc_disable = i9xx_crtc_disable;
11463 dev_priv->display.off = i9xx_crtc_off;
11464 dev_priv->display.update_primary_plane =
11465 i9xx_update_primary_plane;
11468 /* Returns the core display clock speed */
11469 if (IS_VALLEYVIEW(dev))
11470 dev_priv->display.get_display_clock_speed =
11471 valleyview_get_display_clock_speed;
11472 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
11473 dev_priv->display.get_display_clock_speed =
11474 i945_get_display_clock_speed;
11475 else if (IS_I915G(dev))
11476 dev_priv->display.get_display_clock_speed =
11477 i915_get_display_clock_speed;
11478 else if (IS_I945GM(dev) || IS_845G(dev))
11479 dev_priv->display.get_display_clock_speed =
11480 i9xx_misc_get_display_clock_speed;
11481 else if (IS_PINEVIEW(dev))
11482 dev_priv->display.get_display_clock_speed =
11483 pnv_get_display_clock_speed;
11484 else if (IS_I915GM(dev))
11485 dev_priv->display.get_display_clock_speed =
11486 i915gm_get_display_clock_speed;
11487 else if (IS_I865G(dev))
11488 dev_priv->display.get_display_clock_speed =
11489 i865_get_display_clock_speed;
11490 else if (IS_I85X(dev))
11491 dev_priv->display.get_display_clock_speed =
11492 i855_get_display_clock_speed;
11493 else /* 852, 830 */
11494 dev_priv->display.get_display_clock_speed =
11495 i830_get_display_clock_speed;
11497 if (HAS_PCH_SPLIT(dev)) {
11498 if (IS_GEN5(dev)) {
11499 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
11500 dev_priv->display.write_eld = ironlake_write_eld;
11501 } else if (IS_GEN6(dev)) {
11502 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
11503 dev_priv->display.write_eld = ironlake_write_eld;
11504 dev_priv->display.modeset_global_resources =
11505 snb_modeset_global_resources;
11506 } else if (IS_IVYBRIDGE(dev)) {
11507 /* FIXME: detect B0+ stepping and use auto training */
11508 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
11509 dev_priv->display.write_eld = ironlake_write_eld;
11510 dev_priv->display.modeset_global_resources =
11511 ivb_modeset_global_resources;
11512 } else if (IS_HASWELL(dev) || IS_GEN8(dev)) {
11513 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
11514 dev_priv->display.write_eld = haswell_write_eld;
11515 dev_priv->display.modeset_global_resources =
11516 haswell_modeset_global_resources;
11518 } else if (IS_G4X(dev)) {
11519 dev_priv->display.write_eld = g4x_write_eld;
11520 } else if (IS_VALLEYVIEW(dev)) {
11521 dev_priv->display.modeset_global_resources =
11522 valleyview_modeset_global_resources;
11523 dev_priv->display.write_eld = ironlake_write_eld;
11526 /* Default just returns -ENODEV to indicate unsupported */
11527 dev_priv->display.queue_flip = intel_default_queue_flip;
11529 switch (INTEL_INFO(dev)->gen) {
11531 dev_priv->display.queue_flip = intel_gen2_queue_flip;
11535 dev_priv->display.queue_flip = intel_gen3_queue_flip;
11540 dev_priv->display.queue_flip = intel_gen4_queue_flip;
11544 dev_priv->display.queue_flip = intel_gen6_queue_flip;
11547 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
11548 dev_priv->display.queue_flip = intel_gen7_queue_flip;
11552 intel_panel_init_backlight_funcs(dev);
11556 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
11557 * resume, or other times. This quirk makes sure that's the case for
11558 * affected systems.
11560 static void quirk_pipea_force(struct drm_device *dev)
11562 struct drm_i915_private *dev_priv = dev->dev_private;
11564 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
11565 DRM_INFO("applying pipe a force quirk\n");
11569 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
11571 static void quirk_ssc_force_disable(struct drm_device *dev)
11573 struct drm_i915_private *dev_priv = dev->dev_private;
11574 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
11575 DRM_INFO("applying lvds SSC disable quirk\n");
11579 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
11582 static void quirk_invert_brightness(struct drm_device *dev)
11584 struct drm_i915_private *dev_priv = dev->dev_private;
11585 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
11586 DRM_INFO("applying inverted panel brightness quirk\n");
11589 /* Some VBT's incorrectly indicate no backlight is present */
11590 static void quirk_backlight_present(struct drm_device *dev)
11592 struct drm_i915_private *dev_priv = dev->dev_private;
11593 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
11594 DRM_INFO("applying backlight present quirk\n");
11597 struct intel_quirk {
11599 int subsystem_vendor;
11600 int subsystem_device;
11601 void (*hook)(struct drm_device *dev);
11604 /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
11605 struct intel_dmi_quirk {
11606 void (*hook)(struct drm_device *dev);
11607 const struct dmi_system_id (*dmi_id_list)[];
11610 static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
11612 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
11616 static const struct intel_dmi_quirk intel_dmi_quirks[] = {
11618 .dmi_id_list = &(const struct dmi_system_id[]) {
11620 .callback = intel_dmi_reverse_brightness,
11621 .ident = "NCR Corporation",
11622 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
11623 DMI_MATCH(DMI_PRODUCT_NAME, ""),
11626 { } /* terminating entry */
11628 .hook = quirk_invert_brightness,
11632 static struct intel_quirk intel_quirks[] = {
11633 /* HP Mini needs pipe A force quirk (LP: #322104) */
11634 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
11636 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
11637 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
11639 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
11640 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
11642 /* Lenovo U160 cannot use SSC on LVDS */
11643 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
11645 /* Sony Vaio Y cannot use SSC on LVDS */
11646 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
11648 /* Acer Aspire 5734Z must invert backlight brightness */
11649 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
11651 /* Acer/eMachines G725 */
11652 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
11654 /* Acer/eMachines e725 */
11655 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
11657 /* Acer/Packard Bell NCL20 */
11658 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
11660 /* Acer Aspire 4736Z */
11661 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
11663 /* Acer Aspire 5336 */
11664 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
11666 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
11667 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
11669 /* Toshiba CB35 Chromebook (Celeron 2955U) */
11670 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
11672 /* HP Chromebook 14 (Celeron 2955U) */
11673 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
11676 static void intel_init_quirks(struct drm_device *dev)
11678 struct device *d = dev->dev;
11681 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
11682 struct intel_quirk *q = &intel_quirks[i];
11684 if (pci_get_device(d) == q->device &&
11685 (pci_get_subvendor(d) == q->subsystem_vendor ||
11686 q->subsystem_vendor == PCI_ANY_ID) &&
11687 (pci_get_subdevice(d) == q->subsystem_device ||
11688 q->subsystem_device == PCI_ANY_ID))
11691 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
11692 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
11693 intel_dmi_quirks[i].hook(dev);
11697 /* Disable the VGA plane that we never use */
11698 static void i915_disable_vga(struct drm_device *dev)
11700 struct drm_i915_private *dev_priv = dev->dev_private;
11702 u32 vga_reg = i915_vgacntrl_reg(dev);
11704 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
11706 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
11708 outb(VGA_SR_INDEX, SR01);
11709 sr1 = inb(VGA_SR_DATA);
11710 outb(VGA_SR_DATA, sr1 | 1 << 5);
11712 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
11716 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
11717 POSTING_READ(vga_reg);
11720 void intel_modeset_init_hw(struct drm_device *dev)
11722 intel_prepare_ddi(dev);
11724 intel_init_clock_gating(dev);
11726 intel_reset_dpio(dev);
11728 intel_enable_gt_powersave(dev);
11731 void intel_modeset_suspend_hw(struct drm_device *dev)
11733 intel_suspend_hw(dev);
11736 void intel_modeset_init(struct drm_device *dev)
11738 struct drm_i915_private *dev_priv = dev->dev_private;
11740 enum i915_pipe pipe;
11741 struct intel_crtc *crtc;
11743 drm_mode_config_init(dev);
11745 dev->mode_config.min_width = 0;
11746 dev->mode_config.min_height = 0;
11748 dev->mode_config.preferred_depth = 24;
11749 dev->mode_config.prefer_shadow = 1;
11751 dev->mode_config.funcs = &intel_mode_funcs;
11753 intel_init_quirks(dev);
11755 intel_init_pm(dev);
11757 if (INTEL_INFO(dev)->num_pipes == 0)
11760 intel_init_display(dev);
11762 if (IS_GEN2(dev)) {
11763 dev->mode_config.max_width = 2048;
11764 dev->mode_config.max_height = 2048;
11765 } else if (IS_GEN3(dev)) {
11766 dev->mode_config.max_width = 4096;
11767 dev->mode_config.max_height = 4096;
11769 dev->mode_config.max_width = 8192;
11770 dev->mode_config.max_height = 8192;
11773 if (IS_GEN2(dev)) {
11774 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
11775 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
11777 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
11778 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
11781 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
11783 DRM_DEBUG_KMS("%d display pipe%s available.\n",
11784 INTEL_INFO(dev)->num_pipes,
11785 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
11787 for_each_pipe(pipe) {
11788 intel_crtc_init(dev, pipe);
11789 for_each_sprite(pipe, sprite) {
11790 ret = intel_plane_init(dev, pipe, sprite);
11792 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
11793 pipe_name(pipe), sprite_name(pipe, sprite), ret);
11797 intel_init_dpio(dev);
11798 intel_reset_dpio(dev);
11800 intel_cpu_pll_init(dev);
11801 intel_shared_dpll_init(dev);
11803 /* Just disable it once at startup */
11804 i915_disable_vga(dev);
11805 intel_setup_outputs(dev);
11807 /* Just in case the BIOS is doing something questionable. */
11808 intel_disable_fbc(dev);
11810 drm_modeset_lock_all(dev);
11811 intel_modeset_setup_hw_state(dev, false);
11812 drm_modeset_unlock_all(dev);
11814 for_each_intel_crtc(dev, crtc) {
11819 * Note that reserving the BIOS fb up front prevents us
11820 * from stuffing other stolen allocations like the ring
11821 * on top. This prevents some ugliness at boot time, and
11822 * can even allow for smooth boot transitions if the BIOS
11823 * fb is large enough for the active pipe configuration.
11825 if (dev_priv->display.get_plane_config) {
11826 dev_priv->display.get_plane_config(crtc,
11827 &crtc->plane_config);
11829 * If the fb is shared between multiple heads, we'll
11830 * just get the first one.
11832 intel_find_plane_obj(crtc, &crtc->plane_config);
11837 static void intel_enable_pipe_a(struct drm_device *dev)
11839 struct intel_connector *connector;
11840 struct drm_connector *crt = NULL;
11841 struct intel_load_detect_pipe load_detect_temp;
11842 struct drm_modeset_acquire_ctx ctx;
11844 /* We can't just switch on the pipe A, we need to set things up with a
11845 * proper mode and output configuration. As a gross hack, enable pipe A
11846 * by enabling the load detect pipe once. */
11847 list_for_each_entry(connector,
11848 &dev->mode_config.connector_list,
11850 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
11851 crt = &connector->base;
11859 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, &ctx))
11860 intel_release_load_detect_pipe(crt, &load_detect_temp, &ctx);
11866 intel_check_plane_mapping(struct intel_crtc *crtc)
11868 struct drm_device *dev = crtc->base.dev;
11869 struct drm_i915_private *dev_priv = dev->dev_private;
11872 if (INTEL_INFO(dev)->num_pipes == 1)
11875 reg = DSPCNTR(!crtc->plane);
11876 val = I915_READ(reg);
11878 if ((val & DISPLAY_PLANE_ENABLE) &&
11879 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
11885 static void intel_sanitize_crtc(struct intel_crtc *crtc)
11887 struct drm_device *dev = crtc->base.dev;
11888 struct drm_i915_private *dev_priv = dev->dev_private;
11891 /* Clear any frame start delays used for debugging left by the BIOS */
11892 reg = PIPECONF(crtc->config.cpu_transcoder);
11893 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
11895 /* restore vblank interrupts to correct state */
11897 drm_vblank_on(dev, crtc->pipe);
11899 drm_vblank_off(dev, crtc->pipe);
11901 /* We need to sanitize the plane -> pipe mapping first because this will
11902 * disable the crtc (and hence change the state) if it is wrong. Note
11903 * that gen4+ has a fixed plane -> pipe mapping. */
11904 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
11905 struct intel_connector *connector;
11908 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
11909 crtc->base.base.id);
11911 /* Pipe has the wrong plane attached and the plane is active.
11912 * Temporarily change the plane mapping and disable everything
11914 plane = crtc->plane;
11915 crtc->plane = !plane;
11916 crtc->primary_enabled = true;
11917 dev_priv->display.crtc_disable(&crtc->base);
11918 crtc->plane = plane;
11920 /* ... and break all links. */
11921 list_for_each_entry(connector, &dev->mode_config.connector_list,
11923 if (connector->encoder->base.crtc != &crtc->base)
11926 connector->base.dpms = DRM_MODE_DPMS_OFF;
11927 connector->base.encoder = NULL;
11929 /* multiple connectors may have the same encoder:
11930 * handle them and break crtc link separately */
11931 list_for_each_entry(connector, &dev->mode_config.connector_list,
11933 if (connector->encoder->base.crtc == &crtc->base) {
11934 connector->encoder->base.crtc = NULL;
11935 connector->encoder->connectors_active = false;
11938 WARN_ON(crtc->active);
11939 crtc->base.enabled = false;
11942 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
11943 crtc->pipe == PIPE_A && !crtc->active) {
11944 /* BIOS forgot to enable pipe A, this mostly happens after
11945 * resume. Force-enable the pipe to fix this, the update_dpms
11946 * call below we restore the pipe to the right state, but leave
11947 * the required bits on. */
11948 intel_enable_pipe_a(dev);
11951 /* Adjust the state of the output pipe according to whether we
11952 * have active connectors/encoders. */
11953 intel_crtc_update_dpms(&crtc->base);
11955 if (crtc->active != crtc->base.enabled) {
11956 struct intel_encoder *encoder;
11958 /* This can happen either due to bugs in the get_hw_state
11959 * functions or because the pipe is force-enabled due to the
11961 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
11962 crtc->base.base.id,
11963 crtc->base.enabled ? "enabled" : "disabled",
11964 crtc->active ? "enabled" : "disabled");
11966 crtc->base.enabled = crtc->active;
11968 /* Because we only establish the connector -> encoder ->
11969 * crtc links if something is active, this means the
11970 * crtc is now deactivated. Break the links. connector
11971 * -> encoder links are only establish when things are
11972 * actually up, hence no need to break them. */
11973 WARN_ON(crtc->active);
11975 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
11976 WARN_ON(encoder->connectors_active);
11977 encoder->base.crtc = NULL;
11981 if (crtc->active || IS_VALLEYVIEW(dev) || INTEL_INFO(dev)->gen < 5) {
11983 * We start out with underrun reporting disabled to avoid races.
11984 * For correct bookkeeping mark this on active crtcs.
11986 * Also on gmch platforms we dont have any hardware bits to
11987 * disable the underrun reporting. Which means we need to start
11988 * out with underrun reporting disabled also on inactive pipes,
11989 * since otherwise we'll complain about the garbage we read when
11990 * e.g. coming up after runtime pm.
11992 * No protection against concurrent access is required - at
11993 * worst a fifo underrun happens which also sets this to false.
11995 crtc->cpu_fifo_underrun_disabled = true;
11996 crtc->pch_fifo_underrun_disabled = true;
11998 update_scanline_offset(crtc);
12002 static void intel_sanitize_encoder(struct intel_encoder *encoder)
12004 struct intel_connector *connector;
12005 struct drm_device *dev = encoder->base.dev;
12007 /* We need to check both for a crtc link (meaning that the
12008 * encoder is active and trying to read from a pipe) and the
12009 * pipe itself being active. */
12010 bool has_active_crtc = encoder->base.crtc &&
12011 to_intel_crtc(encoder->base.crtc)->active;
12013 if (encoder->connectors_active && !has_active_crtc) {
12014 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
12015 encoder->base.base.id,
12016 encoder->base.name);
12018 /* Connector is active, but has no active pipe. This is
12019 * fallout from our resume register restoring. Disable
12020 * the encoder manually again. */
12021 if (encoder->base.crtc) {
12022 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
12023 encoder->base.base.id,
12024 encoder->base.name);
12025 encoder->disable(encoder);
12027 encoder->base.crtc = NULL;
12028 encoder->connectors_active = false;
12030 /* Inconsistent output/port/pipe state happens presumably due to
12031 * a bug in one of the get_hw_state functions. Or someplace else
12032 * in our code, like the register restore mess on resume. Clamp
12033 * things to off as a safer default. */
12034 list_for_each_entry(connector,
12035 &dev->mode_config.connector_list,
12037 if (connector->encoder != encoder)
12040 connector->base.dpms = DRM_MODE_DPMS_OFF;
12041 connector->base.encoder = NULL;
12044 /* Enabled encoders without active connectors will be fixed in
12045 * the crtc fixup. */
12048 void i915_redisable_vga_power_on(struct drm_device *dev)
12050 struct drm_i915_private *dev_priv = dev->dev_private;
12051 u32 vga_reg = i915_vgacntrl_reg(dev);
12053 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
12054 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
12055 i915_disable_vga(dev);
12059 void i915_redisable_vga(struct drm_device *dev)
12061 struct drm_i915_private *dev_priv = dev->dev_private;
12063 /* This function can be called both from intel_modeset_setup_hw_state or
12064 * at a very early point in our resume sequence, where the power well
12065 * structures are not yet restored. Since this function is at a very
12066 * paranoid "someone might have enabled VGA while we were not looking"
12067 * level, just check if the power well is enabled instead of trying to
12068 * follow the "don't touch the power well if we don't need it" policy
12069 * the rest of the driver uses. */
12070 if (!intel_display_power_enabled(dev_priv, POWER_DOMAIN_VGA))
12073 i915_redisable_vga_power_on(dev);
12076 static bool primary_get_hw_state(struct intel_crtc *crtc)
12078 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
12083 return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
12086 static void intel_modeset_readout_hw_state(struct drm_device *dev)
12088 struct drm_i915_private *dev_priv = dev->dev_private;
12089 enum i915_pipe pipe;
12090 struct intel_crtc *crtc;
12091 struct intel_encoder *encoder;
12092 struct intel_connector *connector;
12095 for_each_intel_crtc(dev, crtc) {
12096 memset(&crtc->config, 0, sizeof(crtc->config));
12098 crtc->config.quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
12100 crtc->active = dev_priv->display.get_pipe_config(crtc,
12103 crtc->base.enabled = crtc->active;
12104 crtc->primary_enabled = primary_get_hw_state(crtc);
12106 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
12107 crtc->base.base.id,
12108 crtc->active ? "enabled" : "disabled");
12111 /* FIXME: Smash this into the new shared dpll infrastructure. */
12113 intel_ddi_setup_hw_pll_state(dev);
12115 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
12116 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
12118 pll->on = pll->get_hw_state(dev_priv, pll, &pll->hw_state);
12120 for_each_intel_crtc(dev, crtc) {
12121 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
12124 pll->refcount = pll->active;
12126 DRM_DEBUG_KMS("%s hw state readout: refcount %i, on %i\n",
12127 pll->name, pll->refcount, pll->on);
12130 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
12134 if (encoder->get_hw_state(encoder, &pipe)) {
12135 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
12136 encoder->base.crtc = &crtc->base;
12137 encoder->get_config(encoder, &crtc->config);
12139 encoder->base.crtc = NULL;
12142 encoder->connectors_active = false;
12143 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
12144 encoder->base.base.id,
12145 encoder->base.name,
12146 encoder->base.crtc ? "enabled" : "disabled",
12150 list_for_each_entry(connector, &dev->mode_config.connector_list,
12152 if (connector->get_hw_state(connector)) {
12153 connector->base.dpms = DRM_MODE_DPMS_ON;
12154 connector->encoder->connectors_active = true;
12155 connector->base.encoder = &connector->encoder->base;
12157 connector->base.dpms = DRM_MODE_DPMS_OFF;
12158 connector->base.encoder = NULL;
12160 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
12161 connector->base.base.id,
12162 connector->base.name,
12163 connector->base.encoder ? "enabled" : "disabled");
12167 /* Scan out the current hw modeset state, sanitizes it and maps it into the drm
12168 * and i915 state tracking structures. */
12169 void intel_modeset_setup_hw_state(struct drm_device *dev,
12170 bool force_restore)
12172 struct drm_i915_private *dev_priv = dev->dev_private;
12173 enum i915_pipe pipe;
12174 struct intel_crtc *crtc;
12175 struct intel_encoder *encoder;
12178 intel_modeset_readout_hw_state(dev);
12181 * Now that we have the config, copy it to each CRTC struct
12182 * Note that this could go away if we move to using crtc_config
12183 * checking everywhere.
12185 for_each_intel_crtc(dev, crtc) {
12186 if (crtc->active && i915.fastboot) {
12187 intel_mode_from_pipe_config(&crtc->base.mode, &crtc->config);
12188 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
12189 crtc->base.base.id);
12190 drm_mode_debug_printmodeline(&crtc->base.mode);
12194 /* HW state is read out, now we need to sanitize this mess. */
12195 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
12197 intel_sanitize_encoder(encoder);
12200 for_each_pipe(pipe) {
12201 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
12202 intel_sanitize_crtc(crtc);
12203 intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
12206 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
12207 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
12209 if (!pll->on || pll->active)
12212 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
12214 pll->disable(dev_priv, pll);
12218 if (HAS_PCH_SPLIT(dev))
12219 ilk_wm_get_hw_state(dev);
12221 if (force_restore) {
12222 i915_redisable_vga(dev);
12225 * We need to use raw interfaces for restoring state to avoid
12226 * checking (bogus) intermediate states.
12228 for_each_pipe(pipe) {
12229 struct drm_crtc *crtc =
12230 dev_priv->pipe_to_crtc_mapping[pipe];
12232 __intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
12233 crtc->primary->fb);
12236 intel_modeset_update_staged_output_state(dev);
12239 intel_modeset_check_state(dev);
12242 void intel_modeset_gem_init(struct drm_device *dev)
12244 struct drm_crtc *c;
12245 struct intel_framebuffer *fb;
12247 mutex_lock(&dev->struct_mutex);
12248 intel_init_gt_powersave(dev);
12249 mutex_unlock(&dev->struct_mutex);
12251 intel_modeset_init_hw(dev);
12253 intel_setup_overlay(dev);
12256 * Make sure any fbs we allocated at startup are properly
12257 * pinned & fenced. When we do the allocation it's too early
12260 mutex_lock(&dev->struct_mutex);
12261 for_each_crtc(dev, c) {
12262 if (!c->primary->fb)
12265 fb = to_intel_framebuffer(c->primary->fb);
12266 if (intel_pin_and_fence_fb_obj(dev, fb->obj, NULL)) {
12267 DRM_ERROR("failed to pin boot fb on pipe %d\n",
12268 to_intel_crtc(c)->pipe);
12269 drm_framebuffer_unreference(c->primary->fb);
12270 c->primary->fb = NULL;
12273 mutex_unlock(&dev->struct_mutex);
12276 void intel_connector_unregister(struct intel_connector *intel_connector)
12278 struct drm_connector *connector = &intel_connector->base;
12280 intel_panel_destroy_backlight(connector);
12281 drm_sysfs_connector_remove(connector);
12284 void intel_modeset_cleanup(struct drm_device *dev)
12286 struct drm_i915_private *dev_priv = dev->dev_private;
12287 struct drm_crtc *crtc;
12288 struct drm_connector *connector;
12291 * Interrupts and polling as the first thing to avoid creating havoc.
12292 * Too much stuff here (turning of rps, connectors, ...) would
12293 * experience fancy races otherwise.
12295 drm_irq_uninstall(dev);
12296 cancel_work_sync(&dev_priv->hotplug_work);
12298 * Due to the hpd irq storm handling the hotplug work can re-arm the
12299 * poll handlers. Hence disable polling after hpd handling is shut down.
12301 drm_kms_helper_poll_fini(dev);
12303 mutex_lock(&dev->struct_mutex);
12305 intel_unregister_dsm_handler();
12307 for_each_crtc(dev, crtc) {
12308 /* Skip inactive CRTCs */
12309 if (!crtc->primary->fb)
12312 intel_increase_pllclock(crtc);
12315 intel_disable_fbc(dev);
12317 intel_disable_gt_powersave(dev);
12319 ironlake_teardown_rc6(dev);
12321 mutex_unlock(&dev->struct_mutex);
12323 /* flush any delayed tasks or pending work */
12324 flush_scheduled_work();
12326 /* destroy the backlight and sysfs files before encoders/connectors */
12327 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
12328 struct intel_connector *intel_connector;
12330 intel_connector = to_intel_connector(connector);
12331 intel_connector->unregister(intel_connector);
12334 drm_mode_config_cleanup(dev);
12336 intel_cleanup_overlay(dev);
12338 mutex_lock(&dev->struct_mutex);
12339 intel_cleanup_gt_powersave(dev);
12340 mutex_unlock(&dev->struct_mutex);
12344 * Return which encoder is currently attached for connector.
12346 struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
12348 return &intel_attached_encoder(connector)->base;
12351 void intel_connector_attach_encoder(struct intel_connector *connector,
12352 struct intel_encoder *encoder)
12354 connector->encoder = encoder;
12355 drm_mode_connector_attach_encoder(&connector->base,
12360 * set vga decode state - true == enable VGA decode
12362 int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
12364 struct drm_i915_private *dev_priv = dev->dev_private;
12365 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
12368 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
12369 DRM_ERROR("failed to read control word\n");
12373 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
12377 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
12379 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
12381 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
12382 DRM_ERROR("failed to write control word\n");
12390 struct intel_display_error_state {
12392 u32 power_well_driver;
12394 int num_transcoders;
12396 struct intel_cursor_error_state {
12401 } cursor[I915_MAX_PIPES];
12403 struct intel_pipe_error_state {
12404 bool power_domain_on;
12407 } pipe[I915_MAX_PIPES];
12409 struct intel_plane_error_state {
12417 } plane[I915_MAX_PIPES];
12419 struct intel_transcoder_error_state {
12420 bool power_domain_on;
12421 enum transcoder cpu_transcoder;
12434 struct intel_display_error_state *
12435 intel_display_capture_error_state(struct drm_device *dev)
12437 struct drm_i915_private *dev_priv = dev->dev_private;
12438 struct intel_display_error_state *error;
12439 int transcoders[] = {
12447 if (INTEL_INFO(dev)->num_pipes == 0)
12450 error = kzalloc(sizeof(*error), GFP_ATOMIC);
12454 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
12455 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
12458 error->pipe[i].power_domain_on =
12459 intel_display_power_enabled_unlocked(dev_priv,
12460 POWER_DOMAIN_PIPE(i));
12461 if (!error->pipe[i].power_domain_on)
12464 error->cursor[i].control = I915_READ(CURCNTR(i));
12465 error->cursor[i].position = I915_READ(CURPOS(i));
12466 error->cursor[i].base = I915_READ(CURBASE(i));
12468 error->plane[i].control = I915_READ(DSPCNTR(i));
12469 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
12470 if (INTEL_INFO(dev)->gen <= 3) {
12471 error->plane[i].size = I915_READ(DSPSIZE(i));
12472 error->plane[i].pos = I915_READ(DSPPOS(i));
12474 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
12475 error->plane[i].addr = I915_READ(DSPADDR(i));
12476 if (INTEL_INFO(dev)->gen >= 4) {
12477 error->plane[i].surface = I915_READ(DSPSURF(i));
12478 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
12481 error->pipe[i].source = I915_READ(PIPESRC(i));
12483 if (!HAS_PCH_SPLIT(dev))
12484 error->pipe[i].stat = I915_READ(PIPESTAT(i));
12487 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
12488 if (HAS_DDI(dev_priv->dev))
12489 error->num_transcoders++; /* Account for eDP. */
12491 for (i = 0; i < error->num_transcoders; i++) {
12492 enum transcoder cpu_transcoder = transcoders[i];
12494 error->transcoder[i].power_domain_on =
12495 intel_display_power_enabled_unlocked(dev_priv,
12496 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
12497 if (!error->transcoder[i].power_domain_on)
12500 error->transcoder[i].cpu_transcoder = cpu_transcoder;
12502 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
12503 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
12504 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
12505 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
12506 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
12507 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
12508 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
12514 #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
12517 intel_display_print_error_state(struct drm_i915_error_state_buf *m,
12518 struct drm_device *dev,
12519 struct intel_display_error_state *error)
12526 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
12527 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
12528 err_printf(m, "PWR_WELL_CTL2: %08x\n",
12529 error->power_well_driver);
12531 err_printf(m, "Pipe [%d]:\n", i);
12532 err_printf(m, " Power: %s\n",
12533 error->pipe[i].power_domain_on ? "on" : "off");
12534 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
12535 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
12537 err_printf(m, "Plane [%d]:\n", i);
12538 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
12539 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
12540 if (INTEL_INFO(dev)->gen <= 3) {
12541 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
12542 err_printf(m, " POS: %08x\n", error->plane[i].pos);
12544 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
12545 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
12546 if (INTEL_INFO(dev)->gen >= 4) {
12547 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
12548 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
12551 err_printf(m, "Cursor [%d]:\n", i);
12552 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
12553 err_printf(m, " POS: %08x\n", error->cursor[i].position);
12554 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
12557 for (i = 0; i < error->num_transcoders; i++) {
12558 err_printf(m, "CPU transcoder: %c\n",
12559 transcoder_name(error->transcoder[i].cpu_transcoder));
12560 err_printf(m, " Power: %s\n",
12561 error->transcoder[i].power_domain_on ? "on" : "off");
12562 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
12563 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
12564 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
12565 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
12566 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
12567 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
12568 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);