8e8ac366e75e50d8320a2d7ae3f632bf92a46f25
[deliverable/linux.git] / drivers / gpu / drm / i915 / intel_display.c
1 /*
2 * Copyright © 2006-2007 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 */
26
27 #include <linux/dmi.h>
28 #include <linux/module.h>
29 #include <linux/input.h>
30 #include <linux/i2c.h>
31 #include <linux/kernel.h>
32 #include <linux/slab.h>
33 #include <linux/vgaarb.h>
34 #include <drm/drm_edid.h>
35 #include <drm/drmP.h>
36 #include "intel_drv.h"
37 #include <drm/i915_drm.h>
38 #include "i915_drv.h"
39 #include "i915_trace.h"
40 #include <drm/drm_dp_helper.h>
41 #include <drm/drm_crtc_helper.h>
42 #include <linux/dma_remapping.h>
43
44 bool intel_pipe_has_type(struct drm_crtc *crtc, int type);
45 static void intel_increase_pllclock(struct drm_crtc *crtc);
46 static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
47
48 typedef struct {
49 int min, max;
50 } intel_range_t;
51
52 typedef struct {
53 int dot_limit;
54 int p2_slow, p2_fast;
55 } intel_p2_t;
56
57 #define INTEL_P2_NUM 2
58 typedef struct intel_limit intel_limit_t;
59 struct intel_limit {
60 intel_range_t dot, vco, n, m, m1, m2, p, p1;
61 intel_p2_t p2;
62 /**
63 * find_pll() - Find the best values for the PLL
64 * @limit: limits for the PLL
65 * @crtc: current CRTC
66 * @target: target frequency in kHz
67 * @refclk: reference clock frequency in kHz
68 * @match_clock: if provided, @best_clock P divider must
69 * match the P divider from @match_clock
70 * used for LVDS downclocking
71 * @best_clock: best PLL values found
72 *
73 * Returns true on success, false on failure.
74 */
75 bool (*find_pll)(const intel_limit_t *limit,
76 struct drm_crtc *crtc,
77 int target, int refclk,
78 intel_clock_t *match_clock,
79 intel_clock_t *best_clock);
80 };
81
82 /* FDI */
83 #define IRONLAKE_FDI_FREQ 2700000 /* in kHz for mode->clock */
84
85 int
86 intel_pch_rawclk(struct drm_device *dev)
87 {
88 struct drm_i915_private *dev_priv = dev->dev_private;
89
90 WARN_ON(!HAS_PCH_SPLIT(dev));
91
92 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
93 }
94
95 static bool
96 intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
97 int target, int refclk, intel_clock_t *match_clock,
98 intel_clock_t *best_clock);
99 static bool
100 intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
101 int target, int refclk, intel_clock_t *match_clock,
102 intel_clock_t *best_clock);
103
104 static bool
105 intel_find_pll_g4x_dp(const intel_limit_t *, struct drm_crtc *crtc,
106 int target, int refclk, intel_clock_t *match_clock,
107 intel_clock_t *best_clock);
108 static bool
109 intel_find_pll_ironlake_dp(const intel_limit_t *, struct drm_crtc *crtc,
110 int target, int refclk, intel_clock_t *match_clock,
111 intel_clock_t *best_clock);
112
113 static bool
114 intel_vlv_find_best_pll(const intel_limit_t *limit, struct drm_crtc *crtc,
115 int target, int refclk, intel_clock_t *match_clock,
116 intel_clock_t *best_clock);
117
118 static inline u32 /* units of 100MHz */
119 intel_fdi_link_freq(struct drm_device *dev)
120 {
121 if (IS_GEN5(dev)) {
122 struct drm_i915_private *dev_priv = dev->dev_private;
123 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
124 } else
125 return 27;
126 }
127
128 static const intel_limit_t intel_limits_i8xx_dvo = {
129 .dot = { .min = 25000, .max = 350000 },
130 .vco = { .min = 930000, .max = 1400000 },
131 .n = { .min = 3, .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 = 2, .max = 33 },
137 .p2 = { .dot_limit = 165000,
138 .p2_slow = 4, .p2_fast = 2 },
139 .find_pll = intel_find_best_PLL,
140 };
141
142 static const intel_limit_t intel_limits_i8xx_lvds = {
143 .dot = { .min = 25000, .max = 350000 },
144 .vco = { .min = 930000, .max = 1400000 },
145 .n = { .min = 3, .max = 16 },
146 .m = { .min = 96, .max = 140 },
147 .m1 = { .min = 18, .max = 26 },
148 .m2 = { .min = 6, .max = 16 },
149 .p = { .min = 4, .max = 128 },
150 .p1 = { .min = 1, .max = 6 },
151 .p2 = { .dot_limit = 165000,
152 .p2_slow = 14, .p2_fast = 7 },
153 .find_pll = intel_find_best_PLL,
154 };
155
156 static const intel_limit_t intel_limits_i9xx_sdvo = {
157 .dot = { .min = 20000, .max = 400000 },
158 .vco = { .min = 1400000, .max = 2800000 },
159 .n = { .min = 1, .max = 6 },
160 .m = { .min = 70, .max = 120 },
161 .m1 = { .min = 8, .max = 18 },
162 .m2 = { .min = 3, .max = 7 },
163 .p = { .min = 5, .max = 80 },
164 .p1 = { .min = 1, .max = 8 },
165 .p2 = { .dot_limit = 200000,
166 .p2_slow = 10, .p2_fast = 5 },
167 .find_pll = intel_find_best_PLL,
168 };
169
170 static const intel_limit_t intel_limits_i9xx_lvds = {
171 .dot = { .min = 20000, .max = 400000 },
172 .vco = { .min = 1400000, .max = 2800000 },
173 .n = { .min = 1, .max = 6 },
174 .m = { .min = 70, .max = 120 },
175 .m1 = { .min = 8, .max = 18 },
176 .m2 = { .min = 3, .max = 7 },
177 .p = { .min = 7, .max = 98 },
178 .p1 = { .min = 1, .max = 8 },
179 .p2 = { .dot_limit = 112000,
180 .p2_slow = 14, .p2_fast = 7 },
181 .find_pll = intel_find_best_PLL,
182 };
183
184
185 static const intel_limit_t intel_limits_g4x_sdvo = {
186 .dot = { .min = 25000, .max = 270000 },
187 .vco = { .min = 1750000, .max = 3500000},
188 .n = { .min = 1, .max = 4 },
189 .m = { .min = 104, .max = 138 },
190 .m1 = { .min = 17, .max = 23 },
191 .m2 = { .min = 5, .max = 11 },
192 .p = { .min = 10, .max = 30 },
193 .p1 = { .min = 1, .max = 3},
194 .p2 = { .dot_limit = 270000,
195 .p2_slow = 10,
196 .p2_fast = 10
197 },
198 .find_pll = intel_g4x_find_best_PLL,
199 };
200
201 static const intel_limit_t intel_limits_g4x_hdmi = {
202 .dot = { .min = 22000, .max = 400000 },
203 .vco = { .min = 1750000, .max = 3500000},
204 .n = { .min = 1, .max = 4 },
205 .m = { .min = 104, .max = 138 },
206 .m1 = { .min = 16, .max = 23 },
207 .m2 = { .min = 5, .max = 11 },
208 .p = { .min = 5, .max = 80 },
209 .p1 = { .min = 1, .max = 8},
210 .p2 = { .dot_limit = 165000,
211 .p2_slow = 10, .p2_fast = 5 },
212 .find_pll = intel_g4x_find_best_PLL,
213 };
214
215 static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
216 .dot = { .min = 20000, .max = 115000 },
217 .vco = { .min = 1750000, .max = 3500000 },
218 .n = { .min = 1, .max = 3 },
219 .m = { .min = 104, .max = 138 },
220 .m1 = { .min = 17, .max = 23 },
221 .m2 = { .min = 5, .max = 11 },
222 .p = { .min = 28, .max = 112 },
223 .p1 = { .min = 2, .max = 8 },
224 .p2 = { .dot_limit = 0,
225 .p2_slow = 14, .p2_fast = 14
226 },
227 .find_pll = intel_g4x_find_best_PLL,
228 };
229
230 static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
231 .dot = { .min = 80000, .max = 224000 },
232 .vco = { .min = 1750000, .max = 3500000 },
233 .n = { .min = 1, .max = 3 },
234 .m = { .min = 104, .max = 138 },
235 .m1 = { .min = 17, .max = 23 },
236 .m2 = { .min = 5, .max = 11 },
237 .p = { .min = 14, .max = 42 },
238 .p1 = { .min = 2, .max = 6 },
239 .p2 = { .dot_limit = 0,
240 .p2_slow = 7, .p2_fast = 7
241 },
242 .find_pll = intel_g4x_find_best_PLL,
243 };
244
245 static const intel_limit_t intel_limits_g4x_display_port = {
246 .dot = { .min = 161670, .max = 227000 },
247 .vco = { .min = 1750000, .max = 3500000},
248 .n = { .min = 1, .max = 2 },
249 .m = { .min = 97, .max = 108 },
250 .m1 = { .min = 0x10, .max = 0x12 },
251 .m2 = { .min = 0x05, .max = 0x06 },
252 .p = { .min = 10, .max = 20 },
253 .p1 = { .min = 1, .max = 2},
254 .p2 = { .dot_limit = 0,
255 .p2_slow = 10, .p2_fast = 10 },
256 .find_pll = intel_find_pll_g4x_dp,
257 };
258
259 static const intel_limit_t intel_limits_pineview_sdvo = {
260 .dot = { .min = 20000, .max = 400000},
261 .vco = { .min = 1700000, .max = 3500000 },
262 /* Pineview's Ncounter is a ring counter */
263 .n = { .min = 3, .max = 6 },
264 .m = { .min = 2, .max = 256 },
265 /* Pineview only has one combined m divider, which we treat as m2. */
266 .m1 = { .min = 0, .max = 0 },
267 .m2 = { .min = 0, .max = 254 },
268 .p = { .min = 5, .max = 80 },
269 .p1 = { .min = 1, .max = 8 },
270 .p2 = { .dot_limit = 200000,
271 .p2_slow = 10, .p2_fast = 5 },
272 .find_pll = intel_find_best_PLL,
273 };
274
275 static const intel_limit_t intel_limits_pineview_lvds = {
276 .dot = { .min = 20000, .max = 400000 },
277 .vco = { .min = 1700000, .max = 3500000 },
278 .n = { .min = 3, .max = 6 },
279 .m = { .min = 2, .max = 256 },
280 .m1 = { .min = 0, .max = 0 },
281 .m2 = { .min = 0, .max = 254 },
282 .p = { .min = 7, .max = 112 },
283 .p1 = { .min = 1, .max = 8 },
284 .p2 = { .dot_limit = 112000,
285 .p2_slow = 14, .p2_fast = 14 },
286 .find_pll = intel_find_best_PLL,
287 };
288
289 /* Ironlake / Sandybridge
290 *
291 * We calculate clock using (register_value + 2) for N/M1/M2, so here
292 * the range value for them is (actual_value - 2).
293 */
294 static const intel_limit_t intel_limits_ironlake_dac = {
295 .dot = { .min = 25000, .max = 350000 },
296 .vco = { .min = 1760000, .max = 3510000 },
297 .n = { .min = 1, .max = 5 },
298 .m = { .min = 79, .max = 127 },
299 .m1 = { .min = 12, .max = 22 },
300 .m2 = { .min = 5, .max = 9 },
301 .p = { .min = 5, .max = 80 },
302 .p1 = { .min = 1, .max = 8 },
303 .p2 = { .dot_limit = 225000,
304 .p2_slow = 10, .p2_fast = 5 },
305 .find_pll = intel_g4x_find_best_PLL,
306 };
307
308 static const intel_limit_t intel_limits_ironlake_single_lvds = {
309 .dot = { .min = 25000, .max = 350000 },
310 .vco = { .min = 1760000, .max = 3510000 },
311 .n = { .min = 1, .max = 3 },
312 .m = { .min = 79, .max = 118 },
313 .m1 = { .min = 12, .max = 22 },
314 .m2 = { .min = 5, .max = 9 },
315 .p = { .min = 28, .max = 112 },
316 .p1 = { .min = 2, .max = 8 },
317 .p2 = { .dot_limit = 225000,
318 .p2_slow = 14, .p2_fast = 14 },
319 .find_pll = intel_g4x_find_best_PLL,
320 };
321
322 static const intel_limit_t intel_limits_ironlake_dual_lvds = {
323 .dot = { .min = 25000, .max = 350000 },
324 .vco = { .min = 1760000, .max = 3510000 },
325 .n = { .min = 1, .max = 3 },
326 .m = { .min = 79, .max = 127 },
327 .m1 = { .min = 12, .max = 22 },
328 .m2 = { .min = 5, .max = 9 },
329 .p = { .min = 14, .max = 56 },
330 .p1 = { .min = 2, .max = 8 },
331 .p2 = { .dot_limit = 225000,
332 .p2_slow = 7, .p2_fast = 7 },
333 .find_pll = intel_g4x_find_best_PLL,
334 };
335
336 /* LVDS 100mhz refclk limits. */
337 static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
338 .dot = { .min = 25000, .max = 350000 },
339 .vco = { .min = 1760000, .max = 3510000 },
340 .n = { .min = 1, .max = 2 },
341 .m = { .min = 79, .max = 126 },
342 .m1 = { .min = 12, .max = 22 },
343 .m2 = { .min = 5, .max = 9 },
344 .p = { .min = 28, .max = 112 },
345 .p1 = { .min = 2, .max = 8 },
346 .p2 = { .dot_limit = 225000,
347 .p2_slow = 14, .p2_fast = 14 },
348 .find_pll = intel_g4x_find_best_PLL,
349 };
350
351 static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
352 .dot = { .min = 25000, .max = 350000 },
353 .vco = { .min = 1760000, .max = 3510000 },
354 .n = { .min = 1, .max = 3 },
355 .m = { .min = 79, .max = 126 },
356 .m1 = { .min = 12, .max = 22 },
357 .m2 = { .min = 5, .max = 9 },
358 .p = { .min = 14, .max = 42 },
359 .p1 = { .min = 2, .max = 6 },
360 .p2 = { .dot_limit = 225000,
361 .p2_slow = 7, .p2_fast = 7 },
362 .find_pll = intel_g4x_find_best_PLL,
363 };
364
365 static const intel_limit_t intel_limits_ironlake_display_port = {
366 .dot = { .min = 25000, .max = 350000 },
367 .vco = { .min = 1760000, .max = 3510000},
368 .n = { .min = 1, .max = 2 },
369 .m = { .min = 81, .max = 90 },
370 .m1 = { .min = 12, .max = 22 },
371 .m2 = { .min = 5, .max = 9 },
372 .p = { .min = 10, .max = 20 },
373 .p1 = { .min = 1, .max = 2},
374 .p2 = { .dot_limit = 0,
375 .p2_slow = 10, .p2_fast = 10 },
376 .find_pll = intel_find_pll_ironlake_dp,
377 };
378
379 static const intel_limit_t intel_limits_vlv_dac = {
380 .dot = { .min = 25000, .max = 270000 },
381 .vco = { .min = 4000000, .max = 6000000 },
382 .n = { .min = 1, .max = 7 },
383 .m = { .min = 22, .max = 450 }, /* guess */
384 .m1 = { .min = 2, .max = 3 },
385 .m2 = { .min = 11, .max = 156 },
386 .p = { .min = 10, .max = 30 },
387 .p1 = { .min = 1, .max = 3 },
388 .p2 = { .dot_limit = 270000,
389 .p2_slow = 2, .p2_fast = 20 },
390 .find_pll = intel_vlv_find_best_pll,
391 };
392
393 static const intel_limit_t intel_limits_vlv_hdmi = {
394 .dot = { .min = 25000, .max = 270000 },
395 .vco = { .min = 4000000, .max = 6000000 },
396 .n = { .min = 1, .max = 7 },
397 .m = { .min = 60, .max = 300 }, /* guess */
398 .m1 = { .min = 2, .max = 3 },
399 .m2 = { .min = 11, .max = 156 },
400 .p = { .min = 10, .max = 30 },
401 .p1 = { .min = 2, .max = 3 },
402 .p2 = { .dot_limit = 270000,
403 .p2_slow = 2, .p2_fast = 20 },
404 .find_pll = intel_vlv_find_best_pll,
405 };
406
407 static const intel_limit_t intel_limits_vlv_dp = {
408 .dot = { .min = 25000, .max = 270000 },
409 .vco = { .min = 4000000, .max = 6000000 },
410 .n = { .min = 1, .max = 7 },
411 .m = { .min = 22, .max = 450 },
412 .m1 = { .min = 2, .max = 3 },
413 .m2 = { .min = 11, .max = 156 },
414 .p = { .min = 10, .max = 30 },
415 .p1 = { .min = 1, .max = 3 },
416 .p2 = { .dot_limit = 270000,
417 .p2_slow = 2, .p2_fast = 20 },
418 .find_pll = intel_vlv_find_best_pll,
419 };
420
421 u32 intel_dpio_read(struct drm_i915_private *dev_priv, int reg)
422 {
423 WARN_ON(!mutex_is_locked(&dev_priv->dpio_lock));
424
425 if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100)) {
426 DRM_ERROR("DPIO idle wait timed out\n");
427 return 0;
428 }
429
430 I915_WRITE(DPIO_REG, reg);
431 I915_WRITE(DPIO_PKT, DPIO_RID | DPIO_OP_READ | DPIO_PORTID |
432 DPIO_BYTE);
433 if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100)) {
434 DRM_ERROR("DPIO read wait timed out\n");
435 return 0;
436 }
437
438 return I915_READ(DPIO_DATA);
439 }
440
441 void intel_dpio_write(struct drm_i915_private *dev_priv, int reg, u32 val)
442 {
443 WARN_ON(!mutex_is_locked(&dev_priv->dpio_lock));
444
445 if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100)) {
446 DRM_ERROR("DPIO idle wait timed out\n");
447 return;
448 }
449
450 I915_WRITE(DPIO_DATA, val);
451 I915_WRITE(DPIO_REG, reg);
452 I915_WRITE(DPIO_PKT, DPIO_RID | DPIO_OP_WRITE | DPIO_PORTID |
453 DPIO_BYTE);
454 if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100))
455 DRM_ERROR("DPIO write wait timed out\n");
456 }
457
458 static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
459 int refclk)
460 {
461 struct drm_device *dev = crtc->dev;
462 const intel_limit_t *limit;
463
464 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
465 if (intel_is_dual_link_lvds(dev)) {
466 if (refclk == 100000)
467 limit = &intel_limits_ironlake_dual_lvds_100m;
468 else
469 limit = &intel_limits_ironlake_dual_lvds;
470 } else {
471 if (refclk == 100000)
472 limit = &intel_limits_ironlake_single_lvds_100m;
473 else
474 limit = &intel_limits_ironlake_single_lvds;
475 }
476 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
477 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
478 limit = &intel_limits_ironlake_display_port;
479 else
480 limit = &intel_limits_ironlake_dac;
481
482 return limit;
483 }
484
485 static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
486 {
487 struct drm_device *dev = crtc->dev;
488 const intel_limit_t *limit;
489
490 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
491 if (intel_is_dual_link_lvds(dev))
492 limit = &intel_limits_g4x_dual_channel_lvds;
493 else
494 limit = &intel_limits_g4x_single_channel_lvds;
495 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
496 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
497 limit = &intel_limits_g4x_hdmi;
498 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
499 limit = &intel_limits_g4x_sdvo;
500 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
501 limit = &intel_limits_g4x_display_port;
502 } else /* The option is for other outputs */
503 limit = &intel_limits_i9xx_sdvo;
504
505 return limit;
506 }
507
508 static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
509 {
510 struct drm_device *dev = crtc->dev;
511 const intel_limit_t *limit;
512
513 if (HAS_PCH_SPLIT(dev))
514 limit = intel_ironlake_limit(crtc, refclk);
515 else if (IS_G4X(dev)) {
516 limit = intel_g4x_limit(crtc);
517 } else if (IS_PINEVIEW(dev)) {
518 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
519 limit = &intel_limits_pineview_lvds;
520 else
521 limit = &intel_limits_pineview_sdvo;
522 } else if (IS_VALLEYVIEW(dev)) {
523 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG))
524 limit = &intel_limits_vlv_dac;
525 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
526 limit = &intel_limits_vlv_hdmi;
527 else
528 limit = &intel_limits_vlv_dp;
529 } else if (!IS_GEN2(dev)) {
530 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
531 limit = &intel_limits_i9xx_lvds;
532 else
533 limit = &intel_limits_i9xx_sdvo;
534 } else {
535 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
536 limit = &intel_limits_i8xx_lvds;
537 else
538 limit = &intel_limits_i8xx_dvo;
539 }
540 return limit;
541 }
542
543 /* m1 is reserved as 0 in Pineview, n is a ring counter */
544 static void pineview_clock(int refclk, intel_clock_t *clock)
545 {
546 clock->m = clock->m2 + 2;
547 clock->p = clock->p1 * clock->p2;
548 clock->vco = refclk * clock->m / clock->n;
549 clock->dot = clock->vco / clock->p;
550 }
551
552 static void intel_clock(struct drm_device *dev, int refclk, intel_clock_t *clock)
553 {
554 if (IS_PINEVIEW(dev)) {
555 pineview_clock(refclk, clock);
556 return;
557 }
558 clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
559 clock->p = clock->p1 * clock->p2;
560 clock->vco = refclk * clock->m / (clock->n + 2);
561 clock->dot = clock->vco / clock->p;
562 }
563
564 /**
565 * Returns whether any output on the specified pipe is of the specified type
566 */
567 bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
568 {
569 struct drm_device *dev = crtc->dev;
570 struct intel_encoder *encoder;
571
572 for_each_encoder_on_crtc(dev, crtc, encoder)
573 if (encoder->type == type)
574 return true;
575
576 return false;
577 }
578
579 #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
580 /**
581 * Returns whether the given set of divisors are valid for a given refclk with
582 * the given connectors.
583 */
584
585 static bool intel_PLL_is_valid(struct drm_device *dev,
586 const intel_limit_t *limit,
587 const intel_clock_t *clock)
588 {
589 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
590 INTELPllInvalid("p1 out of range\n");
591 if (clock->p < limit->p.min || limit->p.max < clock->p)
592 INTELPllInvalid("p out of range\n");
593 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
594 INTELPllInvalid("m2 out of range\n");
595 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
596 INTELPllInvalid("m1 out of range\n");
597 if (clock->m1 <= clock->m2 && !IS_PINEVIEW(dev))
598 INTELPllInvalid("m1 <= m2\n");
599 if (clock->m < limit->m.min || limit->m.max < clock->m)
600 INTELPllInvalid("m out of range\n");
601 if (clock->n < limit->n.min || limit->n.max < clock->n)
602 INTELPllInvalid("n out of range\n");
603 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
604 INTELPllInvalid("vco out of range\n");
605 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
606 * connector, etc., rather than just a single range.
607 */
608 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
609 INTELPllInvalid("dot out of range\n");
610
611 return true;
612 }
613
614 static bool
615 intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
616 int target, int refclk, intel_clock_t *match_clock,
617 intel_clock_t *best_clock)
618
619 {
620 struct drm_device *dev = crtc->dev;
621 intel_clock_t clock;
622 int err = target;
623
624 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
625 /*
626 * For LVDS just rely on its current settings for dual-channel.
627 * We haven't figured out how to reliably set up different
628 * single/dual channel state, if we even can.
629 */
630 if (intel_is_dual_link_lvds(dev))
631 clock.p2 = limit->p2.p2_fast;
632 else
633 clock.p2 = limit->p2.p2_slow;
634 } else {
635 if (target < limit->p2.dot_limit)
636 clock.p2 = limit->p2.p2_slow;
637 else
638 clock.p2 = limit->p2.p2_fast;
639 }
640
641 memset(best_clock, 0, sizeof(*best_clock));
642
643 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
644 clock.m1++) {
645 for (clock.m2 = limit->m2.min;
646 clock.m2 <= limit->m2.max; clock.m2++) {
647 /* m1 is always 0 in Pineview */
648 if (clock.m2 >= clock.m1 && !IS_PINEVIEW(dev))
649 break;
650 for (clock.n = limit->n.min;
651 clock.n <= limit->n.max; clock.n++) {
652 for (clock.p1 = limit->p1.min;
653 clock.p1 <= limit->p1.max; clock.p1++) {
654 int this_err;
655
656 intel_clock(dev, refclk, &clock);
657 if (!intel_PLL_is_valid(dev, limit,
658 &clock))
659 continue;
660 if (match_clock &&
661 clock.p != match_clock->p)
662 continue;
663
664 this_err = abs(clock.dot - target);
665 if (this_err < err) {
666 *best_clock = clock;
667 err = this_err;
668 }
669 }
670 }
671 }
672 }
673
674 return (err != target);
675 }
676
677 static bool
678 intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
679 int target, int refclk, intel_clock_t *match_clock,
680 intel_clock_t *best_clock)
681 {
682 struct drm_device *dev = crtc->dev;
683 intel_clock_t clock;
684 int max_n;
685 bool found;
686 /* approximately equals target * 0.00585 */
687 int err_most = (target >> 8) + (target >> 9);
688 found = false;
689
690 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
691 int lvds_reg;
692
693 if (HAS_PCH_SPLIT(dev))
694 lvds_reg = PCH_LVDS;
695 else
696 lvds_reg = LVDS;
697 if (intel_is_dual_link_lvds(dev))
698 clock.p2 = limit->p2.p2_fast;
699 else
700 clock.p2 = limit->p2.p2_slow;
701 } else {
702 if (target < limit->p2.dot_limit)
703 clock.p2 = limit->p2.p2_slow;
704 else
705 clock.p2 = limit->p2.p2_fast;
706 }
707
708 memset(best_clock, 0, sizeof(*best_clock));
709 max_n = limit->n.max;
710 /* based on hardware requirement, prefer smaller n to precision */
711 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
712 /* based on hardware requirement, prefere larger m1,m2 */
713 for (clock.m1 = limit->m1.max;
714 clock.m1 >= limit->m1.min; clock.m1--) {
715 for (clock.m2 = limit->m2.max;
716 clock.m2 >= limit->m2.min; clock.m2--) {
717 for (clock.p1 = limit->p1.max;
718 clock.p1 >= limit->p1.min; clock.p1--) {
719 int this_err;
720
721 intel_clock(dev, refclk, &clock);
722 if (!intel_PLL_is_valid(dev, limit,
723 &clock))
724 continue;
725 if (match_clock &&
726 clock.p != match_clock->p)
727 continue;
728
729 this_err = abs(clock.dot - target);
730 if (this_err < err_most) {
731 *best_clock = clock;
732 err_most = this_err;
733 max_n = clock.n;
734 found = true;
735 }
736 }
737 }
738 }
739 }
740 return found;
741 }
742
743 static bool
744 intel_find_pll_ironlake_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
745 int target, int refclk, intel_clock_t *match_clock,
746 intel_clock_t *best_clock)
747 {
748 struct drm_device *dev = crtc->dev;
749 intel_clock_t clock;
750
751 if (target < 200000) {
752 clock.n = 1;
753 clock.p1 = 2;
754 clock.p2 = 10;
755 clock.m1 = 12;
756 clock.m2 = 9;
757 } else {
758 clock.n = 2;
759 clock.p1 = 1;
760 clock.p2 = 10;
761 clock.m1 = 14;
762 clock.m2 = 8;
763 }
764 intel_clock(dev, refclk, &clock);
765 memcpy(best_clock, &clock, sizeof(intel_clock_t));
766 return true;
767 }
768
769 /* DisplayPort has only two frequencies, 162MHz and 270MHz */
770 static bool
771 intel_find_pll_g4x_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
772 int target, int refclk, intel_clock_t *match_clock,
773 intel_clock_t *best_clock)
774 {
775 intel_clock_t clock;
776 if (target < 200000) {
777 clock.p1 = 2;
778 clock.p2 = 10;
779 clock.n = 2;
780 clock.m1 = 23;
781 clock.m2 = 8;
782 } else {
783 clock.p1 = 1;
784 clock.p2 = 10;
785 clock.n = 1;
786 clock.m1 = 14;
787 clock.m2 = 2;
788 }
789 clock.m = 5 * (clock.m1 + 2) + (clock.m2 + 2);
790 clock.p = (clock.p1 * clock.p2);
791 clock.dot = 96000 * clock.m / (clock.n + 2) / clock.p;
792 clock.vco = 0;
793 memcpy(best_clock, &clock, sizeof(intel_clock_t));
794 return true;
795 }
796 static bool
797 intel_vlv_find_best_pll(const intel_limit_t *limit, struct drm_crtc *crtc,
798 int target, int refclk, intel_clock_t *match_clock,
799 intel_clock_t *best_clock)
800 {
801 u32 p1, p2, m1, m2, vco, bestn, bestm1, bestm2, bestp1, bestp2;
802 u32 m, n, fastclk;
803 u32 updrate, minupdate, fracbits, p;
804 unsigned long bestppm, ppm, absppm;
805 int dotclk, flag;
806
807 flag = 0;
808 dotclk = target * 1000;
809 bestppm = 1000000;
810 ppm = absppm = 0;
811 fastclk = dotclk / (2*100);
812 updrate = 0;
813 minupdate = 19200;
814 fracbits = 1;
815 n = p = p1 = p2 = m = m1 = m2 = vco = bestn = 0;
816 bestm1 = bestm2 = bestp1 = bestp2 = 0;
817
818 /* based on hardware requirement, prefer smaller n to precision */
819 for (n = limit->n.min; n <= ((refclk) / minupdate); n++) {
820 updrate = refclk / n;
821 for (p1 = limit->p1.max; p1 > limit->p1.min; p1--) {
822 for (p2 = limit->p2.p2_fast+1; p2 > 0; p2--) {
823 if (p2 > 10)
824 p2 = p2 - 1;
825 p = p1 * p2;
826 /* based on hardware requirement, prefer bigger m1,m2 values */
827 for (m1 = limit->m1.min; m1 <= limit->m1.max; m1++) {
828 m2 = (((2*(fastclk * p * n / m1 )) +
829 refclk) / (2*refclk));
830 m = m1 * m2;
831 vco = updrate * m;
832 if (vco >= limit->vco.min && vco < limit->vco.max) {
833 ppm = 1000000 * ((vco / p) - fastclk) / fastclk;
834 absppm = (ppm > 0) ? ppm : (-ppm);
835 if (absppm < 100 && ((p1 * p2) > (bestp1 * bestp2))) {
836 bestppm = 0;
837 flag = 1;
838 }
839 if (absppm < bestppm - 10) {
840 bestppm = absppm;
841 flag = 1;
842 }
843 if (flag) {
844 bestn = n;
845 bestm1 = m1;
846 bestm2 = m2;
847 bestp1 = p1;
848 bestp2 = p2;
849 flag = 0;
850 }
851 }
852 }
853 }
854 }
855 }
856 best_clock->n = bestn;
857 best_clock->m1 = bestm1;
858 best_clock->m2 = bestm2;
859 best_clock->p1 = bestp1;
860 best_clock->p2 = bestp2;
861
862 return true;
863 }
864
865 enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
866 enum pipe pipe)
867 {
868 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
869 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
870
871 return intel_crtc->config.cpu_transcoder;
872 }
873
874 static void ironlake_wait_for_vblank(struct drm_device *dev, int pipe)
875 {
876 struct drm_i915_private *dev_priv = dev->dev_private;
877 u32 frame, frame_reg = PIPEFRAME(pipe);
878
879 frame = I915_READ(frame_reg);
880
881 if (wait_for(I915_READ_NOTRACE(frame_reg) != frame, 50))
882 DRM_DEBUG_KMS("vblank wait timed out\n");
883 }
884
885 /**
886 * intel_wait_for_vblank - wait for vblank on a given pipe
887 * @dev: drm device
888 * @pipe: pipe to wait for
889 *
890 * Wait for vblank to occur on a given pipe. Needed for various bits of
891 * mode setting code.
892 */
893 void intel_wait_for_vblank(struct drm_device *dev, int pipe)
894 {
895 struct drm_i915_private *dev_priv = dev->dev_private;
896 int pipestat_reg = PIPESTAT(pipe);
897
898 if (INTEL_INFO(dev)->gen >= 5) {
899 ironlake_wait_for_vblank(dev, pipe);
900 return;
901 }
902
903 /* Clear existing vblank status. Note this will clear any other
904 * sticky status fields as well.
905 *
906 * This races with i915_driver_irq_handler() with the result
907 * that either function could miss a vblank event. Here it is not
908 * fatal, as we will either wait upon the next vblank interrupt or
909 * timeout. Generally speaking intel_wait_for_vblank() is only
910 * called during modeset at which time the GPU should be idle and
911 * should *not* be performing page flips and thus not waiting on
912 * vblanks...
913 * Currently, the result of us stealing a vblank from the irq
914 * handler is that a single frame will be skipped during swapbuffers.
915 */
916 I915_WRITE(pipestat_reg,
917 I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
918
919 /* Wait for vblank interrupt bit to set */
920 if (wait_for(I915_READ(pipestat_reg) &
921 PIPE_VBLANK_INTERRUPT_STATUS,
922 50))
923 DRM_DEBUG_KMS("vblank wait timed out\n");
924 }
925
926 /*
927 * intel_wait_for_pipe_off - wait for pipe to turn off
928 * @dev: drm device
929 * @pipe: pipe to wait for
930 *
931 * After disabling a pipe, we can't wait for vblank in the usual way,
932 * spinning on the vblank interrupt status bit, since we won't actually
933 * see an interrupt when the pipe is disabled.
934 *
935 * On Gen4 and above:
936 * wait for the pipe register state bit to turn off
937 *
938 * Otherwise:
939 * wait for the display line value to settle (it usually
940 * ends up stopping at the start of the next frame).
941 *
942 */
943 void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
944 {
945 struct drm_i915_private *dev_priv = dev->dev_private;
946 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
947 pipe);
948
949 if (INTEL_INFO(dev)->gen >= 4) {
950 int reg = PIPECONF(cpu_transcoder);
951
952 /* Wait for the Pipe State to go off */
953 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
954 100))
955 WARN(1, "pipe_off wait timed out\n");
956 } else {
957 u32 last_line, line_mask;
958 int reg = PIPEDSL(pipe);
959 unsigned long timeout = jiffies + msecs_to_jiffies(100);
960
961 if (IS_GEN2(dev))
962 line_mask = DSL_LINEMASK_GEN2;
963 else
964 line_mask = DSL_LINEMASK_GEN3;
965
966 /* Wait for the display line to settle */
967 do {
968 last_line = I915_READ(reg) & line_mask;
969 mdelay(5);
970 } while (((I915_READ(reg) & line_mask) != last_line) &&
971 time_after(timeout, jiffies));
972 if (time_after(jiffies, timeout))
973 WARN(1, "pipe_off wait timed out\n");
974 }
975 }
976
977 /*
978 * ibx_digital_port_connected - is the specified port connected?
979 * @dev_priv: i915 private structure
980 * @port: the port to test
981 *
982 * Returns true if @port is connected, false otherwise.
983 */
984 bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
985 struct intel_digital_port *port)
986 {
987 u32 bit;
988
989 if (HAS_PCH_IBX(dev_priv->dev)) {
990 switch(port->port) {
991 case PORT_B:
992 bit = SDE_PORTB_HOTPLUG;
993 break;
994 case PORT_C:
995 bit = SDE_PORTC_HOTPLUG;
996 break;
997 case PORT_D:
998 bit = SDE_PORTD_HOTPLUG;
999 break;
1000 default:
1001 return true;
1002 }
1003 } else {
1004 switch(port->port) {
1005 case PORT_B:
1006 bit = SDE_PORTB_HOTPLUG_CPT;
1007 break;
1008 case PORT_C:
1009 bit = SDE_PORTC_HOTPLUG_CPT;
1010 break;
1011 case PORT_D:
1012 bit = SDE_PORTD_HOTPLUG_CPT;
1013 break;
1014 default:
1015 return true;
1016 }
1017 }
1018
1019 return I915_READ(SDEISR) & bit;
1020 }
1021
1022 static const char *state_string(bool enabled)
1023 {
1024 return enabled ? "on" : "off";
1025 }
1026
1027 /* Only for pre-ILK configs */
1028 static void assert_pll(struct drm_i915_private *dev_priv,
1029 enum pipe pipe, bool state)
1030 {
1031 int reg;
1032 u32 val;
1033 bool cur_state;
1034
1035 reg = DPLL(pipe);
1036 val = I915_READ(reg);
1037 cur_state = !!(val & DPLL_VCO_ENABLE);
1038 WARN(cur_state != state,
1039 "PLL state assertion failure (expected %s, current %s)\n",
1040 state_string(state), state_string(cur_state));
1041 }
1042 #define assert_pll_enabled(d, p) assert_pll(d, p, true)
1043 #define assert_pll_disabled(d, p) assert_pll(d, p, false)
1044
1045 /* For ILK+ */
1046 static void assert_pch_pll(struct drm_i915_private *dev_priv,
1047 struct intel_pch_pll *pll,
1048 struct intel_crtc *crtc,
1049 bool state)
1050 {
1051 u32 val;
1052 bool cur_state;
1053
1054 if (HAS_PCH_LPT(dev_priv->dev)) {
1055 DRM_DEBUG_DRIVER("LPT detected: skipping PCH PLL test\n");
1056 return;
1057 }
1058
1059 if (WARN (!pll,
1060 "asserting PCH PLL %s with no PLL\n", state_string(state)))
1061 return;
1062
1063 val = I915_READ(pll->pll_reg);
1064 cur_state = !!(val & DPLL_VCO_ENABLE);
1065 WARN(cur_state != state,
1066 "PCH PLL state for reg %x assertion failure (expected %s, current %s), val=%08x\n",
1067 pll->pll_reg, state_string(state), state_string(cur_state), val);
1068
1069 /* Make sure the selected PLL is correctly attached to the transcoder */
1070 if (crtc && HAS_PCH_CPT(dev_priv->dev)) {
1071 u32 pch_dpll;
1072
1073 pch_dpll = I915_READ(PCH_DPLL_SEL);
1074 cur_state = pll->pll_reg == _PCH_DPLL_B;
1075 if (!WARN(((pch_dpll >> (4 * crtc->pipe)) & 1) != cur_state,
1076 "PLL[%d] not attached to this transcoder %c: %08x\n",
1077 cur_state, pipe_name(crtc->pipe), pch_dpll)) {
1078 cur_state = !!(val >> (4*crtc->pipe + 3));
1079 WARN(cur_state != state,
1080 "PLL[%d] not %s on this transcoder %c: %08x\n",
1081 pll->pll_reg == _PCH_DPLL_B,
1082 state_string(state),
1083 pipe_name(crtc->pipe),
1084 val);
1085 }
1086 }
1087 }
1088 #define assert_pch_pll_enabled(d, p, c) assert_pch_pll(d, p, c, true)
1089 #define assert_pch_pll_disabled(d, p, c) assert_pch_pll(d, p, c, false)
1090
1091 static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1092 enum pipe pipe, bool state)
1093 {
1094 int reg;
1095 u32 val;
1096 bool cur_state;
1097 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1098 pipe);
1099
1100 if (HAS_DDI(dev_priv->dev)) {
1101 /* DDI does not have a specific FDI_TX register */
1102 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
1103 val = I915_READ(reg);
1104 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
1105 } else {
1106 reg = FDI_TX_CTL(pipe);
1107 val = I915_READ(reg);
1108 cur_state = !!(val & FDI_TX_ENABLE);
1109 }
1110 WARN(cur_state != state,
1111 "FDI TX state assertion failure (expected %s, current %s)\n",
1112 state_string(state), state_string(cur_state));
1113 }
1114 #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1115 #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1116
1117 static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1118 enum pipe pipe, bool state)
1119 {
1120 int reg;
1121 u32 val;
1122 bool cur_state;
1123
1124 reg = FDI_RX_CTL(pipe);
1125 val = I915_READ(reg);
1126 cur_state = !!(val & FDI_RX_ENABLE);
1127 WARN(cur_state != state,
1128 "FDI RX state assertion failure (expected %s, current %s)\n",
1129 state_string(state), state_string(cur_state));
1130 }
1131 #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1132 #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1133
1134 static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1135 enum pipe pipe)
1136 {
1137 int reg;
1138 u32 val;
1139
1140 /* ILK FDI PLL is always enabled */
1141 if (dev_priv->info->gen == 5)
1142 return;
1143
1144 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
1145 if (HAS_DDI(dev_priv->dev))
1146 return;
1147
1148 reg = FDI_TX_CTL(pipe);
1149 val = I915_READ(reg);
1150 WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
1151 }
1152
1153 static void assert_fdi_rx_pll_enabled(struct drm_i915_private *dev_priv,
1154 enum pipe pipe)
1155 {
1156 int reg;
1157 u32 val;
1158
1159 reg = FDI_RX_CTL(pipe);
1160 val = I915_READ(reg);
1161 WARN(!(val & FDI_RX_PLL_ENABLE), "FDI RX PLL assertion failure, should be active but is disabled\n");
1162 }
1163
1164 static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1165 enum pipe pipe)
1166 {
1167 int pp_reg, lvds_reg;
1168 u32 val;
1169 enum pipe panel_pipe = PIPE_A;
1170 bool locked = true;
1171
1172 if (HAS_PCH_SPLIT(dev_priv->dev)) {
1173 pp_reg = PCH_PP_CONTROL;
1174 lvds_reg = PCH_LVDS;
1175 } else {
1176 pp_reg = PP_CONTROL;
1177 lvds_reg = LVDS;
1178 }
1179
1180 val = I915_READ(pp_reg);
1181 if (!(val & PANEL_POWER_ON) ||
1182 ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
1183 locked = false;
1184
1185 if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
1186 panel_pipe = PIPE_B;
1187
1188 WARN(panel_pipe == pipe && locked,
1189 "panel assertion failure, pipe %c regs locked\n",
1190 pipe_name(pipe));
1191 }
1192
1193 void assert_pipe(struct drm_i915_private *dev_priv,
1194 enum pipe pipe, bool state)
1195 {
1196 int reg;
1197 u32 val;
1198 bool cur_state;
1199 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1200 pipe);
1201
1202 /* if we need the pipe A quirk it must be always on */
1203 if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
1204 state = true;
1205
1206 if (!intel_using_power_well(dev_priv->dev) &&
1207 cpu_transcoder != TRANSCODER_EDP) {
1208 cur_state = false;
1209 } else {
1210 reg = PIPECONF(cpu_transcoder);
1211 val = I915_READ(reg);
1212 cur_state = !!(val & PIPECONF_ENABLE);
1213 }
1214
1215 WARN(cur_state != state,
1216 "pipe %c assertion failure (expected %s, current %s)\n",
1217 pipe_name(pipe), state_string(state), state_string(cur_state));
1218 }
1219
1220 static void assert_plane(struct drm_i915_private *dev_priv,
1221 enum plane plane, bool state)
1222 {
1223 int reg;
1224 u32 val;
1225 bool cur_state;
1226
1227 reg = DSPCNTR(plane);
1228 val = I915_READ(reg);
1229 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
1230 WARN(cur_state != state,
1231 "plane %c assertion failure (expected %s, current %s)\n",
1232 plane_name(plane), state_string(state), state_string(cur_state));
1233 }
1234
1235 #define assert_plane_enabled(d, p) assert_plane(d, p, true)
1236 #define assert_plane_disabled(d, p) assert_plane(d, p, false)
1237
1238 static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1239 enum pipe pipe)
1240 {
1241 int reg, i;
1242 u32 val;
1243 int cur_pipe;
1244
1245 /* Planes are fixed to pipes on ILK+ */
1246 if (HAS_PCH_SPLIT(dev_priv->dev) || IS_VALLEYVIEW(dev_priv->dev)) {
1247 reg = DSPCNTR(pipe);
1248 val = I915_READ(reg);
1249 WARN((val & DISPLAY_PLANE_ENABLE),
1250 "plane %c assertion failure, should be disabled but not\n",
1251 plane_name(pipe));
1252 return;
1253 }
1254
1255 /* Need to check both planes against the pipe */
1256 for (i = 0; i < 2; i++) {
1257 reg = DSPCNTR(i);
1258 val = I915_READ(reg);
1259 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1260 DISPPLANE_SEL_PIPE_SHIFT;
1261 WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
1262 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1263 plane_name(i), pipe_name(pipe));
1264 }
1265 }
1266
1267 static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1268 enum pipe pipe)
1269 {
1270 int reg, i;
1271 u32 val;
1272
1273 if (!IS_VALLEYVIEW(dev_priv->dev))
1274 return;
1275
1276 /* Need to check both planes against the pipe */
1277 for (i = 0; i < dev_priv->num_plane; i++) {
1278 reg = SPCNTR(pipe, i);
1279 val = I915_READ(reg);
1280 WARN((val & SP_ENABLE),
1281 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1282 sprite_name(pipe, i), pipe_name(pipe));
1283 }
1284 }
1285
1286 static void assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
1287 {
1288 u32 val;
1289 bool enabled;
1290
1291 if (HAS_PCH_LPT(dev_priv->dev)) {
1292 DRM_DEBUG_DRIVER("LPT does not has PCH refclk, skipping check\n");
1293 return;
1294 }
1295
1296 val = I915_READ(PCH_DREF_CONTROL);
1297 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1298 DREF_SUPERSPREAD_SOURCE_MASK));
1299 WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
1300 }
1301
1302 static void assert_transcoder_disabled(struct drm_i915_private *dev_priv,
1303 enum pipe pipe)
1304 {
1305 int reg;
1306 u32 val;
1307 bool enabled;
1308
1309 reg = TRANSCONF(pipe);
1310 val = I915_READ(reg);
1311 enabled = !!(val & TRANS_ENABLE);
1312 WARN(enabled,
1313 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1314 pipe_name(pipe));
1315 }
1316
1317 static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1318 enum pipe pipe, u32 port_sel, u32 val)
1319 {
1320 if ((val & DP_PORT_EN) == 0)
1321 return false;
1322
1323 if (HAS_PCH_CPT(dev_priv->dev)) {
1324 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1325 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1326 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1327 return false;
1328 } else {
1329 if ((val & DP_PIPE_MASK) != (pipe << 30))
1330 return false;
1331 }
1332 return true;
1333 }
1334
1335 static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1336 enum pipe pipe, u32 val)
1337 {
1338 if ((val & SDVO_ENABLE) == 0)
1339 return false;
1340
1341 if (HAS_PCH_CPT(dev_priv->dev)) {
1342 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
1343 return false;
1344 } else {
1345 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
1346 return false;
1347 }
1348 return true;
1349 }
1350
1351 static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1352 enum pipe pipe, u32 val)
1353 {
1354 if ((val & LVDS_PORT_EN) == 0)
1355 return false;
1356
1357 if (HAS_PCH_CPT(dev_priv->dev)) {
1358 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1359 return false;
1360 } else {
1361 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1362 return false;
1363 }
1364 return true;
1365 }
1366
1367 static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1368 enum pipe pipe, u32 val)
1369 {
1370 if ((val & ADPA_DAC_ENABLE) == 0)
1371 return false;
1372 if (HAS_PCH_CPT(dev_priv->dev)) {
1373 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1374 return false;
1375 } else {
1376 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1377 return false;
1378 }
1379 return true;
1380 }
1381
1382 static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
1383 enum pipe pipe, int reg, u32 port_sel)
1384 {
1385 u32 val = I915_READ(reg);
1386 WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
1387 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
1388 reg, pipe_name(pipe));
1389
1390 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
1391 && (val & DP_PIPEB_SELECT),
1392 "IBX PCH dp port still using transcoder B\n");
1393 }
1394
1395 static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1396 enum pipe pipe, int reg)
1397 {
1398 u32 val = I915_READ(reg);
1399 WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
1400 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
1401 reg, pipe_name(pipe));
1402
1403 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
1404 && (val & SDVO_PIPE_B_SELECT),
1405 "IBX PCH hdmi port still using transcoder B\n");
1406 }
1407
1408 static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1409 enum pipe pipe)
1410 {
1411 int reg;
1412 u32 val;
1413
1414 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1415 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1416 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
1417
1418 reg = PCH_ADPA;
1419 val = I915_READ(reg);
1420 WARN(adpa_pipe_enabled(dev_priv, pipe, val),
1421 "PCH VGA enabled on transcoder %c, should be disabled\n",
1422 pipe_name(pipe));
1423
1424 reg = PCH_LVDS;
1425 val = I915_READ(reg);
1426 WARN(lvds_pipe_enabled(dev_priv, pipe, val),
1427 "PCH LVDS enabled on transcoder %c, should be disabled\n",
1428 pipe_name(pipe));
1429
1430 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1431 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1432 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
1433 }
1434
1435 /**
1436 * intel_enable_pll - enable a PLL
1437 * @dev_priv: i915 private structure
1438 * @pipe: pipe PLL to enable
1439 *
1440 * Enable @pipe's PLL so we can start pumping pixels from a plane. Check to
1441 * make sure the PLL reg is writable first though, since the panel write
1442 * protect mechanism may be enabled.
1443 *
1444 * Note! This is for pre-ILK only.
1445 *
1446 * Unfortunately needed by dvo_ns2501 since the dvo depends on it running.
1447 */
1448 static void intel_enable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1449 {
1450 int reg;
1451 u32 val;
1452
1453 assert_pipe_disabled(dev_priv, pipe);
1454
1455 /* No really, not for ILK+ */
1456 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev) && dev_priv->info->gen >= 5);
1457
1458 /* PLL is protected by panel, make sure we can write it */
1459 if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
1460 assert_panel_unlocked(dev_priv, pipe);
1461
1462 reg = DPLL(pipe);
1463 val = I915_READ(reg);
1464 val |= DPLL_VCO_ENABLE;
1465
1466 /* We do this three times for luck */
1467 I915_WRITE(reg, val);
1468 POSTING_READ(reg);
1469 udelay(150); /* wait for warmup */
1470 I915_WRITE(reg, val);
1471 POSTING_READ(reg);
1472 udelay(150); /* wait for warmup */
1473 I915_WRITE(reg, val);
1474 POSTING_READ(reg);
1475 udelay(150); /* wait for warmup */
1476 }
1477
1478 /**
1479 * intel_disable_pll - disable a PLL
1480 * @dev_priv: i915 private structure
1481 * @pipe: pipe PLL to disable
1482 *
1483 * Disable the PLL for @pipe, making sure the pipe is off first.
1484 *
1485 * Note! This is for pre-ILK only.
1486 */
1487 static void intel_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1488 {
1489 int reg;
1490 u32 val;
1491
1492 /* Don't disable pipe A or pipe A PLLs if needed */
1493 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1494 return;
1495
1496 /* Make sure the pipe isn't still relying on us */
1497 assert_pipe_disabled(dev_priv, pipe);
1498
1499 reg = DPLL(pipe);
1500 val = I915_READ(reg);
1501 val &= ~DPLL_VCO_ENABLE;
1502 I915_WRITE(reg, val);
1503 POSTING_READ(reg);
1504 }
1505
1506 /* SBI access */
1507 static void
1508 intel_sbi_write(struct drm_i915_private *dev_priv, u16 reg, u32 value,
1509 enum intel_sbi_destination destination)
1510 {
1511 u32 tmp;
1512
1513 WARN_ON(!mutex_is_locked(&dev_priv->dpio_lock));
1514
1515 if (wait_for((I915_READ(SBI_CTL_STAT) & SBI_BUSY) == 0,
1516 100)) {
1517 DRM_ERROR("timeout waiting for SBI to become ready\n");
1518 return;
1519 }
1520
1521 I915_WRITE(SBI_ADDR, (reg << 16));
1522 I915_WRITE(SBI_DATA, value);
1523
1524 if (destination == SBI_ICLK)
1525 tmp = SBI_CTL_DEST_ICLK | SBI_CTL_OP_CRWR;
1526 else
1527 tmp = SBI_CTL_DEST_MPHY | SBI_CTL_OP_IOWR;
1528 I915_WRITE(SBI_CTL_STAT, SBI_BUSY | tmp);
1529
1530 if (wait_for((I915_READ(SBI_CTL_STAT) & (SBI_BUSY | SBI_RESPONSE_FAIL)) == 0,
1531 100)) {
1532 DRM_ERROR("timeout waiting for SBI to complete write transaction\n");
1533 return;
1534 }
1535 }
1536
1537 static u32
1538 intel_sbi_read(struct drm_i915_private *dev_priv, u16 reg,
1539 enum intel_sbi_destination destination)
1540 {
1541 u32 value = 0;
1542 WARN_ON(!mutex_is_locked(&dev_priv->dpio_lock));
1543
1544 if (wait_for((I915_READ(SBI_CTL_STAT) & SBI_BUSY) == 0,
1545 100)) {
1546 DRM_ERROR("timeout waiting for SBI to become ready\n");
1547 return 0;
1548 }
1549
1550 I915_WRITE(SBI_ADDR, (reg << 16));
1551
1552 if (destination == SBI_ICLK)
1553 value = SBI_CTL_DEST_ICLK | SBI_CTL_OP_CRRD;
1554 else
1555 value = SBI_CTL_DEST_MPHY | SBI_CTL_OP_IORD;
1556 I915_WRITE(SBI_CTL_STAT, value | SBI_BUSY);
1557
1558 if (wait_for((I915_READ(SBI_CTL_STAT) & (SBI_BUSY | SBI_RESPONSE_FAIL)) == 0,
1559 100)) {
1560 DRM_ERROR("timeout waiting for SBI to complete read transaction\n");
1561 return 0;
1562 }
1563
1564 return I915_READ(SBI_DATA);
1565 }
1566
1567 void vlv_wait_port_ready(struct drm_i915_private *dev_priv, int port)
1568 {
1569 u32 port_mask;
1570
1571 if (!port)
1572 port_mask = DPLL_PORTB_READY_MASK;
1573 else
1574 port_mask = DPLL_PORTC_READY_MASK;
1575
1576 if (wait_for((I915_READ(DPLL(0)) & port_mask) == 0, 1000))
1577 WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
1578 'B' + port, I915_READ(DPLL(0)));
1579 }
1580
1581 /**
1582 * ironlake_enable_pch_pll - enable PCH PLL
1583 * @dev_priv: i915 private structure
1584 * @pipe: pipe PLL to enable
1585 *
1586 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1587 * drives the transcoder clock.
1588 */
1589 static void ironlake_enable_pch_pll(struct intel_crtc *intel_crtc)
1590 {
1591 struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
1592 struct intel_pch_pll *pll;
1593 int reg;
1594 u32 val;
1595
1596 /* PCH PLLs only available on ILK, SNB and IVB */
1597 BUG_ON(dev_priv->info->gen < 5);
1598 pll = intel_crtc->pch_pll;
1599 if (pll == NULL)
1600 return;
1601
1602 if (WARN_ON(pll->refcount == 0))
1603 return;
1604
1605 DRM_DEBUG_KMS("enable PCH PLL %x (active %d, on? %d)for crtc %d\n",
1606 pll->pll_reg, pll->active, pll->on,
1607 intel_crtc->base.base.id);
1608
1609 /* PCH refclock must be enabled first */
1610 assert_pch_refclk_enabled(dev_priv);
1611
1612 if (pll->active++ && pll->on) {
1613 assert_pch_pll_enabled(dev_priv, pll, NULL);
1614 return;
1615 }
1616
1617 DRM_DEBUG_KMS("enabling PCH PLL %x\n", pll->pll_reg);
1618
1619 reg = pll->pll_reg;
1620 val = I915_READ(reg);
1621 val |= DPLL_VCO_ENABLE;
1622 I915_WRITE(reg, val);
1623 POSTING_READ(reg);
1624 udelay(200);
1625
1626 pll->on = true;
1627 }
1628
1629 static void intel_disable_pch_pll(struct intel_crtc *intel_crtc)
1630 {
1631 struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
1632 struct intel_pch_pll *pll = intel_crtc->pch_pll;
1633 int reg;
1634 u32 val;
1635
1636 /* PCH only available on ILK+ */
1637 BUG_ON(dev_priv->info->gen < 5);
1638 if (pll == NULL)
1639 return;
1640
1641 if (WARN_ON(pll->refcount == 0))
1642 return;
1643
1644 DRM_DEBUG_KMS("disable PCH PLL %x (active %d, on? %d) for crtc %d\n",
1645 pll->pll_reg, pll->active, pll->on,
1646 intel_crtc->base.base.id);
1647
1648 if (WARN_ON(pll->active == 0)) {
1649 assert_pch_pll_disabled(dev_priv, pll, NULL);
1650 return;
1651 }
1652
1653 if (--pll->active) {
1654 assert_pch_pll_enabled(dev_priv, pll, NULL);
1655 return;
1656 }
1657
1658 DRM_DEBUG_KMS("disabling PCH PLL %x\n", pll->pll_reg);
1659
1660 /* Make sure transcoder isn't still depending on us */
1661 assert_transcoder_disabled(dev_priv, intel_crtc->pipe);
1662
1663 reg = pll->pll_reg;
1664 val = I915_READ(reg);
1665 val &= ~DPLL_VCO_ENABLE;
1666 I915_WRITE(reg, val);
1667 POSTING_READ(reg);
1668 udelay(200);
1669
1670 pll->on = false;
1671 }
1672
1673 static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1674 enum pipe pipe)
1675 {
1676 struct drm_device *dev = dev_priv->dev;
1677 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1678 uint32_t reg, val, pipeconf_val;
1679
1680 /* PCH only available on ILK+ */
1681 BUG_ON(dev_priv->info->gen < 5);
1682
1683 /* Make sure PCH DPLL is enabled */
1684 assert_pch_pll_enabled(dev_priv,
1685 to_intel_crtc(crtc)->pch_pll,
1686 to_intel_crtc(crtc));
1687
1688 /* FDI must be feeding us bits for PCH ports */
1689 assert_fdi_tx_enabled(dev_priv, pipe);
1690 assert_fdi_rx_enabled(dev_priv, pipe);
1691
1692 if (HAS_PCH_CPT(dev)) {
1693 /* Workaround: Set the timing override bit before enabling the
1694 * pch transcoder. */
1695 reg = TRANS_CHICKEN2(pipe);
1696 val = I915_READ(reg);
1697 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1698 I915_WRITE(reg, val);
1699 }
1700
1701 reg = TRANSCONF(pipe);
1702 val = I915_READ(reg);
1703 pipeconf_val = I915_READ(PIPECONF(pipe));
1704
1705 if (HAS_PCH_IBX(dev_priv->dev)) {
1706 /*
1707 * make the BPC in transcoder be consistent with
1708 * that in pipeconf reg.
1709 */
1710 val &= ~PIPECONF_BPC_MASK;
1711 val |= pipeconf_val & PIPECONF_BPC_MASK;
1712 }
1713
1714 val &= ~TRANS_INTERLACE_MASK;
1715 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
1716 if (HAS_PCH_IBX(dev_priv->dev) &&
1717 intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
1718 val |= TRANS_LEGACY_INTERLACED_ILK;
1719 else
1720 val |= TRANS_INTERLACED;
1721 else
1722 val |= TRANS_PROGRESSIVE;
1723
1724 I915_WRITE(reg, val | TRANS_ENABLE);
1725 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
1726 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
1727 }
1728
1729 static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1730 enum transcoder cpu_transcoder)
1731 {
1732 u32 val, pipeconf_val;
1733
1734 /* PCH only available on ILK+ */
1735 BUG_ON(dev_priv->info->gen < 5);
1736
1737 /* FDI must be feeding us bits for PCH ports */
1738 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
1739 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
1740
1741 /* Workaround: set timing override bit. */
1742 val = I915_READ(_TRANSA_CHICKEN2);
1743 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1744 I915_WRITE(_TRANSA_CHICKEN2, val);
1745
1746 val = TRANS_ENABLE;
1747 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
1748
1749 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1750 PIPECONF_INTERLACED_ILK)
1751 val |= TRANS_INTERLACED;
1752 else
1753 val |= TRANS_PROGRESSIVE;
1754
1755 I915_WRITE(TRANSCONF(TRANSCODER_A), val);
1756 if (wait_for(I915_READ(_TRANSACONF) & TRANS_STATE_ENABLE, 100))
1757 DRM_ERROR("Failed to enable PCH transcoder\n");
1758 }
1759
1760 static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1761 enum pipe pipe)
1762 {
1763 struct drm_device *dev = dev_priv->dev;
1764 uint32_t reg, val;
1765
1766 /* FDI relies on the transcoder */
1767 assert_fdi_tx_disabled(dev_priv, pipe);
1768 assert_fdi_rx_disabled(dev_priv, pipe);
1769
1770 /* Ports must be off as well */
1771 assert_pch_ports_disabled(dev_priv, pipe);
1772
1773 reg = TRANSCONF(pipe);
1774 val = I915_READ(reg);
1775 val &= ~TRANS_ENABLE;
1776 I915_WRITE(reg, val);
1777 /* wait for PCH transcoder off, transcoder state */
1778 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
1779 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
1780
1781 if (!HAS_PCH_IBX(dev)) {
1782 /* Workaround: Clear the timing override chicken bit again. */
1783 reg = TRANS_CHICKEN2(pipe);
1784 val = I915_READ(reg);
1785 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1786 I915_WRITE(reg, val);
1787 }
1788 }
1789
1790 static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
1791 {
1792 u32 val;
1793
1794 val = I915_READ(_TRANSACONF);
1795 val &= ~TRANS_ENABLE;
1796 I915_WRITE(_TRANSACONF, val);
1797 /* wait for PCH transcoder off, transcoder state */
1798 if (wait_for((I915_READ(_TRANSACONF) & TRANS_STATE_ENABLE) == 0, 50))
1799 DRM_ERROR("Failed to disable PCH transcoder\n");
1800
1801 /* Workaround: clear timing override bit. */
1802 val = I915_READ(_TRANSA_CHICKEN2);
1803 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1804 I915_WRITE(_TRANSA_CHICKEN2, val);
1805 }
1806
1807 /**
1808 * intel_enable_pipe - enable a pipe, asserting requirements
1809 * @dev_priv: i915 private structure
1810 * @pipe: pipe to enable
1811 * @pch_port: on ILK+, is this pipe driving a PCH port or not
1812 *
1813 * Enable @pipe, making sure that various hardware specific requirements
1814 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
1815 *
1816 * @pipe should be %PIPE_A or %PIPE_B.
1817 *
1818 * Will wait until the pipe is actually running (i.e. first vblank) before
1819 * returning.
1820 */
1821 static void intel_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe,
1822 bool pch_port)
1823 {
1824 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1825 pipe);
1826 enum pipe pch_transcoder;
1827 int reg;
1828 u32 val;
1829
1830 assert_planes_disabled(dev_priv, pipe);
1831 assert_sprites_disabled(dev_priv, pipe);
1832
1833 if (HAS_PCH_LPT(dev_priv->dev))
1834 pch_transcoder = TRANSCODER_A;
1835 else
1836 pch_transcoder = pipe;
1837
1838 /*
1839 * A pipe without a PLL won't actually be able to drive bits from
1840 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1841 * need the check.
1842 */
1843 if (!HAS_PCH_SPLIT(dev_priv->dev))
1844 assert_pll_enabled(dev_priv, pipe);
1845 else {
1846 if (pch_port) {
1847 /* if driving the PCH, we need FDI enabled */
1848 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
1849 assert_fdi_tx_pll_enabled(dev_priv,
1850 (enum pipe) cpu_transcoder);
1851 }
1852 /* FIXME: assert CPU port conditions for SNB+ */
1853 }
1854
1855 reg = PIPECONF(cpu_transcoder);
1856 val = I915_READ(reg);
1857 if (val & PIPECONF_ENABLE)
1858 return;
1859
1860 I915_WRITE(reg, val | PIPECONF_ENABLE);
1861 intel_wait_for_vblank(dev_priv->dev, pipe);
1862 }
1863
1864 /**
1865 * intel_disable_pipe - disable a pipe, asserting requirements
1866 * @dev_priv: i915 private structure
1867 * @pipe: pipe to disable
1868 *
1869 * Disable @pipe, making sure that various hardware specific requirements
1870 * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
1871 *
1872 * @pipe should be %PIPE_A or %PIPE_B.
1873 *
1874 * Will wait until the pipe has shut down before returning.
1875 */
1876 static void intel_disable_pipe(struct drm_i915_private *dev_priv,
1877 enum pipe pipe)
1878 {
1879 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1880 pipe);
1881 int reg;
1882 u32 val;
1883
1884 /*
1885 * Make sure planes won't keep trying to pump pixels to us,
1886 * or we might hang the display.
1887 */
1888 assert_planes_disabled(dev_priv, pipe);
1889 assert_sprites_disabled(dev_priv, pipe);
1890
1891 /* Don't disable pipe A or pipe A PLLs if needed */
1892 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1893 return;
1894
1895 reg = PIPECONF(cpu_transcoder);
1896 val = I915_READ(reg);
1897 if ((val & PIPECONF_ENABLE) == 0)
1898 return;
1899
1900 I915_WRITE(reg, val & ~PIPECONF_ENABLE);
1901 intel_wait_for_pipe_off(dev_priv->dev, pipe);
1902 }
1903
1904 /*
1905 * Plane regs are double buffered, going from enabled->disabled needs a
1906 * trigger in order to latch. The display address reg provides this.
1907 */
1908 void intel_flush_display_plane(struct drm_i915_private *dev_priv,
1909 enum plane plane)
1910 {
1911 if (dev_priv->info->gen >= 4)
1912 I915_WRITE(DSPSURF(plane), I915_READ(DSPSURF(plane)));
1913 else
1914 I915_WRITE(DSPADDR(plane), I915_READ(DSPADDR(plane)));
1915 }
1916
1917 /**
1918 * intel_enable_plane - enable a display plane on a given pipe
1919 * @dev_priv: i915 private structure
1920 * @plane: plane to enable
1921 * @pipe: pipe being fed
1922 *
1923 * Enable @plane on @pipe, making sure that @pipe is running first.
1924 */
1925 static void intel_enable_plane(struct drm_i915_private *dev_priv,
1926 enum plane plane, enum pipe pipe)
1927 {
1928 int reg;
1929 u32 val;
1930
1931 /* If the pipe isn't enabled, we can't pump pixels and may hang */
1932 assert_pipe_enabled(dev_priv, pipe);
1933
1934 reg = DSPCNTR(plane);
1935 val = I915_READ(reg);
1936 if (val & DISPLAY_PLANE_ENABLE)
1937 return;
1938
1939 I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
1940 intel_flush_display_plane(dev_priv, plane);
1941 intel_wait_for_vblank(dev_priv->dev, pipe);
1942 }
1943
1944 /**
1945 * intel_disable_plane - disable a display plane
1946 * @dev_priv: i915 private structure
1947 * @plane: plane to disable
1948 * @pipe: pipe consuming the data
1949 *
1950 * Disable @plane; should be an independent operation.
1951 */
1952 static void intel_disable_plane(struct drm_i915_private *dev_priv,
1953 enum plane plane, enum pipe pipe)
1954 {
1955 int reg;
1956 u32 val;
1957
1958 reg = DSPCNTR(plane);
1959 val = I915_READ(reg);
1960 if ((val & DISPLAY_PLANE_ENABLE) == 0)
1961 return;
1962
1963 I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
1964 intel_flush_display_plane(dev_priv, plane);
1965 intel_wait_for_vblank(dev_priv->dev, pipe);
1966 }
1967
1968 static bool need_vtd_wa(struct drm_device *dev)
1969 {
1970 #ifdef CONFIG_INTEL_IOMMU
1971 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
1972 return true;
1973 #endif
1974 return false;
1975 }
1976
1977 int
1978 intel_pin_and_fence_fb_obj(struct drm_device *dev,
1979 struct drm_i915_gem_object *obj,
1980 struct intel_ring_buffer *pipelined)
1981 {
1982 struct drm_i915_private *dev_priv = dev->dev_private;
1983 u32 alignment;
1984 int ret;
1985
1986 switch (obj->tiling_mode) {
1987 case I915_TILING_NONE:
1988 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
1989 alignment = 128 * 1024;
1990 else if (INTEL_INFO(dev)->gen >= 4)
1991 alignment = 4 * 1024;
1992 else
1993 alignment = 64 * 1024;
1994 break;
1995 case I915_TILING_X:
1996 /* pin() will align the object as required by fence */
1997 alignment = 0;
1998 break;
1999 case I915_TILING_Y:
2000 /* Despite that we check this in framebuffer_init userspace can
2001 * screw us over and change the tiling after the fact. Only
2002 * pinned buffers can't change their tiling. */
2003 DRM_DEBUG_DRIVER("Y tiled not allowed for scan out buffers\n");
2004 return -EINVAL;
2005 default:
2006 BUG();
2007 }
2008
2009 /* Note that the w/a also requires 64 PTE of padding following the
2010 * bo. We currently fill all unused PTE with the shadow page and so
2011 * we should always have valid PTE following the scanout preventing
2012 * the VT-d warning.
2013 */
2014 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2015 alignment = 256 * 1024;
2016
2017 dev_priv->mm.interruptible = false;
2018 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
2019 if (ret)
2020 goto err_interruptible;
2021
2022 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2023 * fence, whereas 965+ only requires a fence if using
2024 * framebuffer compression. For simplicity, we always install
2025 * a fence as the cost is not that onerous.
2026 */
2027 ret = i915_gem_object_get_fence(obj);
2028 if (ret)
2029 goto err_unpin;
2030
2031 i915_gem_object_pin_fence(obj);
2032
2033 dev_priv->mm.interruptible = true;
2034 return 0;
2035
2036 err_unpin:
2037 i915_gem_object_unpin(obj);
2038 err_interruptible:
2039 dev_priv->mm.interruptible = true;
2040 return ret;
2041 }
2042
2043 void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
2044 {
2045 i915_gem_object_unpin_fence(obj);
2046 i915_gem_object_unpin(obj);
2047 }
2048
2049 /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2050 * is assumed to be a power-of-two. */
2051 unsigned long intel_gen4_compute_page_offset(int *x, int *y,
2052 unsigned int tiling_mode,
2053 unsigned int cpp,
2054 unsigned int pitch)
2055 {
2056 if (tiling_mode != I915_TILING_NONE) {
2057 unsigned int tile_rows, tiles;
2058
2059 tile_rows = *y / 8;
2060 *y %= 8;
2061
2062 tiles = *x / (512/cpp);
2063 *x %= 512/cpp;
2064
2065 return tile_rows * pitch * 8 + tiles * 4096;
2066 } else {
2067 unsigned int offset;
2068
2069 offset = *y * pitch + *x * cpp;
2070 *y = 0;
2071 *x = (offset & 4095) / cpp;
2072 return offset & -4096;
2073 }
2074 }
2075
2076 static int i9xx_update_plane(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2077 int x, int y)
2078 {
2079 struct drm_device *dev = crtc->dev;
2080 struct drm_i915_private *dev_priv = dev->dev_private;
2081 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2082 struct intel_framebuffer *intel_fb;
2083 struct drm_i915_gem_object *obj;
2084 int plane = intel_crtc->plane;
2085 unsigned long linear_offset;
2086 u32 dspcntr;
2087 u32 reg;
2088
2089 switch (plane) {
2090 case 0:
2091 case 1:
2092 break;
2093 default:
2094 DRM_ERROR("Can't update plane %c in SAREA\n", plane_name(plane));
2095 return -EINVAL;
2096 }
2097
2098 intel_fb = to_intel_framebuffer(fb);
2099 obj = intel_fb->obj;
2100
2101 reg = DSPCNTR(plane);
2102 dspcntr = I915_READ(reg);
2103 /* Mask out pixel format bits in case we change it */
2104 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
2105 switch (fb->pixel_format) {
2106 case DRM_FORMAT_C8:
2107 dspcntr |= DISPPLANE_8BPP;
2108 break;
2109 case DRM_FORMAT_XRGB1555:
2110 case DRM_FORMAT_ARGB1555:
2111 dspcntr |= DISPPLANE_BGRX555;
2112 break;
2113 case DRM_FORMAT_RGB565:
2114 dspcntr |= DISPPLANE_BGRX565;
2115 break;
2116 case DRM_FORMAT_XRGB8888:
2117 case DRM_FORMAT_ARGB8888:
2118 dspcntr |= DISPPLANE_BGRX888;
2119 break;
2120 case DRM_FORMAT_XBGR8888:
2121 case DRM_FORMAT_ABGR8888:
2122 dspcntr |= DISPPLANE_RGBX888;
2123 break;
2124 case DRM_FORMAT_XRGB2101010:
2125 case DRM_FORMAT_ARGB2101010:
2126 dspcntr |= DISPPLANE_BGRX101010;
2127 break;
2128 case DRM_FORMAT_XBGR2101010:
2129 case DRM_FORMAT_ABGR2101010:
2130 dspcntr |= DISPPLANE_RGBX101010;
2131 break;
2132 default:
2133 BUG();
2134 }
2135
2136 if (INTEL_INFO(dev)->gen >= 4) {
2137 if (obj->tiling_mode != I915_TILING_NONE)
2138 dspcntr |= DISPPLANE_TILED;
2139 else
2140 dspcntr &= ~DISPPLANE_TILED;
2141 }
2142
2143 I915_WRITE(reg, dspcntr);
2144
2145 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
2146
2147 if (INTEL_INFO(dev)->gen >= 4) {
2148 intel_crtc->dspaddr_offset =
2149 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2150 fb->bits_per_pixel / 8,
2151 fb->pitches[0]);
2152 linear_offset -= intel_crtc->dspaddr_offset;
2153 } else {
2154 intel_crtc->dspaddr_offset = linear_offset;
2155 }
2156
2157 DRM_DEBUG_KMS("Writing base %08X %08lX %d %d %d\n",
2158 obj->gtt_offset, linear_offset, x, y, fb->pitches[0]);
2159 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
2160 if (INTEL_INFO(dev)->gen >= 4) {
2161 I915_MODIFY_DISPBASE(DSPSURF(plane),
2162 obj->gtt_offset + intel_crtc->dspaddr_offset);
2163 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2164 I915_WRITE(DSPLINOFF(plane), linear_offset);
2165 } else
2166 I915_WRITE(DSPADDR(plane), obj->gtt_offset + linear_offset);
2167 POSTING_READ(reg);
2168
2169 return 0;
2170 }
2171
2172 static int ironlake_update_plane(struct drm_crtc *crtc,
2173 struct drm_framebuffer *fb, int x, int y)
2174 {
2175 struct drm_device *dev = crtc->dev;
2176 struct drm_i915_private *dev_priv = dev->dev_private;
2177 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2178 struct intel_framebuffer *intel_fb;
2179 struct drm_i915_gem_object *obj;
2180 int plane = intel_crtc->plane;
2181 unsigned long linear_offset;
2182 u32 dspcntr;
2183 u32 reg;
2184
2185 switch (plane) {
2186 case 0:
2187 case 1:
2188 case 2:
2189 break;
2190 default:
2191 DRM_ERROR("Can't update plane %c in SAREA\n", plane_name(plane));
2192 return -EINVAL;
2193 }
2194
2195 intel_fb = to_intel_framebuffer(fb);
2196 obj = intel_fb->obj;
2197
2198 reg = DSPCNTR(plane);
2199 dspcntr = I915_READ(reg);
2200 /* Mask out pixel format bits in case we change it */
2201 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
2202 switch (fb->pixel_format) {
2203 case DRM_FORMAT_C8:
2204 dspcntr |= DISPPLANE_8BPP;
2205 break;
2206 case DRM_FORMAT_RGB565:
2207 dspcntr |= DISPPLANE_BGRX565;
2208 break;
2209 case DRM_FORMAT_XRGB8888:
2210 case DRM_FORMAT_ARGB8888:
2211 dspcntr |= DISPPLANE_BGRX888;
2212 break;
2213 case DRM_FORMAT_XBGR8888:
2214 case DRM_FORMAT_ABGR8888:
2215 dspcntr |= DISPPLANE_RGBX888;
2216 break;
2217 case DRM_FORMAT_XRGB2101010:
2218 case DRM_FORMAT_ARGB2101010:
2219 dspcntr |= DISPPLANE_BGRX101010;
2220 break;
2221 case DRM_FORMAT_XBGR2101010:
2222 case DRM_FORMAT_ABGR2101010:
2223 dspcntr |= DISPPLANE_RGBX101010;
2224 break;
2225 default:
2226 BUG();
2227 }
2228
2229 if (obj->tiling_mode != I915_TILING_NONE)
2230 dspcntr |= DISPPLANE_TILED;
2231 else
2232 dspcntr &= ~DISPPLANE_TILED;
2233
2234 /* must disable */
2235 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2236
2237 I915_WRITE(reg, dspcntr);
2238
2239 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
2240 intel_crtc->dspaddr_offset =
2241 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2242 fb->bits_per_pixel / 8,
2243 fb->pitches[0]);
2244 linear_offset -= intel_crtc->dspaddr_offset;
2245
2246 DRM_DEBUG_KMS("Writing base %08X %08lX %d %d %d\n",
2247 obj->gtt_offset, linear_offset, x, y, fb->pitches[0]);
2248 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
2249 I915_MODIFY_DISPBASE(DSPSURF(plane),
2250 obj->gtt_offset + intel_crtc->dspaddr_offset);
2251 if (IS_HASWELL(dev)) {
2252 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2253 } else {
2254 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2255 I915_WRITE(DSPLINOFF(plane), linear_offset);
2256 }
2257 POSTING_READ(reg);
2258
2259 return 0;
2260 }
2261
2262 /* Assume fb object is pinned & idle & fenced and just update base pointers */
2263 static int
2264 intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2265 int x, int y, enum mode_set_atomic state)
2266 {
2267 struct drm_device *dev = crtc->dev;
2268 struct drm_i915_private *dev_priv = dev->dev_private;
2269
2270 if (dev_priv->display.disable_fbc)
2271 dev_priv->display.disable_fbc(dev);
2272 intel_increase_pllclock(crtc);
2273
2274 return dev_priv->display.update_plane(crtc, fb, x, y);
2275 }
2276
2277 void intel_display_handle_reset(struct drm_device *dev)
2278 {
2279 struct drm_i915_private *dev_priv = dev->dev_private;
2280 struct drm_crtc *crtc;
2281
2282 /*
2283 * Flips in the rings have been nuked by the reset,
2284 * so complete all pending flips so that user space
2285 * will get its events and not get stuck.
2286 *
2287 * Also update the base address of all primary
2288 * planes to the the last fb to make sure we're
2289 * showing the correct fb after a reset.
2290 *
2291 * Need to make two loops over the crtcs so that we
2292 * don't try to grab a crtc mutex before the
2293 * pending_flip_queue really got woken up.
2294 */
2295
2296 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2297 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2298 enum plane plane = intel_crtc->plane;
2299
2300 intel_prepare_page_flip(dev, plane);
2301 intel_finish_page_flip_plane(dev, plane);
2302 }
2303
2304 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2305 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2306
2307 mutex_lock(&crtc->mutex);
2308 if (intel_crtc->active)
2309 dev_priv->display.update_plane(crtc, crtc->fb,
2310 crtc->x, crtc->y);
2311 mutex_unlock(&crtc->mutex);
2312 }
2313 }
2314
2315 static int
2316 intel_finish_fb(struct drm_framebuffer *old_fb)
2317 {
2318 struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
2319 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2320 bool was_interruptible = dev_priv->mm.interruptible;
2321 int ret;
2322
2323 /* Big Hammer, we also need to ensure that any pending
2324 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2325 * current scanout is retired before unpinning the old
2326 * framebuffer.
2327 *
2328 * This should only fail upon a hung GPU, in which case we
2329 * can safely continue.
2330 */
2331 dev_priv->mm.interruptible = false;
2332 ret = i915_gem_object_finish_gpu(obj);
2333 dev_priv->mm.interruptible = was_interruptible;
2334
2335 return ret;
2336 }
2337
2338 static void intel_crtc_update_sarea_pos(struct drm_crtc *crtc, int x, int y)
2339 {
2340 struct drm_device *dev = crtc->dev;
2341 struct drm_i915_master_private *master_priv;
2342 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2343
2344 if (!dev->primary->master)
2345 return;
2346
2347 master_priv = dev->primary->master->driver_priv;
2348 if (!master_priv->sarea_priv)
2349 return;
2350
2351 switch (intel_crtc->pipe) {
2352 case 0:
2353 master_priv->sarea_priv->pipeA_x = x;
2354 master_priv->sarea_priv->pipeA_y = y;
2355 break;
2356 case 1:
2357 master_priv->sarea_priv->pipeB_x = x;
2358 master_priv->sarea_priv->pipeB_y = y;
2359 break;
2360 default:
2361 break;
2362 }
2363 }
2364
2365 static int
2366 intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
2367 struct drm_framebuffer *fb)
2368 {
2369 struct drm_device *dev = crtc->dev;
2370 struct drm_i915_private *dev_priv = dev->dev_private;
2371 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2372 struct drm_framebuffer *old_fb;
2373 int ret;
2374
2375 /* no fb bound */
2376 if (!fb) {
2377 DRM_ERROR("No FB bound\n");
2378 return 0;
2379 }
2380
2381 if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
2382 DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
2383 plane_name(intel_crtc->plane),
2384 INTEL_INFO(dev)->num_pipes);
2385 return -EINVAL;
2386 }
2387
2388 mutex_lock(&dev->struct_mutex);
2389 ret = intel_pin_and_fence_fb_obj(dev,
2390 to_intel_framebuffer(fb)->obj,
2391 NULL);
2392 if (ret != 0) {
2393 mutex_unlock(&dev->struct_mutex);
2394 DRM_ERROR("pin & fence failed\n");
2395 return ret;
2396 }
2397
2398 ret = dev_priv->display.update_plane(crtc, fb, x, y);
2399 if (ret) {
2400 intel_unpin_fb_obj(to_intel_framebuffer(fb)->obj);
2401 mutex_unlock(&dev->struct_mutex);
2402 DRM_ERROR("failed to update base address\n");
2403 return ret;
2404 }
2405
2406 old_fb = crtc->fb;
2407 crtc->fb = fb;
2408 crtc->x = x;
2409 crtc->y = y;
2410
2411 if (old_fb) {
2412 intel_wait_for_vblank(dev, intel_crtc->pipe);
2413 intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
2414 }
2415
2416 intel_update_fbc(dev);
2417 mutex_unlock(&dev->struct_mutex);
2418
2419 intel_crtc_update_sarea_pos(crtc, x, y);
2420
2421 return 0;
2422 }
2423
2424 static void intel_fdi_normal_train(struct drm_crtc *crtc)
2425 {
2426 struct drm_device *dev = crtc->dev;
2427 struct drm_i915_private *dev_priv = dev->dev_private;
2428 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2429 int pipe = intel_crtc->pipe;
2430 u32 reg, temp;
2431
2432 /* enable normal train */
2433 reg = FDI_TX_CTL(pipe);
2434 temp = I915_READ(reg);
2435 if (IS_IVYBRIDGE(dev)) {
2436 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2437 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
2438 } else {
2439 temp &= ~FDI_LINK_TRAIN_NONE;
2440 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
2441 }
2442 I915_WRITE(reg, temp);
2443
2444 reg = FDI_RX_CTL(pipe);
2445 temp = I915_READ(reg);
2446 if (HAS_PCH_CPT(dev)) {
2447 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2448 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2449 } else {
2450 temp &= ~FDI_LINK_TRAIN_NONE;
2451 temp |= FDI_LINK_TRAIN_NONE;
2452 }
2453 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2454
2455 /* wait one idle pattern time */
2456 POSTING_READ(reg);
2457 udelay(1000);
2458
2459 /* IVB wants error correction enabled */
2460 if (IS_IVYBRIDGE(dev))
2461 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2462 FDI_FE_ERRC_ENABLE);
2463 }
2464
2465 static void ivb_modeset_global_resources(struct drm_device *dev)
2466 {
2467 struct drm_i915_private *dev_priv = dev->dev_private;
2468 struct intel_crtc *pipe_B_crtc =
2469 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
2470 struct intel_crtc *pipe_C_crtc =
2471 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
2472 uint32_t temp;
2473
2474 /* When everything is off disable fdi C so that we could enable fdi B
2475 * with all lanes. XXX: This misses the case where a pipe is not using
2476 * any pch resources and so doesn't need any fdi lanes. */
2477 if (!pipe_B_crtc->base.enabled && !pipe_C_crtc->base.enabled) {
2478 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
2479 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
2480
2481 temp = I915_READ(SOUTH_CHICKEN1);
2482 temp &= ~FDI_BC_BIFURCATION_SELECT;
2483 DRM_DEBUG_KMS("disabling fdi C rx\n");
2484 I915_WRITE(SOUTH_CHICKEN1, temp);
2485 }
2486 }
2487
2488 /* The FDI link training functions for ILK/Ibexpeak. */
2489 static void ironlake_fdi_link_train(struct drm_crtc *crtc)
2490 {
2491 struct drm_device *dev = crtc->dev;
2492 struct drm_i915_private *dev_priv = dev->dev_private;
2493 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2494 int pipe = intel_crtc->pipe;
2495 int plane = intel_crtc->plane;
2496 u32 reg, temp, tries;
2497
2498 /* FDI needs bits from pipe & plane first */
2499 assert_pipe_enabled(dev_priv, pipe);
2500 assert_plane_enabled(dev_priv, plane);
2501
2502 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2503 for train result */
2504 reg = FDI_RX_IMR(pipe);
2505 temp = I915_READ(reg);
2506 temp &= ~FDI_RX_SYMBOL_LOCK;
2507 temp &= ~FDI_RX_BIT_LOCK;
2508 I915_WRITE(reg, temp);
2509 I915_READ(reg);
2510 udelay(150);
2511
2512 /* enable CPU FDI TX and PCH FDI RX */
2513 reg = FDI_TX_CTL(pipe);
2514 temp = I915_READ(reg);
2515 temp &= ~(7 << 19);
2516 temp |= (intel_crtc->fdi_lanes - 1) << 19;
2517 temp &= ~FDI_LINK_TRAIN_NONE;
2518 temp |= FDI_LINK_TRAIN_PATTERN_1;
2519 I915_WRITE(reg, temp | FDI_TX_ENABLE);
2520
2521 reg = FDI_RX_CTL(pipe);
2522 temp = I915_READ(reg);
2523 temp &= ~FDI_LINK_TRAIN_NONE;
2524 temp |= FDI_LINK_TRAIN_PATTERN_1;
2525 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2526
2527 POSTING_READ(reg);
2528 udelay(150);
2529
2530 /* Ironlake workaround, enable clock pointer after FDI enable*/
2531 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2532 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
2533 FDI_RX_PHASE_SYNC_POINTER_EN);
2534
2535 reg = FDI_RX_IIR(pipe);
2536 for (tries = 0; tries < 5; tries++) {
2537 temp = I915_READ(reg);
2538 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2539
2540 if ((temp & FDI_RX_BIT_LOCK)) {
2541 DRM_DEBUG_KMS("FDI train 1 done.\n");
2542 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2543 break;
2544 }
2545 }
2546 if (tries == 5)
2547 DRM_ERROR("FDI train 1 fail!\n");
2548
2549 /* Train 2 */
2550 reg = FDI_TX_CTL(pipe);
2551 temp = I915_READ(reg);
2552 temp &= ~FDI_LINK_TRAIN_NONE;
2553 temp |= FDI_LINK_TRAIN_PATTERN_2;
2554 I915_WRITE(reg, temp);
2555
2556 reg = FDI_RX_CTL(pipe);
2557 temp = I915_READ(reg);
2558 temp &= ~FDI_LINK_TRAIN_NONE;
2559 temp |= FDI_LINK_TRAIN_PATTERN_2;
2560 I915_WRITE(reg, temp);
2561
2562 POSTING_READ(reg);
2563 udelay(150);
2564
2565 reg = FDI_RX_IIR(pipe);
2566 for (tries = 0; tries < 5; tries++) {
2567 temp = I915_READ(reg);
2568 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2569
2570 if (temp & FDI_RX_SYMBOL_LOCK) {
2571 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2572 DRM_DEBUG_KMS("FDI train 2 done.\n");
2573 break;
2574 }
2575 }
2576 if (tries == 5)
2577 DRM_ERROR("FDI train 2 fail!\n");
2578
2579 DRM_DEBUG_KMS("FDI train done\n");
2580
2581 }
2582
2583 static const int snb_b_fdi_train_param[] = {
2584 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
2585 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
2586 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
2587 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
2588 };
2589
2590 /* The FDI link training functions for SNB/Cougarpoint. */
2591 static void gen6_fdi_link_train(struct drm_crtc *crtc)
2592 {
2593 struct drm_device *dev = crtc->dev;
2594 struct drm_i915_private *dev_priv = dev->dev_private;
2595 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2596 int pipe = intel_crtc->pipe;
2597 u32 reg, temp, i, retry;
2598
2599 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2600 for train result */
2601 reg = FDI_RX_IMR(pipe);
2602 temp = I915_READ(reg);
2603 temp &= ~FDI_RX_SYMBOL_LOCK;
2604 temp &= ~FDI_RX_BIT_LOCK;
2605 I915_WRITE(reg, temp);
2606
2607 POSTING_READ(reg);
2608 udelay(150);
2609
2610 /* enable CPU FDI TX and PCH FDI RX */
2611 reg = FDI_TX_CTL(pipe);
2612 temp = I915_READ(reg);
2613 temp &= ~(7 << 19);
2614 temp |= (intel_crtc->fdi_lanes - 1) << 19;
2615 temp &= ~FDI_LINK_TRAIN_NONE;
2616 temp |= FDI_LINK_TRAIN_PATTERN_1;
2617 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2618 /* SNB-B */
2619 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2620 I915_WRITE(reg, temp | FDI_TX_ENABLE);
2621
2622 I915_WRITE(FDI_RX_MISC(pipe),
2623 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2624
2625 reg = FDI_RX_CTL(pipe);
2626 temp = I915_READ(reg);
2627 if (HAS_PCH_CPT(dev)) {
2628 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2629 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2630 } else {
2631 temp &= ~FDI_LINK_TRAIN_NONE;
2632 temp |= FDI_LINK_TRAIN_PATTERN_1;
2633 }
2634 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2635
2636 POSTING_READ(reg);
2637 udelay(150);
2638
2639 for (i = 0; i < 4; i++) {
2640 reg = FDI_TX_CTL(pipe);
2641 temp = I915_READ(reg);
2642 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2643 temp |= snb_b_fdi_train_param[i];
2644 I915_WRITE(reg, temp);
2645
2646 POSTING_READ(reg);
2647 udelay(500);
2648
2649 for (retry = 0; retry < 5; retry++) {
2650 reg = FDI_RX_IIR(pipe);
2651 temp = I915_READ(reg);
2652 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2653 if (temp & FDI_RX_BIT_LOCK) {
2654 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2655 DRM_DEBUG_KMS("FDI train 1 done.\n");
2656 break;
2657 }
2658 udelay(50);
2659 }
2660 if (retry < 5)
2661 break;
2662 }
2663 if (i == 4)
2664 DRM_ERROR("FDI train 1 fail!\n");
2665
2666 /* Train 2 */
2667 reg = FDI_TX_CTL(pipe);
2668 temp = I915_READ(reg);
2669 temp &= ~FDI_LINK_TRAIN_NONE;
2670 temp |= FDI_LINK_TRAIN_PATTERN_2;
2671 if (IS_GEN6(dev)) {
2672 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2673 /* SNB-B */
2674 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2675 }
2676 I915_WRITE(reg, temp);
2677
2678 reg = FDI_RX_CTL(pipe);
2679 temp = I915_READ(reg);
2680 if (HAS_PCH_CPT(dev)) {
2681 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2682 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2683 } else {
2684 temp &= ~FDI_LINK_TRAIN_NONE;
2685 temp |= FDI_LINK_TRAIN_PATTERN_2;
2686 }
2687 I915_WRITE(reg, temp);
2688
2689 POSTING_READ(reg);
2690 udelay(150);
2691
2692 for (i = 0; i < 4; i++) {
2693 reg = FDI_TX_CTL(pipe);
2694 temp = I915_READ(reg);
2695 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2696 temp |= snb_b_fdi_train_param[i];
2697 I915_WRITE(reg, temp);
2698
2699 POSTING_READ(reg);
2700 udelay(500);
2701
2702 for (retry = 0; retry < 5; retry++) {
2703 reg = FDI_RX_IIR(pipe);
2704 temp = I915_READ(reg);
2705 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2706 if (temp & FDI_RX_SYMBOL_LOCK) {
2707 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2708 DRM_DEBUG_KMS("FDI train 2 done.\n");
2709 break;
2710 }
2711 udelay(50);
2712 }
2713 if (retry < 5)
2714 break;
2715 }
2716 if (i == 4)
2717 DRM_ERROR("FDI train 2 fail!\n");
2718
2719 DRM_DEBUG_KMS("FDI train done.\n");
2720 }
2721
2722 /* Manual link training for Ivy Bridge A0 parts */
2723 static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
2724 {
2725 struct drm_device *dev = crtc->dev;
2726 struct drm_i915_private *dev_priv = dev->dev_private;
2727 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2728 int pipe = intel_crtc->pipe;
2729 u32 reg, temp, i;
2730
2731 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2732 for train result */
2733 reg = FDI_RX_IMR(pipe);
2734 temp = I915_READ(reg);
2735 temp &= ~FDI_RX_SYMBOL_LOCK;
2736 temp &= ~FDI_RX_BIT_LOCK;
2737 I915_WRITE(reg, temp);
2738
2739 POSTING_READ(reg);
2740 udelay(150);
2741
2742 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
2743 I915_READ(FDI_RX_IIR(pipe)));
2744
2745 /* enable CPU FDI TX and PCH FDI RX */
2746 reg = FDI_TX_CTL(pipe);
2747 temp = I915_READ(reg);
2748 temp &= ~(7 << 19);
2749 temp |= (intel_crtc->fdi_lanes - 1) << 19;
2750 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
2751 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
2752 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2753 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2754 temp |= FDI_COMPOSITE_SYNC;
2755 I915_WRITE(reg, temp | FDI_TX_ENABLE);
2756
2757 I915_WRITE(FDI_RX_MISC(pipe),
2758 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2759
2760 reg = FDI_RX_CTL(pipe);
2761 temp = I915_READ(reg);
2762 temp &= ~FDI_LINK_TRAIN_AUTO;
2763 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2764 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2765 temp |= FDI_COMPOSITE_SYNC;
2766 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2767
2768 POSTING_READ(reg);
2769 udelay(150);
2770
2771 for (i = 0; i < 4; i++) {
2772 reg = FDI_TX_CTL(pipe);
2773 temp = I915_READ(reg);
2774 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2775 temp |= snb_b_fdi_train_param[i];
2776 I915_WRITE(reg, temp);
2777
2778 POSTING_READ(reg);
2779 udelay(500);
2780
2781 reg = FDI_RX_IIR(pipe);
2782 temp = I915_READ(reg);
2783 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2784
2785 if (temp & FDI_RX_BIT_LOCK ||
2786 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
2787 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2788 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n", i);
2789 break;
2790 }
2791 }
2792 if (i == 4)
2793 DRM_ERROR("FDI train 1 fail!\n");
2794
2795 /* Train 2 */
2796 reg = FDI_TX_CTL(pipe);
2797 temp = I915_READ(reg);
2798 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2799 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
2800 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2801 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2802 I915_WRITE(reg, temp);
2803
2804 reg = FDI_RX_CTL(pipe);
2805 temp = I915_READ(reg);
2806 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2807 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2808 I915_WRITE(reg, temp);
2809
2810 POSTING_READ(reg);
2811 udelay(150);
2812
2813 for (i = 0; i < 4; i++) {
2814 reg = FDI_TX_CTL(pipe);
2815 temp = I915_READ(reg);
2816 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2817 temp |= snb_b_fdi_train_param[i];
2818 I915_WRITE(reg, temp);
2819
2820 POSTING_READ(reg);
2821 udelay(500);
2822
2823 reg = FDI_RX_IIR(pipe);
2824 temp = I915_READ(reg);
2825 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2826
2827 if (temp & FDI_RX_SYMBOL_LOCK) {
2828 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2829 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n", i);
2830 break;
2831 }
2832 }
2833 if (i == 4)
2834 DRM_ERROR("FDI train 2 fail!\n");
2835
2836 DRM_DEBUG_KMS("FDI train done.\n");
2837 }
2838
2839 static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2840 {
2841 struct drm_device *dev = intel_crtc->base.dev;
2842 struct drm_i915_private *dev_priv = dev->dev_private;
2843 int pipe = intel_crtc->pipe;
2844 u32 reg, temp;
2845
2846
2847 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
2848 reg = FDI_RX_CTL(pipe);
2849 temp = I915_READ(reg);
2850 temp &= ~((0x7 << 19) | (0x7 << 16));
2851 temp |= (intel_crtc->fdi_lanes - 1) << 19;
2852 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
2853 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
2854
2855 POSTING_READ(reg);
2856 udelay(200);
2857
2858 /* Switch from Rawclk to PCDclk */
2859 temp = I915_READ(reg);
2860 I915_WRITE(reg, temp | FDI_PCDCLK);
2861
2862 POSTING_READ(reg);
2863 udelay(200);
2864
2865 /* Enable CPU FDI TX PLL, always on for Ironlake */
2866 reg = FDI_TX_CTL(pipe);
2867 temp = I915_READ(reg);
2868 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
2869 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
2870
2871 POSTING_READ(reg);
2872 udelay(100);
2873 }
2874 }
2875
2876 static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
2877 {
2878 struct drm_device *dev = intel_crtc->base.dev;
2879 struct drm_i915_private *dev_priv = dev->dev_private;
2880 int pipe = intel_crtc->pipe;
2881 u32 reg, temp;
2882
2883 /* Switch from PCDclk to Rawclk */
2884 reg = FDI_RX_CTL(pipe);
2885 temp = I915_READ(reg);
2886 I915_WRITE(reg, temp & ~FDI_PCDCLK);
2887
2888 /* Disable CPU FDI TX PLL */
2889 reg = FDI_TX_CTL(pipe);
2890 temp = I915_READ(reg);
2891 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
2892
2893 POSTING_READ(reg);
2894 udelay(100);
2895
2896 reg = FDI_RX_CTL(pipe);
2897 temp = I915_READ(reg);
2898 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
2899
2900 /* Wait for the clocks to turn off. */
2901 POSTING_READ(reg);
2902 udelay(100);
2903 }
2904
2905 static void ironlake_fdi_disable(struct drm_crtc *crtc)
2906 {
2907 struct drm_device *dev = crtc->dev;
2908 struct drm_i915_private *dev_priv = dev->dev_private;
2909 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2910 int pipe = intel_crtc->pipe;
2911 u32 reg, temp;
2912
2913 /* disable CPU FDI tx and PCH FDI rx */
2914 reg = FDI_TX_CTL(pipe);
2915 temp = I915_READ(reg);
2916 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
2917 POSTING_READ(reg);
2918
2919 reg = FDI_RX_CTL(pipe);
2920 temp = I915_READ(reg);
2921 temp &= ~(0x7 << 16);
2922 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
2923 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
2924
2925 POSTING_READ(reg);
2926 udelay(100);
2927
2928 /* Ironlake workaround, disable clock pointer after downing FDI */
2929 if (HAS_PCH_IBX(dev)) {
2930 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2931 }
2932
2933 /* still set train pattern 1 */
2934 reg = FDI_TX_CTL(pipe);
2935 temp = I915_READ(reg);
2936 temp &= ~FDI_LINK_TRAIN_NONE;
2937 temp |= FDI_LINK_TRAIN_PATTERN_1;
2938 I915_WRITE(reg, temp);
2939
2940 reg = FDI_RX_CTL(pipe);
2941 temp = I915_READ(reg);
2942 if (HAS_PCH_CPT(dev)) {
2943 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2944 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2945 } else {
2946 temp &= ~FDI_LINK_TRAIN_NONE;
2947 temp |= FDI_LINK_TRAIN_PATTERN_1;
2948 }
2949 /* BPC in FDI rx is consistent with that in PIPECONF */
2950 temp &= ~(0x07 << 16);
2951 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
2952 I915_WRITE(reg, temp);
2953
2954 POSTING_READ(reg);
2955 udelay(100);
2956 }
2957
2958 static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
2959 {
2960 struct drm_device *dev = crtc->dev;
2961 struct drm_i915_private *dev_priv = dev->dev_private;
2962 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2963 unsigned long flags;
2964 bool pending;
2965
2966 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
2967 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
2968 return false;
2969
2970 spin_lock_irqsave(&dev->event_lock, flags);
2971 pending = to_intel_crtc(crtc)->unpin_work != NULL;
2972 spin_unlock_irqrestore(&dev->event_lock, flags);
2973
2974 return pending;
2975 }
2976
2977 static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
2978 {
2979 struct drm_device *dev = crtc->dev;
2980 struct drm_i915_private *dev_priv = dev->dev_private;
2981
2982 if (crtc->fb == NULL)
2983 return;
2984
2985 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
2986
2987 wait_event(dev_priv->pending_flip_queue,
2988 !intel_crtc_has_pending_flip(crtc));
2989
2990 mutex_lock(&dev->struct_mutex);
2991 intel_finish_fb(crtc->fb);
2992 mutex_unlock(&dev->struct_mutex);
2993 }
2994
2995 /* Program iCLKIP clock to the desired frequency */
2996 static void lpt_program_iclkip(struct drm_crtc *crtc)
2997 {
2998 struct drm_device *dev = crtc->dev;
2999 struct drm_i915_private *dev_priv = dev->dev_private;
3000 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3001 u32 temp;
3002
3003 mutex_lock(&dev_priv->dpio_lock);
3004
3005 /* It is necessary to ungate the pixclk gate prior to programming
3006 * the divisors, and gate it back when it is done.
3007 */
3008 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3009
3010 /* Disable SSCCTL */
3011 intel_sbi_write(dev_priv, SBI_SSCCTL6,
3012 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3013 SBI_SSCCTL_DISABLE,
3014 SBI_ICLK);
3015
3016 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
3017 if (crtc->mode.clock == 20000) {
3018 auxdiv = 1;
3019 divsel = 0x41;
3020 phaseinc = 0x20;
3021 } else {
3022 /* The iCLK virtual clock root frequency is in MHz,
3023 * but the crtc->mode.clock in in KHz. To get the divisors,
3024 * it is necessary to divide one by another, so we
3025 * convert the virtual clock precision to KHz here for higher
3026 * precision.
3027 */
3028 u32 iclk_virtual_root_freq = 172800 * 1000;
3029 u32 iclk_pi_range = 64;
3030 u32 desired_divisor, msb_divisor_value, pi_value;
3031
3032 desired_divisor = (iclk_virtual_root_freq / crtc->mode.clock);
3033 msb_divisor_value = desired_divisor / iclk_pi_range;
3034 pi_value = desired_divisor % iclk_pi_range;
3035
3036 auxdiv = 0;
3037 divsel = msb_divisor_value - 2;
3038 phaseinc = pi_value;
3039 }
3040
3041 /* This should not happen with any sane values */
3042 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3043 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3044 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3045 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3046
3047 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
3048 crtc->mode.clock,
3049 auxdiv,
3050 divsel,
3051 phasedir,
3052 phaseinc);
3053
3054 /* Program SSCDIVINTPHASE6 */
3055 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
3056 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3057 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3058 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3059 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3060 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3061 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
3062 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
3063
3064 /* Program SSCAUXDIV */
3065 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
3066 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3067 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
3068 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
3069
3070 /* Enable modulator and associated divider */
3071 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3072 temp &= ~SBI_SSCCTL_DISABLE;
3073 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
3074
3075 /* Wait for initialization time */
3076 udelay(24);
3077
3078 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
3079
3080 mutex_unlock(&dev_priv->dpio_lock);
3081 }
3082
3083 /*
3084 * Enable PCH resources required for PCH ports:
3085 * - PCH PLLs
3086 * - FDI training & RX/TX
3087 * - update transcoder timings
3088 * - DP transcoding bits
3089 * - transcoder
3090 */
3091 static void ironlake_pch_enable(struct drm_crtc *crtc)
3092 {
3093 struct drm_device *dev = crtc->dev;
3094 struct drm_i915_private *dev_priv = dev->dev_private;
3095 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3096 int pipe = intel_crtc->pipe;
3097 u32 reg, temp;
3098
3099 assert_transcoder_disabled(dev_priv, pipe);
3100
3101 /* Write the TU size bits before fdi link training, so that error
3102 * detection works. */
3103 I915_WRITE(FDI_RX_TUSIZE1(pipe),
3104 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
3105
3106 /* For PCH output, training FDI link */
3107 dev_priv->display.fdi_link_train(crtc);
3108
3109 /* XXX: pch pll's can be enabled any time before we enable the PCH
3110 * transcoder, and we actually should do this to not upset any PCH
3111 * transcoder that already use the clock when we share it.
3112 *
3113 * Note that enable_pch_pll tries to do the right thing, but get_pch_pll
3114 * unconditionally resets the pll - we need that to have the right LVDS
3115 * enable sequence. */
3116 ironlake_enable_pch_pll(intel_crtc);
3117
3118 if (HAS_PCH_CPT(dev)) {
3119 u32 sel;
3120
3121 temp = I915_READ(PCH_DPLL_SEL);
3122 switch (pipe) {
3123 default:
3124 case 0:
3125 temp |= TRANSA_DPLL_ENABLE;
3126 sel = TRANSA_DPLLB_SEL;
3127 break;
3128 case 1:
3129 temp |= TRANSB_DPLL_ENABLE;
3130 sel = TRANSB_DPLLB_SEL;
3131 break;
3132 case 2:
3133 temp |= TRANSC_DPLL_ENABLE;
3134 sel = TRANSC_DPLLB_SEL;
3135 break;
3136 }
3137 if (intel_crtc->pch_pll->pll_reg == _PCH_DPLL_B)
3138 temp |= sel;
3139 else
3140 temp &= ~sel;
3141 I915_WRITE(PCH_DPLL_SEL, temp);
3142 }
3143
3144 /* set transcoder timing, panel must allow it */
3145 assert_panel_unlocked(dev_priv, pipe);
3146 I915_WRITE(TRANS_HTOTAL(pipe), I915_READ(HTOTAL(pipe)));
3147 I915_WRITE(TRANS_HBLANK(pipe), I915_READ(HBLANK(pipe)));
3148 I915_WRITE(TRANS_HSYNC(pipe), I915_READ(HSYNC(pipe)));
3149
3150 I915_WRITE(TRANS_VTOTAL(pipe), I915_READ(VTOTAL(pipe)));
3151 I915_WRITE(TRANS_VBLANK(pipe), I915_READ(VBLANK(pipe)));
3152 I915_WRITE(TRANS_VSYNC(pipe), I915_READ(VSYNC(pipe)));
3153 I915_WRITE(TRANS_VSYNCSHIFT(pipe), I915_READ(VSYNCSHIFT(pipe)));
3154
3155 intel_fdi_normal_train(crtc);
3156
3157 /* For PCH DP, enable TRANS_DP_CTL */
3158 if (HAS_PCH_CPT(dev) &&
3159 (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
3160 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
3161 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
3162 reg = TRANS_DP_CTL(pipe);
3163 temp = I915_READ(reg);
3164 temp &= ~(TRANS_DP_PORT_SEL_MASK |
3165 TRANS_DP_SYNC_MASK |
3166 TRANS_DP_BPC_MASK);
3167 temp |= (TRANS_DP_OUTPUT_ENABLE |
3168 TRANS_DP_ENH_FRAMING);
3169 temp |= bpc << 9; /* same format but at 11:9 */
3170
3171 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
3172 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
3173 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
3174 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
3175
3176 switch (intel_trans_dp_port_sel(crtc)) {
3177 case PCH_DP_B:
3178 temp |= TRANS_DP_PORT_SEL_B;
3179 break;
3180 case PCH_DP_C:
3181 temp |= TRANS_DP_PORT_SEL_C;
3182 break;
3183 case PCH_DP_D:
3184 temp |= TRANS_DP_PORT_SEL_D;
3185 break;
3186 default:
3187 BUG();
3188 }
3189
3190 I915_WRITE(reg, temp);
3191 }
3192
3193 ironlake_enable_pch_transcoder(dev_priv, pipe);
3194 }
3195
3196 static void lpt_pch_enable(struct drm_crtc *crtc)
3197 {
3198 struct drm_device *dev = crtc->dev;
3199 struct drm_i915_private *dev_priv = dev->dev_private;
3200 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3201 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
3202
3203 assert_transcoder_disabled(dev_priv, TRANSCODER_A);
3204
3205 lpt_program_iclkip(crtc);
3206
3207 /* Set transcoder timing. */
3208 I915_WRITE(_TRANS_HTOTAL_A, I915_READ(HTOTAL(cpu_transcoder)));
3209 I915_WRITE(_TRANS_HBLANK_A, I915_READ(HBLANK(cpu_transcoder)));
3210 I915_WRITE(_TRANS_HSYNC_A, I915_READ(HSYNC(cpu_transcoder)));
3211
3212 I915_WRITE(_TRANS_VTOTAL_A, I915_READ(VTOTAL(cpu_transcoder)));
3213 I915_WRITE(_TRANS_VBLANK_A, I915_READ(VBLANK(cpu_transcoder)));
3214 I915_WRITE(_TRANS_VSYNC_A, I915_READ(VSYNC(cpu_transcoder)));
3215 I915_WRITE(_TRANS_VSYNCSHIFT_A, I915_READ(VSYNCSHIFT(cpu_transcoder)));
3216
3217 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
3218 }
3219
3220 static void intel_put_pch_pll(struct intel_crtc *intel_crtc)
3221 {
3222 struct intel_pch_pll *pll = intel_crtc->pch_pll;
3223
3224 if (pll == NULL)
3225 return;
3226
3227 if (pll->refcount == 0) {
3228 WARN(1, "bad PCH PLL refcount\n");
3229 return;
3230 }
3231
3232 --pll->refcount;
3233 intel_crtc->pch_pll = NULL;
3234 }
3235
3236 static struct intel_pch_pll *intel_get_pch_pll(struct intel_crtc *intel_crtc, u32 dpll, u32 fp)
3237 {
3238 struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
3239 struct intel_pch_pll *pll;
3240 int i;
3241
3242 pll = intel_crtc->pch_pll;
3243 if (pll) {
3244 DRM_DEBUG_KMS("CRTC:%d reusing existing PCH PLL %x\n",
3245 intel_crtc->base.base.id, pll->pll_reg);
3246 goto prepare;
3247 }
3248
3249 if (HAS_PCH_IBX(dev_priv->dev)) {
3250 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
3251 i = intel_crtc->pipe;
3252 pll = &dev_priv->pch_plls[i];
3253
3254 DRM_DEBUG_KMS("CRTC:%d using pre-allocated PCH PLL %x\n",
3255 intel_crtc->base.base.id, pll->pll_reg);
3256
3257 goto found;
3258 }
3259
3260 for (i = 0; i < dev_priv->num_pch_pll; i++) {
3261 pll = &dev_priv->pch_plls[i];
3262
3263 /* Only want to check enabled timings first */
3264 if (pll->refcount == 0)
3265 continue;
3266
3267 if (dpll == (I915_READ(pll->pll_reg) & 0x7fffffff) &&
3268 fp == I915_READ(pll->fp0_reg)) {
3269 DRM_DEBUG_KMS("CRTC:%d sharing existing PCH PLL %x (refcount %d, ative %d)\n",
3270 intel_crtc->base.base.id,
3271 pll->pll_reg, pll->refcount, pll->active);
3272
3273 goto found;
3274 }
3275 }
3276
3277 /* Ok no matching timings, maybe there's a free one? */
3278 for (i = 0; i < dev_priv->num_pch_pll; i++) {
3279 pll = &dev_priv->pch_plls[i];
3280 if (pll->refcount == 0) {
3281 DRM_DEBUG_KMS("CRTC:%d allocated PCH PLL %x\n",
3282 intel_crtc->base.base.id, pll->pll_reg);
3283 goto found;
3284 }
3285 }
3286
3287 return NULL;
3288
3289 found:
3290 intel_crtc->pch_pll = pll;
3291 pll->refcount++;
3292 DRM_DEBUG_DRIVER("using pll %d for pipe %c\n", i, pipe_name(intel_crtc->pipe));
3293 prepare: /* separate function? */
3294 DRM_DEBUG_DRIVER("switching PLL %x off\n", pll->pll_reg);
3295
3296 /* Wait for the clocks to stabilize before rewriting the regs */
3297 I915_WRITE(pll->pll_reg, dpll & ~DPLL_VCO_ENABLE);
3298 POSTING_READ(pll->pll_reg);
3299 udelay(150);
3300
3301 I915_WRITE(pll->fp0_reg, fp);
3302 I915_WRITE(pll->pll_reg, dpll & ~DPLL_VCO_ENABLE);
3303 pll->on = false;
3304 return pll;
3305 }
3306
3307 void intel_cpt_verify_modeset(struct drm_device *dev, int pipe)
3308 {
3309 struct drm_i915_private *dev_priv = dev->dev_private;
3310 int dslreg = PIPEDSL(pipe);
3311 u32 temp;
3312
3313 temp = I915_READ(dslreg);
3314 udelay(500);
3315 if (wait_for(I915_READ(dslreg) != temp, 5)) {
3316 if (wait_for(I915_READ(dslreg) != temp, 5))
3317 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
3318 }
3319 }
3320
3321 static void ironlake_crtc_enable(struct drm_crtc *crtc)
3322 {
3323 struct drm_device *dev = crtc->dev;
3324 struct drm_i915_private *dev_priv = dev->dev_private;
3325 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3326 struct intel_encoder *encoder;
3327 int pipe = intel_crtc->pipe;
3328 int plane = intel_crtc->plane;
3329 u32 temp;
3330
3331 WARN_ON(!crtc->enabled);
3332
3333 if (intel_crtc->active)
3334 return;
3335
3336 intel_crtc->active = true;
3337
3338 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
3339 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
3340
3341 intel_update_watermarks(dev);
3342
3343 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
3344 temp = I915_READ(PCH_LVDS);
3345 if ((temp & LVDS_PORT_EN) == 0)
3346 I915_WRITE(PCH_LVDS, temp | LVDS_PORT_EN);
3347 }
3348
3349
3350 if (intel_crtc->config.has_pch_encoder) {
3351 /* Note: FDI PLL enabling _must_ be done before we enable the
3352 * cpu pipes, hence this is separate from all the other fdi/pch
3353 * enabling. */
3354 ironlake_fdi_pll_enable(intel_crtc);
3355 } else {
3356 assert_fdi_tx_disabled(dev_priv, pipe);
3357 assert_fdi_rx_disabled(dev_priv, pipe);
3358 }
3359
3360 for_each_encoder_on_crtc(dev, crtc, encoder)
3361 if (encoder->pre_enable)
3362 encoder->pre_enable(encoder);
3363
3364 /* Enable panel fitting for LVDS */
3365 if (dev_priv->pch_pf_size &&
3366 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
3367 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
3368 /* Force use of hard-coded filter coefficients
3369 * as some pre-programmed values are broken,
3370 * e.g. x201.
3371 */
3372 if (IS_IVYBRIDGE(dev))
3373 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3374 PF_PIPE_SEL_IVB(pipe));
3375 else
3376 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
3377 I915_WRITE(PF_WIN_POS(pipe), dev_priv->pch_pf_pos);
3378 I915_WRITE(PF_WIN_SZ(pipe), dev_priv->pch_pf_size);
3379 }
3380
3381 /*
3382 * On ILK+ LUT must be loaded before the pipe is running but with
3383 * clocks enabled
3384 */
3385 intel_crtc_load_lut(crtc);
3386
3387 intel_enable_pipe(dev_priv, pipe,
3388 intel_crtc->config.has_pch_encoder);
3389 intel_enable_plane(dev_priv, plane, pipe);
3390
3391 if (intel_crtc->config.has_pch_encoder)
3392 ironlake_pch_enable(crtc);
3393
3394 mutex_lock(&dev->struct_mutex);
3395 intel_update_fbc(dev);
3396 mutex_unlock(&dev->struct_mutex);
3397
3398 intel_crtc_update_cursor(crtc, true);
3399
3400 for_each_encoder_on_crtc(dev, crtc, encoder)
3401 encoder->enable(encoder);
3402
3403 if (HAS_PCH_CPT(dev))
3404 intel_cpt_verify_modeset(dev, intel_crtc->pipe);
3405
3406 /*
3407 * There seems to be a race in PCH platform hw (at least on some
3408 * outputs) where an enabled pipe still completes any pageflip right
3409 * away (as if the pipe is off) instead of waiting for vblank. As soon
3410 * as the first vblank happend, everything works as expected. Hence just
3411 * wait for one vblank before returning to avoid strange things
3412 * happening.
3413 */
3414 intel_wait_for_vblank(dev, intel_crtc->pipe);
3415 }
3416
3417 static void haswell_crtc_enable(struct drm_crtc *crtc)
3418 {
3419 struct drm_device *dev = crtc->dev;
3420 struct drm_i915_private *dev_priv = dev->dev_private;
3421 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3422 struct intel_encoder *encoder;
3423 int pipe = intel_crtc->pipe;
3424 int plane = intel_crtc->plane;
3425
3426 WARN_ON(!crtc->enabled);
3427
3428 if (intel_crtc->active)
3429 return;
3430
3431 intel_crtc->active = true;
3432
3433 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
3434 if (intel_crtc->config.has_pch_encoder)
3435 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
3436
3437 intel_update_watermarks(dev);
3438
3439 if (intel_crtc->config.has_pch_encoder)
3440 dev_priv->display.fdi_link_train(crtc);
3441
3442 for_each_encoder_on_crtc(dev, crtc, encoder)
3443 if (encoder->pre_enable)
3444 encoder->pre_enable(encoder);
3445
3446 intel_ddi_enable_pipe_clock(intel_crtc);
3447
3448 /* Enable panel fitting for eDP */
3449 if (dev_priv->pch_pf_size &&
3450 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP)) {
3451 /* Force use of hard-coded filter coefficients
3452 * as some pre-programmed values are broken,
3453 * e.g. x201.
3454 */
3455 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3456 PF_PIPE_SEL_IVB(pipe));
3457 I915_WRITE(PF_WIN_POS(pipe), dev_priv->pch_pf_pos);
3458 I915_WRITE(PF_WIN_SZ(pipe), dev_priv->pch_pf_size);
3459 }
3460
3461 /*
3462 * On ILK+ LUT must be loaded before the pipe is running but with
3463 * clocks enabled
3464 */
3465 intel_crtc_load_lut(crtc);
3466
3467 intel_ddi_set_pipe_settings(crtc);
3468 intel_ddi_enable_transcoder_func(crtc);
3469
3470 intel_enable_pipe(dev_priv, pipe,
3471 intel_crtc->config.has_pch_encoder);
3472 intel_enable_plane(dev_priv, plane, pipe);
3473
3474 if (intel_crtc->config.has_pch_encoder)
3475 lpt_pch_enable(crtc);
3476
3477 mutex_lock(&dev->struct_mutex);
3478 intel_update_fbc(dev);
3479 mutex_unlock(&dev->struct_mutex);
3480
3481 intel_crtc_update_cursor(crtc, true);
3482
3483 for_each_encoder_on_crtc(dev, crtc, encoder)
3484 encoder->enable(encoder);
3485
3486 /*
3487 * There seems to be a race in PCH platform hw (at least on some
3488 * outputs) where an enabled pipe still completes any pageflip right
3489 * away (as if the pipe is off) instead of waiting for vblank. As soon
3490 * as the first vblank happend, everything works as expected. Hence just
3491 * wait for one vblank before returning to avoid strange things
3492 * happening.
3493 */
3494 intel_wait_for_vblank(dev, intel_crtc->pipe);
3495 }
3496
3497 static void ironlake_crtc_disable(struct drm_crtc *crtc)
3498 {
3499 struct drm_device *dev = crtc->dev;
3500 struct drm_i915_private *dev_priv = dev->dev_private;
3501 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3502 struct intel_encoder *encoder;
3503 int pipe = intel_crtc->pipe;
3504 int plane = intel_crtc->plane;
3505 u32 reg, temp;
3506
3507
3508 if (!intel_crtc->active)
3509 return;
3510
3511 for_each_encoder_on_crtc(dev, crtc, encoder)
3512 encoder->disable(encoder);
3513
3514 intel_crtc_wait_for_pending_flips(crtc);
3515 drm_vblank_off(dev, pipe);
3516 intel_crtc_update_cursor(crtc, false);
3517
3518 intel_disable_plane(dev_priv, plane, pipe);
3519
3520 if (dev_priv->cfb_plane == plane)
3521 intel_disable_fbc(dev);
3522
3523 intel_set_pch_fifo_underrun_reporting(dev, pipe, false);
3524 intel_disable_pipe(dev_priv, pipe);
3525
3526 /* Disable PF */
3527 I915_WRITE(PF_CTL(pipe), 0);
3528 I915_WRITE(PF_WIN_SZ(pipe), 0);
3529
3530 for_each_encoder_on_crtc(dev, crtc, encoder)
3531 if (encoder->post_disable)
3532 encoder->post_disable(encoder);
3533
3534 ironlake_fdi_disable(crtc);
3535
3536 ironlake_disable_pch_transcoder(dev_priv, pipe);
3537 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
3538
3539 if (HAS_PCH_CPT(dev)) {
3540 /* disable TRANS_DP_CTL */
3541 reg = TRANS_DP_CTL(pipe);
3542 temp = I915_READ(reg);
3543 temp &= ~(TRANS_DP_OUTPUT_ENABLE | TRANS_DP_PORT_SEL_MASK);
3544 temp |= TRANS_DP_PORT_SEL_NONE;
3545 I915_WRITE(reg, temp);
3546
3547 /* disable DPLL_SEL */
3548 temp = I915_READ(PCH_DPLL_SEL);
3549 switch (pipe) {
3550 case 0:
3551 temp &= ~(TRANSA_DPLL_ENABLE | TRANSA_DPLLB_SEL);
3552 break;
3553 case 1:
3554 temp &= ~(TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
3555 break;
3556 case 2:
3557 /* C shares PLL A or B */
3558 temp &= ~(TRANSC_DPLL_ENABLE | TRANSC_DPLLB_SEL);
3559 break;
3560 default:
3561 BUG(); /* wtf */
3562 }
3563 I915_WRITE(PCH_DPLL_SEL, temp);
3564 }
3565
3566 /* disable PCH DPLL */
3567 intel_disable_pch_pll(intel_crtc);
3568
3569 ironlake_fdi_pll_disable(intel_crtc);
3570
3571 intel_crtc->active = false;
3572 intel_update_watermarks(dev);
3573
3574 mutex_lock(&dev->struct_mutex);
3575 intel_update_fbc(dev);
3576 mutex_unlock(&dev->struct_mutex);
3577 }
3578
3579 static void haswell_crtc_disable(struct drm_crtc *crtc)
3580 {
3581 struct drm_device *dev = crtc->dev;
3582 struct drm_i915_private *dev_priv = dev->dev_private;
3583 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3584 struct intel_encoder *encoder;
3585 int pipe = intel_crtc->pipe;
3586 int plane = intel_crtc->plane;
3587 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
3588
3589 if (!intel_crtc->active)
3590 return;
3591
3592 for_each_encoder_on_crtc(dev, crtc, encoder)
3593 encoder->disable(encoder);
3594
3595 intel_crtc_wait_for_pending_flips(crtc);
3596 drm_vblank_off(dev, pipe);
3597 intel_crtc_update_cursor(crtc, false);
3598
3599 intel_disable_plane(dev_priv, plane, pipe);
3600
3601 if (dev_priv->cfb_plane == plane)
3602 intel_disable_fbc(dev);
3603
3604 if (intel_crtc->config.has_pch_encoder)
3605 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, false);
3606 intel_disable_pipe(dev_priv, pipe);
3607
3608 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
3609
3610 /* XXX: Once we have proper panel fitter state tracking implemented with
3611 * hardware state read/check support we should switch to only disable
3612 * the panel fitter when we know it's used. */
3613 if (intel_using_power_well(dev)) {
3614 I915_WRITE(PF_CTL(pipe), 0);
3615 I915_WRITE(PF_WIN_SZ(pipe), 0);
3616 }
3617
3618 intel_ddi_disable_pipe_clock(intel_crtc);
3619
3620 for_each_encoder_on_crtc(dev, crtc, encoder)
3621 if (encoder->post_disable)
3622 encoder->post_disable(encoder);
3623
3624 if (intel_crtc->config.has_pch_encoder) {
3625 lpt_disable_pch_transcoder(dev_priv);
3626 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
3627 intel_ddi_fdi_disable(crtc);
3628 }
3629
3630 intel_crtc->active = false;
3631 intel_update_watermarks(dev);
3632
3633 mutex_lock(&dev->struct_mutex);
3634 intel_update_fbc(dev);
3635 mutex_unlock(&dev->struct_mutex);
3636 }
3637
3638 static void ironlake_crtc_off(struct drm_crtc *crtc)
3639 {
3640 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3641 intel_put_pch_pll(intel_crtc);
3642 }
3643
3644 static void haswell_crtc_off(struct drm_crtc *crtc)
3645 {
3646 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3647
3648 /* Stop saying we're using TRANSCODER_EDP because some other CRTC might
3649 * start using it. */
3650 intel_crtc->config.cpu_transcoder = (enum transcoder) intel_crtc->pipe;
3651
3652 intel_ddi_put_crtc_pll(crtc);
3653 }
3654
3655 static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
3656 {
3657 if (!enable && intel_crtc->overlay) {
3658 struct drm_device *dev = intel_crtc->base.dev;
3659 struct drm_i915_private *dev_priv = dev->dev_private;
3660
3661 mutex_lock(&dev->struct_mutex);
3662 dev_priv->mm.interruptible = false;
3663 (void) intel_overlay_switch_off(intel_crtc->overlay);
3664 dev_priv->mm.interruptible = true;
3665 mutex_unlock(&dev->struct_mutex);
3666 }
3667
3668 /* Let userspace switch the overlay on again. In most cases userspace
3669 * has to recompute where to put it anyway.
3670 */
3671 }
3672
3673 /**
3674 * i9xx_fixup_plane - ugly workaround for G45 to fire up the hardware
3675 * cursor plane briefly if not already running after enabling the display
3676 * plane.
3677 * This workaround avoids occasional blank screens when self refresh is
3678 * enabled.
3679 */
3680 static void
3681 g4x_fixup_plane(struct drm_i915_private *dev_priv, enum pipe pipe)
3682 {
3683 u32 cntl = I915_READ(CURCNTR(pipe));
3684
3685 if ((cntl & CURSOR_MODE) == 0) {
3686 u32 fw_bcl_self = I915_READ(FW_BLC_SELF);
3687
3688 I915_WRITE(FW_BLC_SELF, fw_bcl_self & ~FW_BLC_SELF_EN);
3689 I915_WRITE(CURCNTR(pipe), CURSOR_MODE_64_ARGB_AX);
3690 intel_wait_for_vblank(dev_priv->dev, pipe);
3691 I915_WRITE(CURCNTR(pipe), cntl);
3692 I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
3693 I915_WRITE(FW_BLC_SELF, fw_bcl_self);
3694 }
3695 }
3696
3697 static void valleyview_crtc_enable(struct drm_crtc *crtc)
3698 {
3699 struct drm_device *dev = crtc->dev;
3700 struct drm_i915_private *dev_priv = dev->dev_private;
3701 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3702 struct intel_encoder *encoder;
3703 int pipe = intel_crtc->pipe;
3704 int plane = intel_crtc->plane;
3705
3706 WARN_ON(!crtc->enabled);
3707
3708 if (intel_crtc->active)
3709 return;
3710
3711 intel_crtc->active = true;
3712 intel_update_watermarks(dev);
3713
3714 mutex_lock(&dev_priv->dpio_lock);
3715
3716 for_each_encoder_on_crtc(dev, crtc, encoder)
3717 if (encoder->pre_pll_enable)
3718 encoder->pre_pll_enable(encoder);
3719
3720 intel_enable_pll(dev_priv, pipe);
3721
3722 for_each_encoder_on_crtc(dev, crtc, encoder)
3723 if (encoder->pre_enable)
3724 encoder->pre_enable(encoder);
3725
3726 /* VLV wants encoder enabling _before_ the pipe is up. */
3727 for_each_encoder_on_crtc(dev, crtc, encoder)
3728 encoder->enable(encoder);
3729
3730 intel_enable_pipe(dev_priv, pipe, false);
3731 intel_enable_plane(dev_priv, plane, pipe);
3732
3733 intel_crtc_load_lut(crtc);
3734 intel_update_fbc(dev);
3735
3736 /* Give the overlay scaler a chance to enable if it's on this pipe */
3737 intel_crtc_dpms_overlay(intel_crtc, true);
3738 intel_crtc_update_cursor(crtc, true);
3739
3740 mutex_unlock(&dev_priv->dpio_lock);
3741 }
3742
3743 static void i9xx_crtc_enable(struct drm_crtc *crtc)
3744 {
3745 struct drm_device *dev = crtc->dev;
3746 struct drm_i915_private *dev_priv = dev->dev_private;
3747 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3748 struct intel_encoder *encoder;
3749 int pipe = intel_crtc->pipe;
3750 int plane = intel_crtc->plane;
3751
3752 WARN_ON(!crtc->enabled);
3753
3754 if (intel_crtc->active)
3755 return;
3756
3757 intel_crtc->active = true;
3758 intel_update_watermarks(dev);
3759
3760 intel_enable_pll(dev_priv, pipe);
3761
3762 for_each_encoder_on_crtc(dev, crtc, encoder)
3763 if (encoder->pre_enable)
3764 encoder->pre_enable(encoder);
3765
3766 intel_enable_pipe(dev_priv, pipe, false);
3767 intel_enable_plane(dev_priv, plane, pipe);
3768 if (IS_G4X(dev))
3769 g4x_fixup_plane(dev_priv, pipe);
3770
3771 intel_crtc_load_lut(crtc);
3772 intel_update_fbc(dev);
3773
3774 /* Give the overlay scaler a chance to enable if it's on this pipe */
3775 intel_crtc_dpms_overlay(intel_crtc, true);
3776 intel_crtc_update_cursor(crtc, true);
3777
3778 for_each_encoder_on_crtc(dev, crtc, encoder)
3779 encoder->enable(encoder);
3780 }
3781
3782 static void i9xx_pfit_disable(struct intel_crtc *crtc)
3783 {
3784 struct drm_device *dev = crtc->base.dev;
3785 struct drm_i915_private *dev_priv = dev->dev_private;
3786 enum pipe pipe;
3787 uint32_t pctl = I915_READ(PFIT_CONTROL);
3788
3789 assert_pipe_disabled(dev_priv, crtc->pipe);
3790
3791 if (INTEL_INFO(dev)->gen >= 4)
3792 pipe = (pctl & PFIT_PIPE_MASK) >> PFIT_PIPE_SHIFT;
3793 else
3794 pipe = PIPE_B;
3795
3796 if (pipe == crtc->pipe) {
3797 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n", pctl);
3798 I915_WRITE(PFIT_CONTROL, 0);
3799 }
3800 }
3801
3802 static void i9xx_crtc_disable(struct drm_crtc *crtc)
3803 {
3804 struct drm_device *dev = crtc->dev;
3805 struct drm_i915_private *dev_priv = dev->dev_private;
3806 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3807 struct intel_encoder *encoder;
3808 int pipe = intel_crtc->pipe;
3809 int plane = intel_crtc->plane;
3810
3811 if (!intel_crtc->active)
3812 return;
3813
3814 for_each_encoder_on_crtc(dev, crtc, encoder)
3815 encoder->disable(encoder);
3816
3817 /* Give the overlay scaler a chance to disable if it's on this pipe */
3818 intel_crtc_wait_for_pending_flips(crtc);
3819 drm_vblank_off(dev, pipe);
3820 intel_crtc_dpms_overlay(intel_crtc, false);
3821 intel_crtc_update_cursor(crtc, false);
3822
3823 if (dev_priv->cfb_plane == plane)
3824 intel_disable_fbc(dev);
3825
3826 intel_disable_plane(dev_priv, plane, pipe);
3827 intel_disable_pipe(dev_priv, pipe);
3828
3829 i9xx_pfit_disable(intel_crtc);
3830
3831 for_each_encoder_on_crtc(dev, crtc, encoder)
3832 if (encoder->post_disable)
3833 encoder->post_disable(encoder);
3834
3835 intel_disable_pll(dev_priv, pipe);
3836
3837 intel_crtc->active = false;
3838 intel_update_fbc(dev);
3839 intel_update_watermarks(dev);
3840 }
3841
3842 static void i9xx_crtc_off(struct drm_crtc *crtc)
3843 {
3844 }
3845
3846 static void intel_crtc_update_sarea(struct drm_crtc *crtc,
3847 bool enabled)
3848 {
3849 struct drm_device *dev = crtc->dev;
3850 struct drm_i915_master_private *master_priv;
3851 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3852 int pipe = intel_crtc->pipe;
3853
3854 if (!dev->primary->master)
3855 return;
3856
3857 master_priv = dev->primary->master->driver_priv;
3858 if (!master_priv->sarea_priv)
3859 return;
3860
3861 switch (pipe) {
3862 case 0:
3863 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
3864 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
3865 break;
3866 case 1:
3867 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
3868 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
3869 break;
3870 default:
3871 DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
3872 break;
3873 }
3874 }
3875
3876 /**
3877 * Sets the power management mode of the pipe and plane.
3878 */
3879 void intel_crtc_update_dpms(struct drm_crtc *crtc)
3880 {
3881 struct drm_device *dev = crtc->dev;
3882 struct drm_i915_private *dev_priv = dev->dev_private;
3883 struct intel_encoder *intel_encoder;
3884 bool enable = false;
3885
3886 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
3887 enable |= intel_encoder->connectors_active;
3888
3889 if (enable)
3890 dev_priv->display.crtc_enable(crtc);
3891 else
3892 dev_priv->display.crtc_disable(crtc);
3893
3894 intel_crtc_update_sarea(crtc, enable);
3895 }
3896
3897 static void intel_crtc_disable(struct drm_crtc *crtc)
3898 {
3899 struct drm_device *dev = crtc->dev;
3900 struct drm_connector *connector;
3901 struct drm_i915_private *dev_priv = dev->dev_private;
3902 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3903
3904 /* crtc should still be enabled when we disable it. */
3905 WARN_ON(!crtc->enabled);
3906
3907 intel_crtc->eld_vld = false;
3908 dev_priv->display.crtc_disable(crtc);
3909 intel_crtc_update_sarea(crtc, false);
3910 dev_priv->display.off(crtc);
3911
3912 assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
3913 assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
3914
3915 if (crtc->fb) {
3916 mutex_lock(&dev->struct_mutex);
3917 intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
3918 mutex_unlock(&dev->struct_mutex);
3919 crtc->fb = NULL;
3920 }
3921
3922 /* Update computed state. */
3923 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3924 if (!connector->encoder || !connector->encoder->crtc)
3925 continue;
3926
3927 if (connector->encoder->crtc != crtc)
3928 continue;
3929
3930 connector->dpms = DRM_MODE_DPMS_OFF;
3931 to_intel_encoder(connector->encoder)->connectors_active = false;
3932 }
3933 }
3934
3935 void intel_modeset_disable(struct drm_device *dev)
3936 {
3937 struct drm_crtc *crtc;
3938
3939 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
3940 if (crtc->enabled)
3941 intel_crtc_disable(crtc);
3942 }
3943 }
3944
3945 void intel_encoder_destroy(struct drm_encoder *encoder)
3946 {
3947 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
3948
3949 drm_encoder_cleanup(encoder);
3950 kfree(intel_encoder);
3951 }
3952
3953 /* Simple dpms helper for encodres with just one connector, no cloning and only
3954 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
3955 * state of the entire output pipe. */
3956 void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
3957 {
3958 if (mode == DRM_MODE_DPMS_ON) {
3959 encoder->connectors_active = true;
3960
3961 intel_crtc_update_dpms(encoder->base.crtc);
3962 } else {
3963 encoder->connectors_active = false;
3964
3965 intel_crtc_update_dpms(encoder->base.crtc);
3966 }
3967 }
3968
3969 /* Cross check the actual hw state with our own modeset state tracking (and it's
3970 * internal consistency). */
3971 static void intel_connector_check_state(struct intel_connector *connector)
3972 {
3973 if (connector->get_hw_state(connector)) {
3974 struct intel_encoder *encoder = connector->encoder;
3975 struct drm_crtc *crtc;
3976 bool encoder_enabled;
3977 enum pipe pipe;
3978
3979 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
3980 connector->base.base.id,
3981 drm_get_connector_name(&connector->base));
3982
3983 WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
3984 "wrong connector dpms state\n");
3985 WARN(connector->base.encoder != &encoder->base,
3986 "active connector not linked to encoder\n");
3987 WARN(!encoder->connectors_active,
3988 "encoder->connectors_active not set\n");
3989
3990 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
3991 WARN(!encoder_enabled, "encoder not enabled\n");
3992 if (WARN_ON(!encoder->base.crtc))
3993 return;
3994
3995 crtc = encoder->base.crtc;
3996
3997 WARN(!crtc->enabled, "crtc not enabled\n");
3998 WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
3999 WARN(pipe != to_intel_crtc(crtc)->pipe,
4000 "encoder active on the wrong pipe\n");
4001 }
4002 }
4003
4004 /* Even simpler default implementation, if there's really no special case to
4005 * consider. */
4006 void intel_connector_dpms(struct drm_connector *connector, int mode)
4007 {
4008 struct intel_encoder *encoder = intel_attached_encoder(connector);
4009
4010 /* All the simple cases only support two dpms states. */
4011 if (mode != DRM_MODE_DPMS_ON)
4012 mode = DRM_MODE_DPMS_OFF;
4013
4014 if (mode == connector->dpms)
4015 return;
4016
4017 connector->dpms = mode;
4018
4019 /* Only need to change hw state when actually enabled */
4020 if (encoder->base.crtc)
4021 intel_encoder_dpms(encoder, mode);
4022 else
4023 WARN_ON(encoder->connectors_active != false);
4024
4025 intel_modeset_check_state(connector->dev);
4026 }
4027
4028 /* Simple connector->get_hw_state implementation for encoders that support only
4029 * one connector and no cloning and hence the encoder state determines the state
4030 * of the connector. */
4031 bool intel_connector_get_hw_state(struct intel_connector *connector)
4032 {
4033 enum pipe pipe = 0;
4034 struct intel_encoder *encoder = connector->encoder;
4035
4036 return encoder->get_hw_state(encoder, &pipe);
4037 }
4038
4039 static bool intel_crtc_compute_config(struct drm_crtc *crtc,
4040 struct intel_crtc_config *pipe_config)
4041 {
4042 struct drm_device *dev = crtc->dev;
4043 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
4044
4045 if (HAS_PCH_SPLIT(dev)) {
4046 /* FDI link clock is fixed at 2.7G */
4047 if (pipe_config->requested_mode.clock * 3
4048 > IRONLAKE_FDI_FREQ * 4)
4049 return false;
4050 }
4051
4052 /* All interlaced capable intel hw wants timings in frames. Note though
4053 * that intel_lvds_mode_fixup does some funny tricks with the crtc
4054 * timings, so we need to be careful not to clobber these.*/
4055 if (!pipe_config->timings_set)
4056 drm_mode_set_crtcinfo(adjusted_mode, 0);
4057
4058 /* WaPruneModeWithIncorrectHsyncOffset: Cantiga+ cannot handle modes
4059 * with a hsync front porch of 0.
4060 */
4061 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
4062 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
4063 return false;
4064
4065 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
4066 pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
4067 } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
4068 /* only a 8bpc pipe, with 6bpc dither through the panel fitter
4069 * for lvds. */
4070 pipe_config->pipe_bpp = 8*3;
4071 }
4072
4073 return true;
4074 }
4075
4076 static int valleyview_get_display_clock_speed(struct drm_device *dev)
4077 {
4078 return 400000; /* FIXME */
4079 }
4080
4081 static int i945_get_display_clock_speed(struct drm_device *dev)
4082 {
4083 return 400000;
4084 }
4085
4086 static int i915_get_display_clock_speed(struct drm_device *dev)
4087 {
4088 return 333000;
4089 }
4090
4091 static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
4092 {
4093 return 200000;
4094 }
4095
4096 static int i915gm_get_display_clock_speed(struct drm_device *dev)
4097 {
4098 u16 gcfgc = 0;
4099
4100 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
4101
4102 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
4103 return 133000;
4104 else {
4105 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
4106 case GC_DISPLAY_CLOCK_333_MHZ:
4107 return 333000;
4108 default:
4109 case GC_DISPLAY_CLOCK_190_200_MHZ:
4110 return 190000;
4111 }
4112 }
4113 }
4114
4115 static int i865_get_display_clock_speed(struct drm_device *dev)
4116 {
4117 return 266000;
4118 }
4119
4120 static int i855_get_display_clock_speed(struct drm_device *dev)
4121 {
4122 u16 hpllcc = 0;
4123 /* Assume that the hardware is in the high speed state. This
4124 * should be the default.
4125 */
4126 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
4127 case GC_CLOCK_133_200:
4128 case GC_CLOCK_100_200:
4129 return 200000;
4130 case GC_CLOCK_166_250:
4131 return 250000;
4132 case GC_CLOCK_100_133:
4133 return 133000;
4134 }
4135
4136 /* Shouldn't happen */
4137 return 0;
4138 }
4139
4140 static int i830_get_display_clock_speed(struct drm_device *dev)
4141 {
4142 return 133000;
4143 }
4144
4145 static void
4146 intel_reduce_ratio(uint32_t *num, uint32_t *den)
4147 {
4148 while (*num > 0xffffff || *den > 0xffffff) {
4149 *num >>= 1;
4150 *den >>= 1;
4151 }
4152 }
4153
4154 void
4155 intel_link_compute_m_n(int bits_per_pixel, int nlanes,
4156 int pixel_clock, int link_clock,
4157 struct intel_link_m_n *m_n)
4158 {
4159 m_n->tu = 64;
4160 m_n->gmch_m = bits_per_pixel * pixel_clock;
4161 m_n->gmch_n = link_clock * nlanes * 8;
4162 intel_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
4163 m_n->link_m = pixel_clock;
4164 m_n->link_n = link_clock;
4165 intel_reduce_ratio(&m_n->link_m, &m_n->link_n);
4166 }
4167
4168 static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
4169 {
4170 if (i915_panel_use_ssc >= 0)
4171 return i915_panel_use_ssc != 0;
4172 return dev_priv->lvds_use_ssc
4173 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
4174 }
4175
4176 static int vlv_get_refclk(struct drm_crtc *crtc)
4177 {
4178 struct drm_device *dev = crtc->dev;
4179 struct drm_i915_private *dev_priv = dev->dev_private;
4180 int refclk = 27000; /* for DP & HDMI */
4181
4182 return 100000; /* only one validated so far */
4183
4184 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
4185 refclk = 96000;
4186 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
4187 if (intel_panel_use_ssc(dev_priv))
4188 refclk = 100000;
4189 else
4190 refclk = 96000;
4191 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP)) {
4192 refclk = 100000;
4193 }
4194
4195 return refclk;
4196 }
4197
4198 static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
4199 {
4200 struct drm_device *dev = crtc->dev;
4201 struct drm_i915_private *dev_priv = dev->dev_private;
4202 int refclk;
4203
4204 if (IS_VALLEYVIEW(dev)) {
4205 refclk = vlv_get_refclk(crtc);
4206 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
4207 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
4208 refclk = dev_priv->lvds_ssc_freq * 1000;
4209 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
4210 refclk / 1000);
4211 } else if (!IS_GEN2(dev)) {
4212 refclk = 96000;
4213 } else {
4214 refclk = 48000;
4215 }
4216
4217 return refclk;
4218 }
4219
4220 static void i9xx_adjust_sdvo_tv_clock(struct intel_crtc *crtc)
4221 {
4222 unsigned dotclock = crtc->config.adjusted_mode.clock;
4223 struct dpll *clock = &crtc->config.dpll;
4224
4225 /* SDVO TV has fixed PLL values depend on its clock range,
4226 this mirrors vbios setting. */
4227 if (dotclock >= 100000 && dotclock < 140500) {
4228 clock->p1 = 2;
4229 clock->p2 = 10;
4230 clock->n = 3;
4231 clock->m1 = 16;
4232 clock->m2 = 8;
4233 } else if (dotclock >= 140500 && dotclock <= 200000) {
4234 clock->p1 = 1;
4235 clock->p2 = 10;
4236 clock->n = 6;
4237 clock->m1 = 12;
4238 clock->m2 = 8;
4239 }
4240
4241 crtc->config.clock_set = true;
4242 }
4243
4244 static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
4245 intel_clock_t *reduced_clock)
4246 {
4247 struct drm_device *dev = crtc->base.dev;
4248 struct drm_i915_private *dev_priv = dev->dev_private;
4249 int pipe = crtc->pipe;
4250 u32 fp, fp2 = 0;
4251 struct dpll *clock = &crtc->config.dpll;
4252
4253 if (IS_PINEVIEW(dev)) {
4254 fp = (1 << clock->n) << 16 | clock->m1 << 8 | clock->m2;
4255 if (reduced_clock)
4256 fp2 = (1 << reduced_clock->n) << 16 |
4257 reduced_clock->m1 << 8 | reduced_clock->m2;
4258 } else {
4259 fp = clock->n << 16 | clock->m1 << 8 | clock->m2;
4260 if (reduced_clock)
4261 fp2 = reduced_clock->n << 16 | reduced_clock->m1 << 8 |
4262 reduced_clock->m2;
4263 }
4264
4265 I915_WRITE(FP0(pipe), fp);
4266
4267 crtc->lowfreq_avail = false;
4268 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
4269 reduced_clock && i915_powersave) {
4270 I915_WRITE(FP1(pipe), fp2);
4271 crtc->lowfreq_avail = true;
4272 } else {
4273 I915_WRITE(FP1(pipe), fp);
4274 }
4275 }
4276
4277 static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv)
4278 {
4279 u32 reg_val;
4280
4281 /*
4282 * PLLB opamp always calibrates to max value of 0x3f, force enable it
4283 * and set it to a reasonable value instead.
4284 */
4285 reg_val = intel_dpio_read(dev_priv, DPIO_IREF(1));
4286 reg_val &= 0xffffff00;
4287 reg_val |= 0x00000030;
4288 intel_dpio_write(dev_priv, DPIO_IREF(1), reg_val);
4289
4290 reg_val = intel_dpio_read(dev_priv, DPIO_CALIBRATION);
4291 reg_val &= 0x8cffffff;
4292 reg_val = 0x8c000000;
4293 intel_dpio_write(dev_priv, DPIO_CALIBRATION, reg_val);
4294
4295 reg_val = intel_dpio_read(dev_priv, DPIO_IREF(1));
4296 reg_val &= 0xffffff00;
4297 intel_dpio_write(dev_priv, DPIO_IREF(1), reg_val);
4298
4299 reg_val = intel_dpio_read(dev_priv, DPIO_CALIBRATION);
4300 reg_val &= 0x00ffffff;
4301 reg_val |= 0xb0000000;
4302 intel_dpio_write(dev_priv, DPIO_CALIBRATION, reg_val);
4303 }
4304
4305 static void intel_dp_set_m_n(struct intel_crtc *crtc)
4306 {
4307 if (crtc->config.has_pch_encoder)
4308 intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
4309 else
4310 intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
4311 }
4312
4313 static void vlv_update_pll(struct intel_crtc *crtc)
4314 {
4315 struct drm_device *dev = crtc->base.dev;
4316 struct drm_i915_private *dev_priv = dev->dev_private;
4317 struct drm_display_mode *adjusted_mode =
4318 &crtc->config.adjusted_mode;
4319 struct intel_encoder *encoder;
4320 int pipe = crtc->pipe;
4321 u32 dpll, mdiv;
4322 u32 bestn, bestm1, bestm2, bestp1, bestp2;
4323 bool is_hdmi;
4324 u32 coreclk, reg_val, temp;
4325
4326 mutex_lock(&dev_priv->dpio_lock);
4327
4328 is_hdmi = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
4329
4330 bestn = crtc->config.dpll.n;
4331 bestm1 = crtc->config.dpll.m1;
4332 bestm2 = crtc->config.dpll.m2;
4333 bestp1 = crtc->config.dpll.p1;
4334 bestp2 = crtc->config.dpll.p2;
4335
4336 /* See eDP HDMI DPIO driver vbios notes doc */
4337
4338 /* PLL B needs special handling */
4339 if (pipe)
4340 vlv_pllb_recal_opamp(dev_priv);
4341
4342 /* Set up Tx target for periodic Rcomp update */
4343 intel_dpio_write(dev_priv, DPIO_IREF_BCAST, 0x0100000f);
4344
4345 /* Disable target IRef on PLL */
4346 reg_val = intel_dpio_read(dev_priv, DPIO_IREF_CTL(pipe));
4347 reg_val &= 0x00ffffff;
4348 intel_dpio_write(dev_priv, DPIO_IREF_CTL(pipe), reg_val);
4349
4350 /* Disable fast lock */
4351 intel_dpio_write(dev_priv, DPIO_FASTCLK_DISABLE, 0x610);
4352
4353 /* Set idtafcrecal before PLL is enabled */
4354 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
4355 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
4356 mdiv |= ((bestn << DPIO_N_SHIFT));
4357 mdiv |= (1 << DPIO_K_SHIFT);
4358 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI) ||
4359 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP) ||
4360 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
4361 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
4362 intel_dpio_write(dev_priv, DPIO_DIV(pipe), mdiv);
4363
4364 mdiv |= DPIO_ENABLE_CALIBRATION;
4365 intel_dpio_write(dev_priv, DPIO_DIV(pipe), mdiv);
4366
4367 /* Set HBR and RBR LPF coefficients */
4368 if (adjusted_mode->clock == 162000 ||
4369 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI))
4370 intel_dpio_write(dev_priv, DPIO_LFP_COEFF(pipe),
4371 0x005f0021);
4372 else
4373 intel_dpio_write(dev_priv, DPIO_LFP_COEFF(pipe),
4374 0x00d0000f);
4375
4376 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP) ||
4377 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT)) {
4378 /* Use SSC source */
4379 if (!pipe)
4380 intel_dpio_write(dev_priv, DPIO_REFSFR(pipe),
4381 0x0df40000);
4382 else
4383 intel_dpio_write(dev_priv, DPIO_REFSFR(pipe),
4384 0x0df70000);
4385 } else { /* HDMI or VGA */
4386 /* Use bend source */
4387 if (!pipe)
4388 intel_dpio_write(dev_priv, DPIO_REFSFR(pipe),
4389 0x0df70000);
4390 else
4391 intel_dpio_write(dev_priv, DPIO_REFSFR(pipe),
4392 0x0df40000);
4393 }
4394
4395 coreclk = intel_dpio_read(dev_priv, DPIO_CORE_CLK(pipe));
4396 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
4397 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT) ||
4398 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))
4399 coreclk |= 0x01000000;
4400 intel_dpio_write(dev_priv, DPIO_CORE_CLK(pipe), coreclk);
4401
4402 intel_dpio_write(dev_priv, DPIO_PLL_CML(pipe), 0x87871000);
4403
4404 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
4405 if (encoder->pre_pll_enable)
4406 encoder->pre_pll_enable(encoder);
4407
4408 /* Enable DPIO clock input */
4409 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
4410 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
4411 if (pipe)
4412 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
4413
4414 dpll |= DPLL_VCO_ENABLE;
4415 I915_WRITE(DPLL(pipe), dpll);
4416 POSTING_READ(DPLL(pipe));
4417 udelay(150);
4418
4419 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
4420 DRM_ERROR("DPLL %d failed to lock\n", pipe);
4421
4422 if (is_hdmi) {
4423 temp = 0;
4424 if (crtc->config.pixel_multiplier > 1) {
4425 temp = (crtc->config.pixel_multiplier - 1)
4426 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
4427 }
4428
4429 I915_WRITE(DPLL_MD(pipe), temp);
4430 POSTING_READ(DPLL_MD(pipe));
4431 }
4432
4433 if (crtc->config.has_dp_encoder)
4434 intel_dp_set_m_n(crtc);
4435
4436 mutex_unlock(&dev_priv->dpio_lock);
4437 }
4438
4439 static void i9xx_update_pll(struct intel_crtc *crtc,
4440 intel_clock_t *reduced_clock,
4441 int num_connectors)
4442 {
4443 struct drm_device *dev = crtc->base.dev;
4444 struct drm_i915_private *dev_priv = dev->dev_private;
4445 struct intel_encoder *encoder;
4446 int pipe = crtc->pipe;
4447 u32 dpll;
4448 bool is_sdvo;
4449 struct dpll *clock = &crtc->config.dpll;
4450
4451 i9xx_update_pll_dividers(crtc, reduced_clock);
4452
4453 is_sdvo = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_SDVO) ||
4454 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
4455
4456 dpll = DPLL_VGA_MODE_DIS;
4457
4458 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS))
4459 dpll |= DPLLB_MODE_LVDS;
4460 else
4461 dpll |= DPLLB_MODE_DAC_SERIAL;
4462
4463 if (is_sdvo) {
4464 if ((crtc->config.pixel_multiplier > 1) &&
4465 (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))) {
4466 dpll |= (crtc->config.pixel_multiplier - 1)
4467 << SDVO_MULTIPLIER_SHIFT_HIRES;
4468 }
4469 dpll |= DPLL_DVO_HIGH_SPEED;
4470 }
4471 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
4472 dpll |= DPLL_DVO_HIGH_SPEED;
4473
4474 /* compute bitmask from p1 value */
4475 if (IS_PINEVIEW(dev))
4476 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
4477 else {
4478 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4479 if (IS_G4X(dev) && reduced_clock)
4480 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
4481 }
4482 switch (clock->p2) {
4483 case 5:
4484 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
4485 break;
4486 case 7:
4487 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
4488 break;
4489 case 10:
4490 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
4491 break;
4492 case 14:
4493 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
4494 break;
4495 }
4496 if (INTEL_INFO(dev)->gen >= 4)
4497 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
4498
4499 if (is_sdvo && intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_TVOUT))
4500 dpll |= PLL_REF_INPUT_TVCLKINBC;
4501 else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_TVOUT))
4502 /* XXX: just matching BIOS for now */
4503 /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
4504 dpll |= 3;
4505 else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
4506 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
4507 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4508 else
4509 dpll |= PLL_REF_INPUT_DREFCLK;
4510
4511 dpll |= DPLL_VCO_ENABLE;
4512 I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
4513 POSTING_READ(DPLL(pipe));
4514 udelay(150);
4515
4516 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
4517 if (encoder->pre_pll_enable)
4518 encoder->pre_pll_enable(encoder);
4519
4520 if (crtc->config.has_dp_encoder)
4521 intel_dp_set_m_n(crtc);
4522
4523 I915_WRITE(DPLL(pipe), dpll);
4524
4525 /* Wait for the clocks to stabilize. */
4526 POSTING_READ(DPLL(pipe));
4527 udelay(150);
4528
4529 if (INTEL_INFO(dev)->gen >= 4) {
4530 u32 temp = 0;
4531 if (is_sdvo) {
4532 temp = 0;
4533 if (crtc->config.pixel_multiplier > 1) {
4534 temp = (crtc->config.pixel_multiplier - 1)
4535 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
4536 }
4537 }
4538 I915_WRITE(DPLL_MD(pipe), temp);
4539 } else {
4540 /* The pixel multiplier can only be updated once the
4541 * DPLL is enabled and the clocks are stable.
4542 *
4543 * So write it again.
4544 */
4545 I915_WRITE(DPLL(pipe), dpll);
4546 }
4547 }
4548
4549 static void i8xx_update_pll(struct intel_crtc *crtc,
4550 struct drm_display_mode *adjusted_mode,
4551 intel_clock_t *reduced_clock,
4552 int num_connectors)
4553 {
4554 struct drm_device *dev = crtc->base.dev;
4555 struct drm_i915_private *dev_priv = dev->dev_private;
4556 struct intel_encoder *encoder;
4557 int pipe = crtc->pipe;
4558 u32 dpll;
4559 struct dpll *clock = &crtc->config.dpll;
4560
4561 i9xx_update_pll_dividers(crtc, reduced_clock);
4562
4563 dpll = DPLL_VGA_MODE_DIS;
4564
4565 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS)) {
4566 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4567 } else {
4568 if (clock->p1 == 2)
4569 dpll |= PLL_P1_DIVIDE_BY_TWO;
4570 else
4571 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4572 if (clock->p2 == 4)
4573 dpll |= PLL_P2_DIVIDE_BY_4;
4574 }
4575
4576 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
4577 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
4578 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4579 else
4580 dpll |= PLL_REF_INPUT_DREFCLK;
4581
4582 dpll |= DPLL_VCO_ENABLE;
4583 I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
4584 POSTING_READ(DPLL(pipe));
4585 udelay(150);
4586
4587 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
4588 if (encoder->pre_pll_enable)
4589 encoder->pre_pll_enable(encoder);
4590
4591 I915_WRITE(DPLL(pipe), dpll);
4592
4593 /* Wait for the clocks to stabilize. */
4594 POSTING_READ(DPLL(pipe));
4595 udelay(150);
4596
4597 /* The pixel multiplier can only be updated once the
4598 * DPLL is enabled and the clocks are stable.
4599 *
4600 * So write it again.
4601 */
4602 I915_WRITE(DPLL(pipe), dpll);
4603 }
4604
4605 static void intel_set_pipe_timings(struct intel_crtc *intel_crtc,
4606 struct drm_display_mode *mode,
4607 struct drm_display_mode *adjusted_mode)
4608 {
4609 struct drm_device *dev = intel_crtc->base.dev;
4610 struct drm_i915_private *dev_priv = dev->dev_private;
4611 enum pipe pipe = intel_crtc->pipe;
4612 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
4613 uint32_t vsyncshift;
4614
4615 if (!IS_GEN2(dev) && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
4616 /* the chip adds 2 halflines automatically */
4617 adjusted_mode->crtc_vtotal -= 1;
4618 adjusted_mode->crtc_vblank_end -= 1;
4619 vsyncshift = adjusted_mode->crtc_hsync_start
4620 - adjusted_mode->crtc_htotal / 2;
4621 } else {
4622 vsyncshift = 0;
4623 }
4624
4625 if (INTEL_INFO(dev)->gen > 3)
4626 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
4627
4628 I915_WRITE(HTOTAL(cpu_transcoder),
4629 (adjusted_mode->crtc_hdisplay - 1) |
4630 ((adjusted_mode->crtc_htotal - 1) << 16));
4631 I915_WRITE(HBLANK(cpu_transcoder),
4632 (adjusted_mode->crtc_hblank_start - 1) |
4633 ((adjusted_mode->crtc_hblank_end - 1) << 16));
4634 I915_WRITE(HSYNC(cpu_transcoder),
4635 (adjusted_mode->crtc_hsync_start - 1) |
4636 ((adjusted_mode->crtc_hsync_end - 1) << 16));
4637
4638 I915_WRITE(VTOTAL(cpu_transcoder),
4639 (adjusted_mode->crtc_vdisplay - 1) |
4640 ((adjusted_mode->crtc_vtotal - 1) << 16));
4641 I915_WRITE(VBLANK(cpu_transcoder),
4642 (adjusted_mode->crtc_vblank_start - 1) |
4643 ((adjusted_mode->crtc_vblank_end - 1) << 16));
4644 I915_WRITE(VSYNC(cpu_transcoder),
4645 (adjusted_mode->crtc_vsync_start - 1) |
4646 ((adjusted_mode->crtc_vsync_end - 1) << 16));
4647
4648 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
4649 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
4650 * documented on the DDI_FUNC_CTL register description, EDP Input Select
4651 * bits. */
4652 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
4653 (pipe == PIPE_B || pipe == PIPE_C))
4654 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
4655
4656 /* pipesrc controls the size that is scaled from, which should
4657 * always be the user's requested size.
4658 */
4659 I915_WRITE(PIPESRC(pipe),
4660 ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
4661 }
4662
4663 static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
4664 {
4665 struct drm_device *dev = intel_crtc->base.dev;
4666 struct drm_i915_private *dev_priv = dev->dev_private;
4667 uint32_t pipeconf;
4668
4669 pipeconf = I915_READ(PIPECONF(intel_crtc->pipe));
4670
4671 if (intel_crtc->pipe == 0 && INTEL_INFO(dev)->gen < 4) {
4672 /* Enable pixel doubling when the dot clock is > 90% of the (display)
4673 * core speed.
4674 *
4675 * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
4676 * pipe == 0 check?
4677 */
4678 if (intel_crtc->config.requested_mode.clock >
4679 dev_priv->display.get_display_clock_speed(dev) * 9 / 10)
4680 pipeconf |= PIPECONF_DOUBLE_WIDE;
4681 else
4682 pipeconf &= ~PIPECONF_DOUBLE_WIDE;
4683 }
4684
4685 /* default to 8bpc */
4686 pipeconf &= ~(PIPECONF_BPC_MASK | PIPECONF_DITHER_EN);
4687 if (intel_crtc->config.has_dp_encoder) {
4688 if (intel_crtc->config.dither) {
4689 pipeconf |= PIPECONF_6BPC |
4690 PIPECONF_DITHER_EN |
4691 PIPECONF_DITHER_TYPE_SP;
4692 }
4693 }
4694
4695 if (IS_VALLEYVIEW(dev) && intel_pipe_has_type(&intel_crtc->base,
4696 INTEL_OUTPUT_EDP)) {
4697 if (intel_crtc->config.dither) {
4698 pipeconf |= PIPECONF_6BPC |
4699 PIPECONF_ENABLE |
4700 I965_PIPECONF_ACTIVE;
4701 }
4702 }
4703
4704 if (HAS_PIPE_CXSR(dev)) {
4705 if (intel_crtc->lowfreq_avail) {
4706 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
4707 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
4708 } else {
4709 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
4710 pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
4711 }
4712 }
4713
4714 pipeconf &= ~PIPECONF_INTERLACE_MASK;
4715 if (!IS_GEN2(dev) &&
4716 intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
4717 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
4718 else
4719 pipeconf |= PIPECONF_PROGRESSIVE;
4720
4721 if (IS_VALLEYVIEW(dev)) {
4722 if (intel_crtc->config.limited_color_range)
4723 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
4724 else
4725 pipeconf &= ~PIPECONF_COLOR_RANGE_SELECT;
4726 }
4727
4728 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
4729 POSTING_READ(PIPECONF(intel_crtc->pipe));
4730 }
4731
4732 static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
4733 int x, int y,
4734 struct drm_framebuffer *fb)
4735 {
4736 struct drm_device *dev = crtc->dev;
4737 struct drm_i915_private *dev_priv = dev->dev_private;
4738 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4739 struct drm_display_mode *adjusted_mode =
4740 &intel_crtc->config.adjusted_mode;
4741 struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
4742 int pipe = intel_crtc->pipe;
4743 int plane = intel_crtc->plane;
4744 int refclk, num_connectors = 0;
4745 intel_clock_t clock, reduced_clock;
4746 u32 dspcntr;
4747 bool ok, has_reduced_clock = false, is_sdvo = false;
4748 bool is_lvds = false, is_tv = false;
4749 struct intel_encoder *encoder;
4750 const intel_limit_t *limit;
4751 int ret;
4752
4753 for_each_encoder_on_crtc(dev, crtc, encoder) {
4754 switch (encoder->type) {
4755 case INTEL_OUTPUT_LVDS:
4756 is_lvds = true;
4757 break;
4758 case INTEL_OUTPUT_SDVO:
4759 case INTEL_OUTPUT_HDMI:
4760 is_sdvo = true;
4761 if (encoder->needs_tv_clock)
4762 is_tv = true;
4763 break;
4764 case INTEL_OUTPUT_TVOUT:
4765 is_tv = true;
4766 break;
4767 }
4768
4769 num_connectors++;
4770 }
4771
4772 refclk = i9xx_get_refclk(crtc, num_connectors);
4773
4774 /*
4775 * Returns a set of divisors for the desired target clock with the given
4776 * refclk, or FALSE. The returned values represent the clock equation:
4777 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
4778 */
4779 limit = intel_limit(crtc, refclk);
4780 ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, NULL,
4781 &clock);
4782 if (!ok) {
4783 DRM_ERROR("Couldn't find PLL settings for mode!\n");
4784 return -EINVAL;
4785 }
4786
4787 /* Ensure that the cursor is valid for the new mode before changing... */
4788 intel_crtc_update_cursor(crtc, true);
4789
4790 if (is_lvds && dev_priv->lvds_downclock_avail) {
4791 /*
4792 * Ensure we match the reduced clock's P to the target clock.
4793 * If the clocks don't match, we can't switch the display clock
4794 * by using the FP0/FP1. In such case we will disable the LVDS
4795 * downclock feature.
4796 */
4797 has_reduced_clock = limit->find_pll(limit, crtc,
4798 dev_priv->lvds_downclock,
4799 refclk,
4800 &clock,
4801 &reduced_clock);
4802 }
4803 /* Compat-code for transition, will disappear. */
4804 if (!intel_crtc->config.clock_set) {
4805 intel_crtc->config.dpll.n = clock.n;
4806 intel_crtc->config.dpll.m1 = clock.m1;
4807 intel_crtc->config.dpll.m2 = clock.m2;
4808 intel_crtc->config.dpll.p1 = clock.p1;
4809 intel_crtc->config.dpll.p2 = clock.p2;
4810 }
4811
4812 if (is_sdvo && is_tv)
4813 i9xx_adjust_sdvo_tv_clock(intel_crtc);
4814
4815 if (IS_GEN2(dev))
4816 i8xx_update_pll(intel_crtc, adjusted_mode,
4817 has_reduced_clock ? &reduced_clock : NULL,
4818 num_connectors);
4819 else if (IS_VALLEYVIEW(dev))
4820 vlv_update_pll(intel_crtc);
4821 else
4822 i9xx_update_pll(intel_crtc,
4823 has_reduced_clock ? &reduced_clock : NULL,
4824 num_connectors);
4825
4826 /* Set up the display plane register */
4827 dspcntr = DISPPLANE_GAMMA_ENABLE;
4828
4829 if (!IS_VALLEYVIEW(dev)) {
4830 if (pipe == 0)
4831 dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
4832 else
4833 dspcntr |= DISPPLANE_SEL_PIPE_B;
4834 }
4835
4836 DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe_name(pipe));
4837 drm_mode_debug_printmodeline(mode);
4838
4839 intel_set_pipe_timings(intel_crtc, mode, adjusted_mode);
4840
4841 /* pipesrc and dspsize control the size that is scaled from,
4842 * which should always be the user's requested size.
4843 */
4844 I915_WRITE(DSPSIZE(plane),
4845 ((mode->vdisplay - 1) << 16) |
4846 (mode->hdisplay - 1));
4847 I915_WRITE(DSPPOS(plane), 0);
4848
4849 i9xx_set_pipeconf(intel_crtc);
4850
4851 I915_WRITE(DSPCNTR(plane), dspcntr);
4852 POSTING_READ(DSPCNTR(plane));
4853
4854 ret = intel_pipe_set_base(crtc, x, y, fb);
4855
4856 intel_update_watermarks(dev);
4857
4858 return ret;
4859 }
4860
4861 static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
4862 struct intel_crtc_config *pipe_config)
4863 {
4864 struct drm_device *dev = crtc->base.dev;
4865 struct drm_i915_private *dev_priv = dev->dev_private;
4866 uint32_t tmp;
4867
4868 tmp = I915_READ(PIPECONF(crtc->pipe));
4869 if (!(tmp & PIPECONF_ENABLE))
4870 return false;
4871
4872 return true;
4873 }
4874
4875 static void ironlake_init_pch_refclk(struct drm_device *dev)
4876 {
4877 struct drm_i915_private *dev_priv = dev->dev_private;
4878 struct drm_mode_config *mode_config = &dev->mode_config;
4879 struct intel_encoder *encoder;
4880 u32 val, final;
4881 bool has_lvds = false;
4882 bool has_cpu_edp = false;
4883 bool has_pch_edp = false;
4884 bool has_panel = false;
4885 bool has_ck505 = false;
4886 bool can_ssc = false;
4887
4888 /* We need to take the global config into account */
4889 list_for_each_entry(encoder, &mode_config->encoder_list,
4890 base.head) {
4891 switch (encoder->type) {
4892 case INTEL_OUTPUT_LVDS:
4893 has_panel = true;
4894 has_lvds = true;
4895 break;
4896 case INTEL_OUTPUT_EDP:
4897 has_panel = true;
4898 if (intel_encoder_is_pch_edp(&encoder->base))
4899 has_pch_edp = true;
4900 else
4901 has_cpu_edp = true;
4902 break;
4903 }
4904 }
4905
4906 if (HAS_PCH_IBX(dev)) {
4907 has_ck505 = dev_priv->display_clock_mode;
4908 can_ssc = has_ck505;
4909 } else {
4910 has_ck505 = false;
4911 can_ssc = true;
4912 }
4913
4914 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_pch_edp %d has_cpu_edp %d has_ck505 %d\n",
4915 has_panel, has_lvds, has_pch_edp, has_cpu_edp,
4916 has_ck505);
4917
4918 /* Ironlake: try to setup display ref clock before DPLL
4919 * enabling. This is only under driver's control after
4920 * PCH B stepping, previous chipset stepping should be
4921 * ignoring this setting.
4922 */
4923 val = I915_READ(PCH_DREF_CONTROL);
4924
4925 /* As we must carefully and slowly disable/enable each source in turn,
4926 * compute the final state we want first and check if we need to
4927 * make any changes at all.
4928 */
4929 final = val;
4930 final &= ~DREF_NONSPREAD_SOURCE_MASK;
4931 if (has_ck505)
4932 final |= DREF_NONSPREAD_CK505_ENABLE;
4933 else
4934 final |= DREF_NONSPREAD_SOURCE_ENABLE;
4935
4936 final &= ~DREF_SSC_SOURCE_MASK;
4937 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
4938 final &= ~DREF_SSC1_ENABLE;
4939
4940 if (has_panel) {
4941 final |= DREF_SSC_SOURCE_ENABLE;
4942
4943 if (intel_panel_use_ssc(dev_priv) && can_ssc)
4944 final |= DREF_SSC1_ENABLE;
4945
4946 if (has_cpu_edp) {
4947 if (intel_panel_use_ssc(dev_priv) && can_ssc)
4948 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
4949 else
4950 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
4951 } else
4952 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
4953 } else {
4954 final |= DREF_SSC_SOURCE_DISABLE;
4955 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
4956 }
4957
4958 if (final == val)
4959 return;
4960
4961 /* Always enable nonspread source */
4962 val &= ~DREF_NONSPREAD_SOURCE_MASK;
4963
4964 if (has_ck505)
4965 val |= DREF_NONSPREAD_CK505_ENABLE;
4966 else
4967 val |= DREF_NONSPREAD_SOURCE_ENABLE;
4968
4969 if (has_panel) {
4970 val &= ~DREF_SSC_SOURCE_MASK;
4971 val |= DREF_SSC_SOURCE_ENABLE;
4972
4973 /* SSC must be turned on before enabling the CPU output */
4974 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
4975 DRM_DEBUG_KMS("Using SSC on panel\n");
4976 val |= DREF_SSC1_ENABLE;
4977 } else
4978 val &= ~DREF_SSC1_ENABLE;
4979
4980 /* Get SSC going before enabling the outputs */
4981 I915_WRITE(PCH_DREF_CONTROL, val);
4982 POSTING_READ(PCH_DREF_CONTROL);
4983 udelay(200);
4984
4985 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
4986
4987 /* Enable CPU source on CPU attached eDP */
4988 if (has_cpu_edp) {
4989 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
4990 DRM_DEBUG_KMS("Using SSC on eDP\n");
4991 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
4992 }
4993 else
4994 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
4995 } else
4996 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
4997
4998 I915_WRITE(PCH_DREF_CONTROL, val);
4999 POSTING_READ(PCH_DREF_CONTROL);
5000 udelay(200);
5001 } else {
5002 DRM_DEBUG_KMS("Disabling SSC entirely\n");
5003
5004 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
5005
5006 /* Turn off CPU output */
5007 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5008
5009 I915_WRITE(PCH_DREF_CONTROL, val);
5010 POSTING_READ(PCH_DREF_CONTROL);
5011 udelay(200);
5012
5013 /* Turn off the SSC source */
5014 val &= ~DREF_SSC_SOURCE_MASK;
5015 val |= DREF_SSC_SOURCE_DISABLE;
5016
5017 /* Turn off SSC1 */
5018 val &= ~DREF_SSC1_ENABLE;
5019
5020 I915_WRITE(PCH_DREF_CONTROL, val);
5021 POSTING_READ(PCH_DREF_CONTROL);
5022 udelay(200);
5023 }
5024
5025 BUG_ON(val != final);
5026 }
5027
5028 /* Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O. */
5029 static void lpt_init_pch_refclk(struct drm_device *dev)
5030 {
5031 struct drm_i915_private *dev_priv = dev->dev_private;
5032 struct drm_mode_config *mode_config = &dev->mode_config;
5033 struct intel_encoder *encoder;
5034 bool has_vga = false;
5035 bool is_sdv = false;
5036 u32 tmp;
5037
5038 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
5039 switch (encoder->type) {
5040 case INTEL_OUTPUT_ANALOG:
5041 has_vga = true;
5042 break;
5043 }
5044 }
5045
5046 if (!has_vga)
5047 return;
5048
5049 mutex_lock(&dev_priv->dpio_lock);
5050
5051 /* XXX: Rip out SDV support once Haswell ships for real. */
5052 if (IS_HASWELL(dev) && (dev->pci_device & 0xFF00) == 0x0C00)
5053 is_sdv = true;
5054
5055 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5056 tmp &= ~SBI_SSCCTL_DISABLE;
5057 tmp |= SBI_SSCCTL_PATHALT;
5058 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5059
5060 udelay(24);
5061
5062 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5063 tmp &= ~SBI_SSCCTL_PATHALT;
5064 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5065
5066 if (!is_sdv) {
5067 tmp = I915_READ(SOUTH_CHICKEN2);
5068 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
5069 I915_WRITE(SOUTH_CHICKEN2, tmp);
5070
5071 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
5072 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
5073 DRM_ERROR("FDI mPHY reset assert timeout\n");
5074
5075 tmp = I915_READ(SOUTH_CHICKEN2);
5076 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
5077 I915_WRITE(SOUTH_CHICKEN2, tmp);
5078
5079 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
5080 FDI_MPHY_IOSFSB_RESET_STATUS) == 0,
5081 100))
5082 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
5083 }
5084
5085 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
5086 tmp &= ~(0xFF << 24);
5087 tmp |= (0x12 << 24);
5088 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
5089
5090 if (is_sdv) {
5091 tmp = intel_sbi_read(dev_priv, 0x800C, SBI_MPHY);
5092 tmp |= 0x7FFF;
5093 intel_sbi_write(dev_priv, 0x800C, tmp, SBI_MPHY);
5094 }
5095
5096 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
5097 tmp |= (1 << 11);
5098 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
5099
5100 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
5101 tmp |= (1 << 11);
5102 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
5103
5104 if (is_sdv) {
5105 tmp = intel_sbi_read(dev_priv, 0x2038, SBI_MPHY);
5106 tmp |= (0x3F << 24) | (0xF << 20) | (0xF << 16);
5107 intel_sbi_write(dev_priv, 0x2038, tmp, SBI_MPHY);
5108
5109 tmp = intel_sbi_read(dev_priv, 0x2138, SBI_MPHY);
5110 tmp |= (0x3F << 24) | (0xF << 20) | (0xF << 16);
5111 intel_sbi_write(dev_priv, 0x2138, tmp, SBI_MPHY);
5112
5113 tmp = intel_sbi_read(dev_priv, 0x203C, SBI_MPHY);
5114 tmp |= (0x3F << 8);
5115 intel_sbi_write(dev_priv, 0x203C, tmp, SBI_MPHY);
5116
5117 tmp = intel_sbi_read(dev_priv, 0x213C, SBI_MPHY);
5118 tmp |= (0x3F << 8);
5119 intel_sbi_write(dev_priv, 0x213C, tmp, SBI_MPHY);
5120 }
5121
5122 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
5123 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5124 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
5125
5126 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
5127 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5128 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
5129
5130 if (!is_sdv) {
5131 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
5132 tmp &= ~(7 << 13);
5133 tmp |= (5 << 13);
5134 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
5135
5136 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
5137 tmp &= ~(7 << 13);
5138 tmp |= (5 << 13);
5139 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
5140 }
5141
5142 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
5143 tmp &= ~0xFF;
5144 tmp |= 0x1C;
5145 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
5146
5147 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
5148 tmp &= ~0xFF;
5149 tmp |= 0x1C;
5150 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
5151
5152 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
5153 tmp &= ~(0xFF << 16);
5154 tmp |= (0x1C << 16);
5155 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
5156
5157 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
5158 tmp &= ~(0xFF << 16);
5159 tmp |= (0x1C << 16);
5160 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
5161
5162 if (!is_sdv) {
5163 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
5164 tmp |= (1 << 27);
5165 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
5166
5167 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
5168 tmp |= (1 << 27);
5169 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
5170
5171 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
5172 tmp &= ~(0xF << 28);
5173 tmp |= (4 << 28);
5174 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
5175
5176 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
5177 tmp &= ~(0xF << 28);
5178 tmp |= (4 << 28);
5179 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
5180 }
5181
5182 /* ULT uses SBI_GEN0, but ULT doesn't have VGA, so we don't care. */
5183 tmp = intel_sbi_read(dev_priv, SBI_DBUFF0, SBI_ICLK);
5184 tmp |= SBI_DBUFF0_ENABLE;
5185 intel_sbi_write(dev_priv, SBI_DBUFF0, tmp, SBI_ICLK);
5186
5187 mutex_unlock(&dev_priv->dpio_lock);
5188 }
5189
5190 /*
5191 * Initialize reference clocks when the driver loads
5192 */
5193 void intel_init_pch_refclk(struct drm_device *dev)
5194 {
5195 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
5196 ironlake_init_pch_refclk(dev);
5197 else if (HAS_PCH_LPT(dev))
5198 lpt_init_pch_refclk(dev);
5199 }
5200
5201 static int ironlake_get_refclk(struct drm_crtc *crtc)
5202 {
5203 struct drm_device *dev = crtc->dev;
5204 struct drm_i915_private *dev_priv = dev->dev_private;
5205 struct intel_encoder *encoder;
5206 struct intel_encoder *edp_encoder = NULL;
5207 int num_connectors = 0;
5208 bool is_lvds = false;
5209
5210 for_each_encoder_on_crtc(dev, crtc, encoder) {
5211 switch (encoder->type) {
5212 case INTEL_OUTPUT_LVDS:
5213 is_lvds = true;
5214 break;
5215 case INTEL_OUTPUT_EDP:
5216 edp_encoder = encoder;
5217 break;
5218 }
5219 num_connectors++;
5220 }
5221
5222 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
5223 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
5224 dev_priv->lvds_ssc_freq);
5225 return dev_priv->lvds_ssc_freq * 1000;
5226 }
5227
5228 return 120000;
5229 }
5230
5231 static void ironlake_set_pipeconf(struct drm_crtc *crtc,
5232 struct drm_display_mode *adjusted_mode,
5233 bool dither)
5234 {
5235 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5236 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5237 int pipe = intel_crtc->pipe;
5238 uint32_t val;
5239
5240 val = I915_READ(PIPECONF(pipe));
5241
5242 val &= ~PIPECONF_BPC_MASK;
5243 switch (intel_crtc->config.pipe_bpp) {
5244 case 18:
5245 val |= PIPECONF_6BPC;
5246 break;
5247 case 24:
5248 val |= PIPECONF_8BPC;
5249 break;
5250 case 30:
5251 val |= PIPECONF_10BPC;
5252 break;
5253 case 36:
5254 val |= PIPECONF_12BPC;
5255 break;
5256 default:
5257 /* Case prevented by intel_choose_pipe_bpp_dither. */
5258 BUG();
5259 }
5260
5261 val &= ~(PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_MASK);
5262 if (dither)
5263 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
5264
5265 val &= ~PIPECONF_INTERLACE_MASK;
5266 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
5267 val |= PIPECONF_INTERLACED_ILK;
5268 else
5269 val |= PIPECONF_PROGRESSIVE;
5270
5271 if (intel_crtc->config.limited_color_range)
5272 val |= PIPECONF_COLOR_RANGE_SELECT;
5273 else
5274 val &= ~PIPECONF_COLOR_RANGE_SELECT;
5275
5276 I915_WRITE(PIPECONF(pipe), val);
5277 POSTING_READ(PIPECONF(pipe));
5278 }
5279
5280 /*
5281 * Set up the pipe CSC unit.
5282 *
5283 * Currently only full range RGB to limited range RGB conversion
5284 * is supported, but eventually this should handle various
5285 * RGB<->YCbCr scenarios as well.
5286 */
5287 static void intel_set_pipe_csc(struct drm_crtc *crtc)
5288 {
5289 struct drm_device *dev = crtc->dev;
5290 struct drm_i915_private *dev_priv = dev->dev_private;
5291 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5292 int pipe = intel_crtc->pipe;
5293 uint16_t coeff = 0x7800; /* 1.0 */
5294
5295 /*
5296 * TODO: Check what kind of values actually come out of the pipe
5297 * with these coeff/postoff values and adjust to get the best
5298 * accuracy. Perhaps we even need to take the bpc value into
5299 * consideration.
5300 */
5301
5302 if (intel_crtc->config.limited_color_range)
5303 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
5304
5305 /*
5306 * GY/GU and RY/RU should be the other way around according
5307 * to BSpec, but reality doesn't agree. Just set them up in
5308 * a way that results in the correct picture.
5309 */
5310 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
5311 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
5312
5313 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
5314 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
5315
5316 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
5317 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
5318
5319 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
5320 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
5321 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
5322
5323 if (INTEL_INFO(dev)->gen > 6) {
5324 uint16_t postoff = 0;
5325
5326 if (intel_crtc->config.limited_color_range)
5327 postoff = (16 * (1 << 13) / 255) & 0x1fff;
5328
5329 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
5330 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
5331 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
5332
5333 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
5334 } else {
5335 uint32_t mode = CSC_MODE_YUV_TO_RGB;
5336
5337 if (intel_crtc->config.limited_color_range)
5338 mode |= CSC_BLACK_SCREEN_OFFSET;
5339
5340 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
5341 }
5342 }
5343
5344 static void haswell_set_pipeconf(struct drm_crtc *crtc,
5345 struct drm_display_mode *adjusted_mode,
5346 bool dither)
5347 {
5348 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5349 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5350 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
5351 uint32_t val;
5352
5353 val = I915_READ(PIPECONF(cpu_transcoder));
5354
5355 val &= ~(PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_MASK);
5356 if (dither)
5357 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
5358
5359 val &= ~PIPECONF_INTERLACE_MASK_HSW;
5360 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
5361 val |= PIPECONF_INTERLACED_ILK;
5362 else
5363 val |= PIPECONF_PROGRESSIVE;
5364
5365 I915_WRITE(PIPECONF(cpu_transcoder), val);
5366 POSTING_READ(PIPECONF(cpu_transcoder));
5367 }
5368
5369 static bool ironlake_compute_clocks(struct drm_crtc *crtc,
5370 struct drm_display_mode *adjusted_mode,
5371 intel_clock_t *clock,
5372 bool *has_reduced_clock,
5373 intel_clock_t *reduced_clock)
5374 {
5375 struct drm_device *dev = crtc->dev;
5376 struct drm_i915_private *dev_priv = dev->dev_private;
5377 struct intel_encoder *intel_encoder;
5378 int refclk;
5379 const intel_limit_t *limit;
5380 bool ret, is_sdvo = false, is_tv = false, is_lvds = false;
5381
5382 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
5383 switch (intel_encoder->type) {
5384 case INTEL_OUTPUT_LVDS:
5385 is_lvds = true;
5386 break;
5387 case INTEL_OUTPUT_SDVO:
5388 case INTEL_OUTPUT_HDMI:
5389 is_sdvo = true;
5390 if (intel_encoder->needs_tv_clock)
5391 is_tv = true;
5392 break;
5393 case INTEL_OUTPUT_TVOUT:
5394 is_tv = true;
5395 break;
5396 }
5397 }
5398
5399 refclk = ironlake_get_refclk(crtc);
5400
5401 /*
5402 * Returns a set of divisors for the desired target clock with the given
5403 * refclk, or FALSE. The returned values represent the clock equation:
5404 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
5405 */
5406 limit = intel_limit(crtc, refclk);
5407 ret = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, NULL,
5408 clock);
5409 if (!ret)
5410 return false;
5411
5412 if (is_lvds && dev_priv->lvds_downclock_avail) {
5413 /*
5414 * Ensure we match the reduced clock's P to the target clock.
5415 * If the clocks don't match, we can't switch the display clock
5416 * by using the FP0/FP1. In such case we will disable the LVDS
5417 * downclock feature.
5418 */
5419 *has_reduced_clock = limit->find_pll(limit, crtc,
5420 dev_priv->lvds_downclock,
5421 refclk,
5422 clock,
5423 reduced_clock);
5424 }
5425
5426 if (is_sdvo && is_tv)
5427 i9xx_adjust_sdvo_tv_clock(to_intel_crtc(crtc));
5428
5429 return true;
5430 }
5431
5432 static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
5433 {
5434 struct drm_i915_private *dev_priv = dev->dev_private;
5435 uint32_t temp;
5436
5437 temp = I915_READ(SOUTH_CHICKEN1);
5438 if (temp & FDI_BC_BIFURCATION_SELECT)
5439 return;
5440
5441 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
5442 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
5443
5444 temp |= FDI_BC_BIFURCATION_SELECT;
5445 DRM_DEBUG_KMS("enabling fdi C rx\n");
5446 I915_WRITE(SOUTH_CHICKEN1, temp);
5447 POSTING_READ(SOUTH_CHICKEN1);
5448 }
5449
5450 static bool ironlake_check_fdi_lanes(struct intel_crtc *intel_crtc)
5451 {
5452 struct drm_device *dev = intel_crtc->base.dev;
5453 struct drm_i915_private *dev_priv = dev->dev_private;
5454 struct intel_crtc *pipe_B_crtc =
5455 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
5456
5457 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
5458 pipe_name(intel_crtc->pipe), intel_crtc->fdi_lanes);
5459 if (intel_crtc->fdi_lanes > 4) {
5460 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
5461 pipe_name(intel_crtc->pipe), intel_crtc->fdi_lanes);
5462 /* Clamp lanes to avoid programming the hw with bogus values. */
5463 intel_crtc->fdi_lanes = 4;
5464
5465 return false;
5466 }
5467
5468 if (INTEL_INFO(dev)->num_pipes == 2)
5469 return true;
5470
5471 switch (intel_crtc->pipe) {
5472 case PIPE_A:
5473 return true;
5474 case PIPE_B:
5475 if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
5476 intel_crtc->fdi_lanes > 2) {
5477 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
5478 pipe_name(intel_crtc->pipe), intel_crtc->fdi_lanes);
5479 /* Clamp lanes to avoid programming the hw with bogus values. */
5480 intel_crtc->fdi_lanes = 2;
5481
5482 return false;
5483 }
5484
5485 if (intel_crtc->fdi_lanes > 2)
5486 WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
5487 else
5488 cpt_enable_fdi_bc_bifurcation(dev);
5489
5490 return true;
5491 case PIPE_C:
5492 if (!pipe_B_crtc->base.enabled || pipe_B_crtc->fdi_lanes <= 2) {
5493 if (intel_crtc->fdi_lanes > 2) {
5494 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
5495 pipe_name(intel_crtc->pipe), intel_crtc->fdi_lanes);
5496 /* Clamp lanes to avoid programming the hw with bogus values. */
5497 intel_crtc->fdi_lanes = 2;
5498
5499 return false;
5500 }
5501 } else {
5502 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
5503 return false;
5504 }
5505
5506 cpt_enable_fdi_bc_bifurcation(dev);
5507
5508 return true;
5509 default:
5510 BUG();
5511 }
5512 }
5513
5514 int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
5515 {
5516 /*
5517 * Account for spread spectrum to avoid
5518 * oversubscribing the link. Max center spread
5519 * is 2.5%; use 5% for safety's sake.
5520 */
5521 u32 bps = target_clock * bpp * 21 / 20;
5522 return bps / (link_bw * 8) + 1;
5523 }
5524
5525 void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
5526 struct intel_link_m_n *m_n)
5527 {
5528 struct drm_device *dev = crtc->base.dev;
5529 struct drm_i915_private *dev_priv = dev->dev_private;
5530 int pipe = crtc->pipe;
5531
5532 I915_WRITE(TRANSDATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
5533 I915_WRITE(TRANSDATA_N1(pipe), m_n->gmch_n);
5534 I915_WRITE(TRANSDPLINK_M1(pipe), m_n->link_m);
5535 I915_WRITE(TRANSDPLINK_N1(pipe), m_n->link_n);
5536 }
5537
5538 void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
5539 struct intel_link_m_n *m_n)
5540 {
5541 struct drm_device *dev = crtc->base.dev;
5542 struct drm_i915_private *dev_priv = dev->dev_private;
5543 int pipe = crtc->pipe;
5544 enum transcoder transcoder = crtc->config.cpu_transcoder;
5545
5546 if (INTEL_INFO(dev)->gen >= 5) {
5547 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
5548 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
5549 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
5550 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
5551 } else {
5552 I915_WRITE(PIPE_GMCH_DATA_M(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
5553 I915_WRITE(PIPE_GMCH_DATA_N(pipe), m_n->gmch_n);
5554 I915_WRITE(PIPE_DP_LINK_M(pipe), m_n->link_m);
5555 I915_WRITE(PIPE_DP_LINK_N(pipe), m_n->link_n);
5556 }
5557 }
5558
5559 static void ironlake_fdi_set_m_n(struct drm_crtc *crtc)
5560 {
5561 struct drm_device *dev = crtc->dev;
5562 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5563 struct drm_display_mode *adjusted_mode =
5564 &intel_crtc->config.adjusted_mode;
5565 struct intel_link_m_n m_n = {0};
5566 int target_clock, lane, link_bw;
5567
5568 /* FDI is a binary signal running at ~2.7GHz, encoding
5569 * each output octet as 10 bits. The actual frequency
5570 * is stored as a divider into a 100MHz clock, and the
5571 * mode pixel clock is stored in units of 1KHz.
5572 * Hence the bw of each lane in terms of the mode signal
5573 * is:
5574 */
5575 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
5576
5577 if (intel_crtc->config.pixel_target_clock)
5578 target_clock = intel_crtc->config.pixel_target_clock;
5579 else
5580 target_clock = adjusted_mode->clock;
5581
5582 lane = ironlake_get_lanes_required(target_clock, link_bw,
5583 intel_crtc->config.pipe_bpp);
5584
5585 intel_crtc->fdi_lanes = lane;
5586
5587 if (intel_crtc->config.pixel_multiplier > 1)
5588 link_bw *= intel_crtc->config.pixel_multiplier;
5589 intel_link_compute_m_n(intel_crtc->config.pipe_bpp, lane, target_clock,
5590 link_bw, &m_n);
5591
5592 intel_cpu_transcoder_set_m_n(intel_crtc, &m_n);
5593 }
5594
5595 static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
5596 intel_clock_t *clock, u32 *fp,
5597 intel_clock_t *reduced_clock, u32 *fp2)
5598 {
5599 struct drm_crtc *crtc = &intel_crtc->base;
5600 struct drm_device *dev = crtc->dev;
5601 struct drm_i915_private *dev_priv = dev->dev_private;
5602 struct intel_encoder *intel_encoder;
5603 uint32_t dpll;
5604 int factor, num_connectors = 0;
5605 bool is_lvds = false, is_sdvo = false, is_tv = false;
5606
5607 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
5608 switch (intel_encoder->type) {
5609 case INTEL_OUTPUT_LVDS:
5610 is_lvds = true;
5611 break;
5612 case INTEL_OUTPUT_SDVO:
5613 case INTEL_OUTPUT_HDMI:
5614 is_sdvo = true;
5615 if (intel_encoder->needs_tv_clock)
5616 is_tv = true;
5617 break;
5618 case INTEL_OUTPUT_TVOUT:
5619 is_tv = true;
5620 break;
5621 }
5622
5623 num_connectors++;
5624 }
5625
5626 /* Enable autotuning of the PLL clock (if permissible) */
5627 factor = 21;
5628 if (is_lvds) {
5629 if ((intel_panel_use_ssc(dev_priv) &&
5630 dev_priv->lvds_ssc_freq == 100) ||
5631 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
5632 factor = 25;
5633 } else if (is_sdvo && is_tv)
5634 factor = 20;
5635
5636 if (clock->m < factor * clock->n)
5637 *fp |= FP_CB_TUNE;
5638
5639 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
5640 *fp2 |= FP_CB_TUNE;
5641
5642 dpll = 0;
5643
5644 if (is_lvds)
5645 dpll |= DPLLB_MODE_LVDS;
5646 else
5647 dpll |= DPLLB_MODE_DAC_SERIAL;
5648 if (is_sdvo) {
5649 if (intel_crtc->config.pixel_multiplier > 1) {
5650 dpll |= (intel_crtc->config.pixel_multiplier - 1)
5651 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
5652 }
5653 dpll |= DPLL_DVO_HIGH_SPEED;
5654 }
5655 if (intel_crtc->config.has_dp_encoder &&
5656 intel_crtc->config.has_pch_encoder)
5657 dpll |= DPLL_DVO_HIGH_SPEED;
5658
5659 /* compute bitmask from p1 value */
5660 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5661 /* also FPA1 */
5662 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
5663
5664 switch (clock->p2) {
5665 case 5:
5666 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
5667 break;
5668 case 7:
5669 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
5670 break;
5671 case 10:
5672 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
5673 break;
5674 case 14:
5675 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
5676 break;
5677 }
5678
5679 if (is_sdvo && is_tv)
5680 dpll |= PLL_REF_INPUT_TVCLKINBC;
5681 else if (is_tv)
5682 /* XXX: just matching BIOS for now */
5683 /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
5684 dpll |= 3;
5685 else if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5686 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5687 else
5688 dpll |= PLL_REF_INPUT_DREFCLK;
5689
5690 return dpll;
5691 }
5692
5693 static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
5694 int x, int y,
5695 struct drm_framebuffer *fb)
5696 {
5697 struct drm_device *dev = crtc->dev;
5698 struct drm_i915_private *dev_priv = dev->dev_private;
5699 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5700 struct drm_display_mode *adjusted_mode =
5701 &intel_crtc->config.adjusted_mode;
5702 struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
5703 int pipe = intel_crtc->pipe;
5704 int plane = intel_crtc->plane;
5705 int num_connectors = 0;
5706 intel_clock_t clock, reduced_clock;
5707 u32 dpll = 0, fp = 0, fp2 = 0;
5708 bool ok, has_reduced_clock = false;
5709 bool is_lvds = false;
5710 struct intel_encoder *encoder;
5711 int ret;
5712 bool dither, fdi_config_ok;
5713
5714 for_each_encoder_on_crtc(dev, crtc, encoder) {
5715 switch (encoder->type) {
5716 case INTEL_OUTPUT_LVDS:
5717 is_lvds = true;
5718 break;
5719 }
5720
5721 num_connectors++;
5722 }
5723
5724 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
5725 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
5726
5727 intel_crtc->config.cpu_transcoder = pipe;
5728
5729 ok = ironlake_compute_clocks(crtc, adjusted_mode, &clock,
5730 &has_reduced_clock, &reduced_clock);
5731 if (!ok) {
5732 DRM_ERROR("Couldn't find PLL settings for mode!\n");
5733 return -EINVAL;
5734 }
5735 /* Compat-code for transition, will disappear. */
5736 if (!intel_crtc->config.clock_set) {
5737 intel_crtc->config.dpll.n = clock.n;
5738 intel_crtc->config.dpll.m1 = clock.m1;
5739 intel_crtc->config.dpll.m2 = clock.m2;
5740 intel_crtc->config.dpll.p1 = clock.p1;
5741 intel_crtc->config.dpll.p2 = clock.p2;
5742 }
5743
5744 /* Ensure that the cursor is valid for the new mode before changing... */
5745 intel_crtc_update_cursor(crtc, true);
5746
5747 /* determine panel color depth */
5748 dither = intel_crtc->config.dither;
5749 if (is_lvds && dev_priv->lvds_dither)
5750 dither = true;
5751
5752 DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe_name(pipe));
5753 drm_mode_debug_printmodeline(mode);
5754
5755 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
5756 if (intel_crtc->config.has_pch_encoder) {
5757 struct intel_pch_pll *pll;
5758
5759 fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
5760 if (has_reduced_clock)
5761 fp2 = reduced_clock.n << 16 | reduced_clock.m1 << 8 |
5762 reduced_clock.m2;
5763
5764 dpll = ironlake_compute_dpll(intel_crtc, &clock,
5765 &fp, &reduced_clock,
5766 has_reduced_clock ? &fp2 : NULL);
5767
5768 pll = intel_get_pch_pll(intel_crtc, dpll, fp);
5769 if (pll == NULL) {
5770 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
5771 pipe_name(pipe));
5772 return -EINVAL;
5773 }
5774 } else
5775 intel_put_pch_pll(intel_crtc);
5776
5777 if (intel_crtc->config.has_dp_encoder)
5778 intel_dp_set_m_n(intel_crtc);
5779
5780 for_each_encoder_on_crtc(dev, crtc, encoder)
5781 if (encoder->pre_pll_enable)
5782 encoder->pre_pll_enable(encoder);
5783
5784 if (intel_crtc->pch_pll) {
5785 I915_WRITE(intel_crtc->pch_pll->pll_reg, dpll);
5786
5787 /* Wait for the clocks to stabilize. */
5788 POSTING_READ(intel_crtc->pch_pll->pll_reg);
5789 udelay(150);
5790
5791 /* The pixel multiplier can only be updated once the
5792 * DPLL is enabled and the clocks are stable.
5793 *
5794 * So write it again.
5795 */
5796 I915_WRITE(intel_crtc->pch_pll->pll_reg, dpll);
5797 }
5798
5799 intel_crtc->lowfreq_avail = false;
5800 if (intel_crtc->pch_pll) {
5801 if (is_lvds && has_reduced_clock && i915_powersave) {
5802 I915_WRITE(intel_crtc->pch_pll->fp1_reg, fp2);
5803 intel_crtc->lowfreq_avail = true;
5804 } else {
5805 I915_WRITE(intel_crtc->pch_pll->fp1_reg, fp);
5806 }
5807 }
5808
5809 intel_set_pipe_timings(intel_crtc, mode, adjusted_mode);
5810
5811 /* Note, this also computes intel_crtc->fdi_lanes which is used below in
5812 * ironlake_check_fdi_lanes. */
5813 intel_crtc->fdi_lanes = 0;
5814 if (intel_crtc->config.has_pch_encoder)
5815 ironlake_fdi_set_m_n(crtc);
5816
5817 fdi_config_ok = ironlake_check_fdi_lanes(intel_crtc);
5818
5819 ironlake_set_pipeconf(crtc, adjusted_mode, dither);
5820
5821 /* Set up the display plane register */
5822 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
5823 POSTING_READ(DSPCNTR(plane));
5824
5825 ret = intel_pipe_set_base(crtc, x, y, fb);
5826
5827 intel_update_watermarks(dev);
5828
5829 intel_update_linetime_watermarks(dev, pipe, adjusted_mode);
5830
5831 return fdi_config_ok ? ret : -EINVAL;
5832 }
5833
5834 static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
5835 struct intel_crtc_config *pipe_config)
5836 {
5837 struct drm_device *dev = crtc->base.dev;
5838 struct drm_i915_private *dev_priv = dev->dev_private;
5839 uint32_t tmp;
5840
5841 tmp = I915_READ(PIPECONF(crtc->pipe));
5842 if (!(tmp & PIPECONF_ENABLE))
5843 return false;
5844
5845 if (I915_READ(TRANSCONF(crtc->pipe)) & TRANS_ENABLE)
5846 pipe_config->has_pch_encoder = true;
5847
5848 return true;
5849 }
5850
5851 static void haswell_modeset_global_resources(struct drm_device *dev)
5852 {
5853 struct drm_i915_private *dev_priv = dev->dev_private;
5854 bool enable = false;
5855 struct intel_crtc *crtc;
5856 struct intel_encoder *encoder;
5857
5858 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
5859 if (crtc->pipe != PIPE_A && crtc->base.enabled)
5860 enable = true;
5861 /* XXX: Should check for edp transcoder here, but thanks to init
5862 * sequence that's not yet available. Just in case desktop eDP
5863 * on PORT D is possible on haswell, too. */
5864 }
5865
5866 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
5867 base.head) {
5868 if (encoder->type != INTEL_OUTPUT_EDP &&
5869 encoder->connectors_active)
5870 enable = true;
5871 }
5872
5873 /* Even the eDP panel fitter is outside the always-on well. */
5874 if (dev_priv->pch_pf_size)
5875 enable = true;
5876
5877 intel_set_power_well(dev, enable);
5878 }
5879
5880 static int haswell_crtc_mode_set(struct drm_crtc *crtc,
5881 int x, int y,
5882 struct drm_framebuffer *fb)
5883 {
5884 struct drm_device *dev = crtc->dev;
5885 struct drm_i915_private *dev_priv = dev->dev_private;
5886 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5887 struct drm_display_mode *adjusted_mode =
5888 &intel_crtc->config.adjusted_mode;
5889 struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
5890 int pipe = intel_crtc->pipe;
5891 int plane = intel_crtc->plane;
5892 int num_connectors = 0;
5893 bool is_cpu_edp = false;
5894 struct intel_encoder *encoder;
5895 int ret;
5896 bool dither;
5897
5898 for_each_encoder_on_crtc(dev, crtc, encoder) {
5899 switch (encoder->type) {
5900 case INTEL_OUTPUT_EDP:
5901 if (!intel_encoder_is_pch_edp(&encoder->base))
5902 is_cpu_edp = true;
5903 break;
5904 }
5905
5906 num_connectors++;
5907 }
5908
5909 if (is_cpu_edp)
5910 intel_crtc->config.cpu_transcoder = TRANSCODER_EDP;
5911 else
5912 intel_crtc->config.cpu_transcoder = pipe;
5913
5914 /* We are not sure yet this won't happen. */
5915 WARN(!HAS_PCH_LPT(dev), "Unexpected PCH type %d\n",
5916 INTEL_PCH_TYPE(dev));
5917
5918 WARN(num_connectors != 1, "%d connectors attached to pipe %c\n",
5919 num_connectors, pipe_name(pipe));
5920
5921 WARN_ON(I915_READ(PIPECONF(intel_crtc->config.cpu_transcoder)) &
5922 (PIPECONF_ENABLE | I965_PIPECONF_ACTIVE));
5923
5924 WARN_ON(I915_READ(DSPCNTR(plane)) & DISPLAY_PLANE_ENABLE);
5925
5926 if (!intel_ddi_pll_mode_set(crtc, adjusted_mode->clock))
5927 return -EINVAL;
5928
5929 /* Ensure that the cursor is valid for the new mode before changing... */
5930 intel_crtc_update_cursor(crtc, true);
5931
5932 /* determine panel color depth */
5933 dither = intel_crtc->config.dither;
5934
5935 DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe_name(pipe));
5936 drm_mode_debug_printmodeline(mode);
5937
5938 if (intel_crtc->config.has_dp_encoder)
5939 intel_dp_set_m_n(intel_crtc);
5940
5941 intel_crtc->lowfreq_avail = false;
5942
5943 intel_set_pipe_timings(intel_crtc, mode, adjusted_mode);
5944
5945 if (intel_crtc->config.has_pch_encoder)
5946 ironlake_fdi_set_m_n(crtc);
5947
5948 haswell_set_pipeconf(crtc, adjusted_mode, dither);
5949
5950 intel_set_pipe_csc(crtc);
5951
5952 /* Set up the display plane register */
5953 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE | DISPPLANE_PIPE_CSC_ENABLE);
5954 POSTING_READ(DSPCNTR(plane));
5955
5956 ret = intel_pipe_set_base(crtc, x, y, fb);
5957
5958 intel_update_watermarks(dev);
5959
5960 intel_update_linetime_watermarks(dev, pipe, adjusted_mode);
5961
5962 return ret;
5963 }
5964
5965 static bool haswell_get_pipe_config(struct intel_crtc *crtc,
5966 struct intel_crtc_config *pipe_config)
5967 {
5968 struct drm_device *dev = crtc->base.dev;
5969 struct drm_i915_private *dev_priv = dev->dev_private;
5970 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
5971 uint32_t tmp;
5972
5973 if (!intel_using_power_well(dev_priv->dev) &&
5974 cpu_transcoder != TRANSCODER_EDP)
5975 return false;
5976
5977 tmp = I915_READ(PIPECONF(cpu_transcoder));
5978 if (!(tmp & PIPECONF_ENABLE))
5979 return false;
5980
5981 /*
5982 * Haswell has only FDI/PCH transcoder A. It is which is connected to
5983 * DDI E. So just check whether this pipe is wired to DDI E and whether
5984 * the PCH transcoder is on.
5985 */
5986 tmp = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
5987 if ((tmp & TRANS_DDI_PORT_MASK) == TRANS_DDI_SELECT_PORT(PORT_E) &&
5988 I915_READ(TRANSCONF(PIPE_A)) & TRANS_ENABLE)
5989 pipe_config->has_pch_encoder = true;
5990
5991 return true;
5992 }
5993
5994 static int intel_crtc_mode_set(struct drm_crtc *crtc,
5995 int x, int y,
5996 struct drm_framebuffer *fb)
5997 {
5998 struct drm_device *dev = crtc->dev;
5999 struct drm_i915_private *dev_priv = dev->dev_private;
6000 struct drm_encoder_helper_funcs *encoder_funcs;
6001 struct intel_encoder *encoder;
6002 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6003 struct drm_display_mode *adjusted_mode =
6004 &intel_crtc->config.adjusted_mode;
6005 struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
6006 int pipe = intel_crtc->pipe;
6007 int ret;
6008
6009 drm_vblank_pre_modeset(dev, pipe);
6010
6011 ret = dev_priv->display.crtc_mode_set(crtc, x, y, fb);
6012
6013 drm_vblank_post_modeset(dev, pipe);
6014
6015 if (ret != 0)
6016 return ret;
6017
6018 for_each_encoder_on_crtc(dev, crtc, encoder) {
6019 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
6020 encoder->base.base.id,
6021 drm_get_encoder_name(&encoder->base),
6022 mode->base.id, mode->name);
6023 if (encoder->mode_set) {
6024 encoder->mode_set(encoder);
6025 } else {
6026 encoder_funcs = encoder->base.helper_private;
6027 encoder_funcs->mode_set(&encoder->base, mode, adjusted_mode);
6028 }
6029 }
6030
6031 return 0;
6032 }
6033
6034 static bool intel_eld_uptodate(struct drm_connector *connector,
6035 int reg_eldv, uint32_t bits_eldv,
6036 int reg_elda, uint32_t bits_elda,
6037 int reg_edid)
6038 {
6039 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6040 uint8_t *eld = connector->eld;
6041 uint32_t i;
6042
6043 i = I915_READ(reg_eldv);
6044 i &= bits_eldv;
6045
6046 if (!eld[0])
6047 return !i;
6048
6049 if (!i)
6050 return false;
6051
6052 i = I915_READ(reg_elda);
6053 i &= ~bits_elda;
6054 I915_WRITE(reg_elda, i);
6055
6056 for (i = 0; i < eld[2]; i++)
6057 if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
6058 return false;
6059
6060 return true;
6061 }
6062
6063 static void g4x_write_eld(struct drm_connector *connector,
6064 struct drm_crtc *crtc)
6065 {
6066 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6067 uint8_t *eld = connector->eld;
6068 uint32_t eldv;
6069 uint32_t len;
6070 uint32_t i;
6071
6072 i = I915_READ(G4X_AUD_VID_DID);
6073
6074 if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
6075 eldv = G4X_ELDV_DEVCL_DEVBLC;
6076 else
6077 eldv = G4X_ELDV_DEVCTG;
6078
6079 if (intel_eld_uptodate(connector,
6080 G4X_AUD_CNTL_ST, eldv,
6081 G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
6082 G4X_HDMIW_HDMIEDID))
6083 return;
6084
6085 i = I915_READ(G4X_AUD_CNTL_ST);
6086 i &= ~(eldv | G4X_ELD_ADDR);
6087 len = (i >> 9) & 0x1f; /* ELD buffer size */
6088 I915_WRITE(G4X_AUD_CNTL_ST, i);
6089
6090 if (!eld[0])
6091 return;
6092
6093 len = min_t(uint8_t, eld[2], len);
6094 DRM_DEBUG_DRIVER("ELD size %d\n", len);
6095 for (i = 0; i < len; i++)
6096 I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
6097
6098 i = I915_READ(G4X_AUD_CNTL_ST);
6099 i |= eldv;
6100 I915_WRITE(G4X_AUD_CNTL_ST, i);
6101 }
6102
6103 static void haswell_write_eld(struct drm_connector *connector,
6104 struct drm_crtc *crtc)
6105 {
6106 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6107 uint8_t *eld = connector->eld;
6108 struct drm_device *dev = crtc->dev;
6109 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6110 uint32_t eldv;
6111 uint32_t i;
6112 int len;
6113 int pipe = to_intel_crtc(crtc)->pipe;
6114 int tmp;
6115
6116 int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
6117 int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
6118 int aud_config = HSW_AUD_CFG(pipe);
6119 int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
6120
6121
6122 DRM_DEBUG_DRIVER("HDMI: Haswell Audio initialize....\n");
6123
6124 /* Audio output enable */
6125 DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
6126 tmp = I915_READ(aud_cntrl_st2);
6127 tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
6128 I915_WRITE(aud_cntrl_st2, tmp);
6129
6130 /* Wait for 1 vertical blank */
6131 intel_wait_for_vblank(dev, pipe);
6132
6133 /* Set ELD valid state */
6134 tmp = I915_READ(aud_cntrl_st2);
6135 DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%8x\n", tmp);
6136 tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
6137 I915_WRITE(aud_cntrl_st2, tmp);
6138 tmp = I915_READ(aud_cntrl_st2);
6139 DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%8x\n", tmp);
6140
6141 /* Enable HDMI mode */
6142 tmp = I915_READ(aud_config);
6143 DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%8x\n", tmp);
6144 /* clear N_programing_enable and N_value_index */
6145 tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
6146 I915_WRITE(aud_config, tmp);
6147
6148 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
6149
6150 eldv = AUDIO_ELD_VALID_A << (pipe * 4);
6151 intel_crtc->eld_vld = true;
6152
6153 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
6154 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
6155 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
6156 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
6157 } else
6158 I915_WRITE(aud_config, 0);
6159
6160 if (intel_eld_uptodate(connector,
6161 aud_cntrl_st2, eldv,
6162 aud_cntl_st, IBX_ELD_ADDRESS,
6163 hdmiw_hdmiedid))
6164 return;
6165
6166 i = I915_READ(aud_cntrl_st2);
6167 i &= ~eldv;
6168 I915_WRITE(aud_cntrl_st2, i);
6169
6170 if (!eld[0])
6171 return;
6172
6173 i = I915_READ(aud_cntl_st);
6174 i &= ~IBX_ELD_ADDRESS;
6175 I915_WRITE(aud_cntl_st, i);
6176 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
6177 DRM_DEBUG_DRIVER("port num:%d\n", i);
6178
6179 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
6180 DRM_DEBUG_DRIVER("ELD size %d\n", len);
6181 for (i = 0; i < len; i++)
6182 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
6183
6184 i = I915_READ(aud_cntrl_st2);
6185 i |= eldv;
6186 I915_WRITE(aud_cntrl_st2, i);
6187
6188 }
6189
6190 static void ironlake_write_eld(struct drm_connector *connector,
6191 struct drm_crtc *crtc)
6192 {
6193 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6194 uint8_t *eld = connector->eld;
6195 uint32_t eldv;
6196 uint32_t i;
6197 int len;
6198 int hdmiw_hdmiedid;
6199 int aud_config;
6200 int aud_cntl_st;
6201 int aud_cntrl_st2;
6202 int pipe = to_intel_crtc(crtc)->pipe;
6203
6204 if (HAS_PCH_IBX(connector->dev)) {
6205 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
6206 aud_config = IBX_AUD_CFG(pipe);
6207 aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
6208 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
6209 } else {
6210 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
6211 aud_config = CPT_AUD_CFG(pipe);
6212 aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
6213 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
6214 }
6215
6216 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
6217
6218 i = I915_READ(aud_cntl_st);
6219 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
6220 if (!i) {
6221 DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
6222 /* operate blindly on all ports */
6223 eldv = IBX_ELD_VALIDB;
6224 eldv |= IBX_ELD_VALIDB << 4;
6225 eldv |= IBX_ELD_VALIDB << 8;
6226 } else {
6227 DRM_DEBUG_DRIVER("ELD on port %c\n", port_name(i));
6228 eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
6229 }
6230
6231 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
6232 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
6233 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
6234 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
6235 } else
6236 I915_WRITE(aud_config, 0);
6237
6238 if (intel_eld_uptodate(connector,
6239 aud_cntrl_st2, eldv,
6240 aud_cntl_st, IBX_ELD_ADDRESS,
6241 hdmiw_hdmiedid))
6242 return;
6243
6244 i = I915_READ(aud_cntrl_st2);
6245 i &= ~eldv;
6246 I915_WRITE(aud_cntrl_st2, i);
6247
6248 if (!eld[0])
6249 return;
6250
6251 i = I915_READ(aud_cntl_st);
6252 i &= ~IBX_ELD_ADDRESS;
6253 I915_WRITE(aud_cntl_st, i);
6254
6255 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
6256 DRM_DEBUG_DRIVER("ELD size %d\n", len);
6257 for (i = 0; i < len; i++)
6258 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
6259
6260 i = I915_READ(aud_cntrl_st2);
6261 i |= eldv;
6262 I915_WRITE(aud_cntrl_st2, i);
6263 }
6264
6265 void intel_write_eld(struct drm_encoder *encoder,
6266 struct drm_display_mode *mode)
6267 {
6268 struct drm_crtc *crtc = encoder->crtc;
6269 struct drm_connector *connector;
6270 struct drm_device *dev = encoder->dev;
6271 struct drm_i915_private *dev_priv = dev->dev_private;
6272
6273 connector = drm_select_eld(encoder, mode);
6274 if (!connector)
6275 return;
6276
6277 DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6278 connector->base.id,
6279 drm_get_connector_name(connector),
6280 connector->encoder->base.id,
6281 drm_get_encoder_name(connector->encoder));
6282
6283 connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
6284
6285 if (dev_priv->display.write_eld)
6286 dev_priv->display.write_eld(connector, crtc);
6287 }
6288
6289 /** Loads the palette/gamma unit for the CRTC with the prepared values */
6290 void intel_crtc_load_lut(struct drm_crtc *crtc)
6291 {
6292 struct drm_device *dev = crtc->dev;
6293 struct drm_i915_private *dev_priv = dev->dev_private;
6294 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6295 int palreg = PALETTE(intel_crtc->pipe);
6296 int i;
6297
6298 /* The clocks have to be on to load the palette. */
6299 if (!crtc->enabled || !intel_crtc->active)
6300 return;
6301
6302 /* use legacy palette for Ironlake */
6303 if (HAS_PCH_SPLIT(dev))
6304 palreg = LGC_PALETTE(intel_crtc->pipe);
6305
6306 for (i = 0; i < 256; i++) {
6307 I915_WRITE(palreg + 4 * i,
6308 (intel_crtc->lut_r[i] << 16) |
6309 (intel_crtc->lut_g[i] << 8) |
6310 intel_crtc->lut_b[i]);
6311 }
6312 }
6313
6314 static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
6315 {
6316 struct drm_device *dev = crtc->dev;
6317 struct drm_i915_private *dev_priv = dev->dev_private;
6318 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6319 bool visible = base != 0;
6320 u32 cntl;
6321
6322 if (intel_crtc->cursor_visible == visible)
6323 return;
6324
6325 cntl = I915_READ(_CURACNTR);
6326 if (visible) {
6327 /* On these chipsets we can only modify the base whilst
6328 * the cursor is disabled.
6329 */
6330 I915_WRITE(_CURABASE, base);
6331
6332 cntl &= ~(CURSOR_FORMAT_MASK);
6333 /* XXX width must be 64, stride 256 => 0x00 << 28 */
6334 cntl |= CURSOR_ENABLE |
6335 CURSOR_GAMMA_ENABLE |
6336 CURSOR_FORMAT_ARGB;
6337 } else
6338 cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
6339 I915_WRITE(_CURACNTR, cntl);
6340
6341 intel_crtc->cursor_visible = visible;
6342 }
6343
6344 static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
6345 {
6346 struct drm_device *dev = crtc->dev;
6347 struct drm_i915_private *dev_priv = dev->dev_private;
6348 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6349 int pipe = intel_crtc->pipe;
6350 bool visible = base != 0;
6351
6352 if (intel_crtc->cursor_visible != visible) {
6353 uint32_t cntl = I915_READ(CURCNTR(pipe));
6354 if (base) {
6355 cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
6356 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
6357 cntl |= pipe << 28; /* Connect to correct pipe */
6358 } else {
6359 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
6360 cntl |= CURSOR_MODE_DISABLE;
6361 }
6362 I915_WRITE(CURCNTR(pipe), cntl);
6363
6364 intel_crtc->cursor_visible = visible;
6365 }
6366 /* and commit changes on next vblank */
6367 I915_WRITE(CURBASE(pipe), base);
6368 }
6369
6370 static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
6371 {
6372 struct drm_device *dev = crtc->dev;
6373 struct drm_i915_private *dev_priv = dev->dev_private;
6374 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6375 int pipe = intel_crtc->pipe;
6376 bool visible = base != 0;
6377
6378 if (intel_crtc->cursor_visible != visible) {
6379 uint32_t cntl = I915_READ(CURCNTR_IVB(pipe));
6380 if (base) {
6381 cntl &= ~CURSOR_MODE;
6382 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
6383 } else {
6384 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
6385 cntl |= CURSOR_MODE_DISABLE;
6386 }
6387 if (IS_HASWELL(dev))
6388 cntl |= CURSOR_PIPE_CSC_ENABLE;
6389 I915_WRITE(CURCNTR_IVB(pipe), cntl);
6390
6391 intel_crtc->cursor_visible = visible;
6392 }
6393 /* and commit changes on next vblank */
6394 I915_WRITE(CURBASE_IVB(pipe), base);
6395 }
6396
6397 /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
6398 static void intel_crtc_update_cursor(struct drm_crtc *crtc,
6399 bool on)
6400 {
6401 struct drm_device *dev = crtc->dev;
6402 struct drm_i915_private *dev_priv = dev->dev_private;
6403 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6404 int pipe = intel_crtc->pipe;
6405 int x = intel_crtc->cursor_x;
6406 int y = intel_crtc->cursor_y;
6407 u32 base, pos;
6408 bool visible;
6409
6410 pos = 0;
6411
6412 if (on && crtc->enabled && crtc->fb) {
6413 base = intel_crtc->cursor_addr;
6414 if (x > (int) crtc->fb->width)
6415 base = 0;
6416
6417 if (y > (int) crtc->fb->height)
6418 base = 0;
6419 } else
6420 base = 0;
6421
6422 if (x < 0) {
6423 if (x + intel_crtc->cursor_width < 0)
6424 base = 0;
6425
6426 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
6427 x = -x;
6428 }
6429 pos |= x << CURSOR_X_SHIFT;
6430
6431 if (y < 0) {
6432 if (y + intel_crtc->cursor_height < 0)
6433 base = 0;
6434
6435 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
6436 y = -y;
6437 }
6438 pos |= y << CURSOR_Y_SHIFT;
6439
6440 visible = base != 0;
6441 if (!visible && !intel_crtc->cursor_visible)
6442 return;
6443
6444 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
6445 I915_WRITE(CURPOS_IVB(pipe), pos);
6446 ivb_update_cursor(crtc, base);
6447 } else {
6448 I915_WRITE(CURPOS(pipe), pos);
6449 if (IS_845G(dev) || IS_I865G(dev))
6450 i845_update_cursor(crtc, base);
6451 else
6452 i9xx_update_cursor(crtc, base);
6453 }
6454 }
6455
6456 static int intel_crtc_cursor_set(struct drm_crtc *crtc,
6457 struct drm_file *file,
6458 uint32_t handle,
6459 uint32_t width, uint32_t height)
6460 {
6461 struct drm_device *dev = crtc->dev;
6462 struct drm_i915_private *dev_priv = dev->dev_private;
6463 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6464 struct drm_i915_gem_object *obj;
6465 uint32_t addr;
6466 int ret;
6467
6468 /* if we want to turn off the cursor ignore width and height */
6469 if (!handle) {
6470 DRM_DEBUG_KMS("cursor off\n");
6471 addr = 0;
6472 obj = NULL;
6473 mutex_lock(&dev->struct_mutex);
6474 goto finish;
6475 }
6476
6477 /* Currently we only support 64x64 cursors */
6478 if (width != 64 || height != 64) {
6479 DRM_ERROR("we currently only support 64x64 cursors\n");
6480 return -EINVAL;
6481 }
6482
6483 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
6484 if (&obj->base == NULL)
6485 return -ENOENT;
6486
6487 if (obj->base.size < width * height * 4) {
6488 DRM_ERROR("buffer is to small\n");
6489 ret = -ENOMEM;
6490 goto fail;
6491 }
6492
6493 /* we only need to pin inside GTT if cursor is non-phy */
6494 mutex_lock(&dev->struct_mutex);
6495 if (!dev_priv->info->cursor_needs_physical) {
6496 unsigned alignment;
6497
6498 if (obj->tiling_mode) {
6499 DRM_ERROR("cursor cannot be tiled\n");
6500 ret = -EINVAL;
6501 goto fail_locked;
6502 }
6503
6504 /* Note that the w/a also requires 2 PTE of padding following
6505 * the bo. We currently fill all unused PTE with the shadow
6506 * page and so we should always have valid PTE following the
6507 * cursor preventing the VT-d warning.
6508 */
6509 alignment = 0;
6510 if (need_vtd_wa(dev))
6511 alignment = 64*1024;
6512
6513 ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
6514 if (ret) {
6515 DRM_ERROR("failed to move cursor bo into the GTT\n");
6516 goto fail_locked;
6517 }
6518
6519 ret = i915_gem_object_put_fence(obj);
6520 if (ret) {
6521 DRM_ERROR("failed to release fence for cursor");
6522 goto fail_unpin;
6523 }
6524
6525 addr = obj->gtt_offset;
6526 } else {
6527 int align = IS_I830(dev) ? 16 * 1024 : 256;
6528 ret = i915_gem_attach_phys_object(dev, obj,
6529 (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
6530 align);
6531 if (ret) {
6532 DRM_ERROR("failed to attach phys object\n");
6533 goto fail_locked;
6534 }
6535 addr = obj->phys_obj->handle->busaddr;
6536 }
6537
6538 if (IS_GEN2(dev))
6539 I915_WRITE(CURSIZE, (height << 12) | width);
6540
6541 finish:
6542 if (intel_crtc->cursor_bo) {
6543 if (dev_priv->info->cursor_needs_physical) {
6544 if (intel_crtc->cursor_bo != obj)
6545 i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
6546 } else
6547 i915_gem_object_unpin(intel_crtc->cursor_bo);
6548 drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
6549 }
6550
6551 mutex_unlock(&dev->struct_mutex);
6552
6553 intel_crtc->cursor_addr = addr;
6554 intel_crtc->cursor_bo = obj;
6555 intel_crtc->cursor_width = width;
6556 intel_crtc->cursor_height = height;
6557
6558 intel_crtc_update_cursor(crtc, true);
6559
6560 return 0;
6561 fail_unpin:
6562 i915_gem_object_unpin(obj);
6563 fail_locked:
6564 mutex_unlock(&dev->struct_mutex);
6565 fail:
6566 drm_gem_object_unreference_unlocked(&obj->base);
6567 return ret;
6568 }
6569
6570 static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
6571 {
6572 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6573
6574 intel_crtc->cursor_x = x;
6575 intel_crtc->cursor_y = y;
6576
6577 intel_crtc_update_cursor(crtc, true);
6578
6579 return 0;
6580 }
6581
6582 /** Sets the color ramps on behalf of RandR */
6583 void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
6584 u16 blue, int regno)
6585 {
6586 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6587
6588 intel_crtc->lut_r[regno] = red >> 8;
6589 intel_crtc->lut_g[regno] = green >> 8;
6590 intel_crtc->lut_b[regno] = blue >> 8;
6591 }
6592
6593 void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
6594 u16 *blue, int regno)
6595 {
6596 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6597
6598 *red = intel_crtc->lut_r[regno] << 8;
6599 *green = intel_crtc->lut_g[regno] << 8;
6600 *blue = intel_crtc->lut_b[regno] << 8;
6601 }
6602
6603 static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
6604 u16 *blue, uint32_t start, uint32_t size)
6605 {
6606 int end = (start + size > 256) ? 256 : start + size, i;
6607 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6608
6609 for (i = start; i < end; i++) {
6610 intel_crtc->lut_r[i] = red[i] >> 8;
6611 intel_crtc->lut_g[i] = green[i] >> 8;
6612 intel_crtc->lut_b[i] = blue[i] >> 8;
6613 }
6614
6615 intel_crtc_load_lut(crtc);
6616 }
6617
6618 /* VESA 640x480x72Hz mode to set on the pipe */
6619 static struct drm_display_mode load_detect_mode = {
6620 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
6621 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
6622 };
6623
6624 static struct drm_framebuffer *
6625 intel_framebuffer_create(struct drm_device *dev,
6626 struct drm_mode_fb_cmd2 *mode_cmd,
6627 struct drm_i915_gem_object *obj)
6628 {
6629 struct intel_framebuffer *intel_fb;
6630 int ret;
6631
6632 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
6633 if (!intel_fb) {
6634 drm_gem_object_unreference_unlocked(&obj->base);
6635 return ERR_PTR(-ENOMEM);
6636 }
6637
6638 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
6639 if (ret) {
6640 drm_gem_object_unreference_unlocked(&obj->base);
6641 kfree(intel_fb);
6642 return ERR_PTR(ret);
6643 }
6644
6645 return &intel_fb->base;
6646 }
6647
6648 static u32
6649 intel_framebuffer_pitch_for_width(int width, int bpp)
6650 {
6651 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
6652 return ALIGN(pitch, 64);
6653 }
6654
6655 static u32
6656 intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
6657 {
6658 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
6659 return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
6660 }
6661
6662 static struct drm_framebuffer *
6663 intel_framebuffer_create_for_mode(struct drm_device *dev,
6664 struct drm_display_mode *mode,
6665 int depth, int bpp)
6666 {
6667 struct drm_i915_gem_object *obj;
6668 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
6669
6670 obj = i915_gem_alloc_object(dev,
6671 intel_framebuffer_size_for_mode(mode, bpp));
6672 if (obj == NULL)
6673 return ERR_PTR(-ENOMEM);
6674
6675 mode_cmd.width = mode->hdisplay;
6676 mode_cmd.height = mode->vdisplay;
6677 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
6678 bpp);
6679 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
6680
6681 return intel_framebuffer_create(dev, &mode_cmd, obj);
6682 }
6683
6684 static struct drm_framebuffer *
6685 mode_fits_in_fbdev(struct drm_device *dev,
6686 struct drm_display_mode *mode)
6687 {
6688 struct drm_i915_private *dev_priv = dev->dev_private;
6689 struct drm_i915_gem_object *obj;
6690 struct drm_framebuffer *fb;
6691
6692 if (dev_priv->fbdev == NULL)
6693 return NULL;
6694
6695 obj = dev_priv->fbdev->ifb.obj;
6696 if (obj == NULL)
6697 return NULL;
6698
6699 fb = &dev_priv->fbdev->ifb.base;
6700 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
6701 fb->bits_per_pixel))
6702 return NULL;
6703
6704 if (obj->base.size < mode->vdisplay * fb->pitches[0])
6705 return NULL;
6706
6707 return fb;
6708 }
6709
6710 bool intel_get_load_detect_pipe(struct drm_connector *connector,
6711 struct drm_display_mode *mode,
6712 struct intel_load_detect_pipe *old)
6713 {
6714 struct intel_crtc *intel_crtc;
6715 struct intel_encoder *intel_encoder =
6716 intel_attached_encoder(connector);
6717 struct drm_crtc *possible_crtc;
6718 struct drm_encoder *encoder = &intel_encoder->base;
6719 struct drm_crtc *crtc = NULL;
6720 struct drm_device *dev = encoder->dev;
6721 struct drm_framebuffer *fb;
6722 int i = -1;
6723
6724 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6725 connector->base.id, drm_get_connector_name(connector),
6726 encoder->base.id, drm_get_encoder_name(encoder));
6727
6728 /*
6729 * Algorithm gets a little messy:
6730 *
6731 * - if the connector already has an assigned crtc, use it (but make
6732 * sure it's on first)
6733 *
6734 * - try to find the first unused crtc that can drive this connector,
6735 * and use that if we find one
6736 */
6737
6738 /* See if we already have a CRTC for this connector */
6739 if (encoder->crtc) {
6740 crtc = encoder->crtc;
6741
6742 mutex_lock(&crtc->mutex);
6743
6744 old->dpms_mode = connector->dpms;
6745 old->load_detect_temp = false;
6746
6747 /* Make sure the crtc and connector are running */
6748 if (connector->dpms != DRM_MODE_DPMS_ON)
6749 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
6750
6751 return true;
6752 }
6753
6754 /* Find an unused one (if possible) */
6755 list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
6756 i++;
6757 if (!(encoder->possible_crtcs & (1 << i)))
6758 continue;
6759 if (!possible_crtc->enabled) {
6760 crtc = possible_crtc;
6761 break;
6762 }
6763 }
6764
6765 /*
6766 * If we didn't find an unused CRTC, don't use any.
6767 */
6768 if (!crtc) {
6769 DRM_DEBUG_KMS("no pipe available for load-detect\n");
6770 return false;
6771 }
6772
6773 mutex_lock(&crtc->mutex);
6774 intel_encoder->new_crtc = to_intel_crtc(crtc);
6775 to_intel_connector(connector)->new_encoder = intel_encoder;
6776
6777 intel_crtc = to_intel_crtc(crtc);
6778 old->dpms_mode = connector->dpms;
6779 old->load_detect_temp = true;
6780 old->release_fb = NULL;
6781
6782 if (!mode)
6783 mode = &load_detect_mode;
6784
6785 /* We need a framebuffer large enough to accommodate all accesses
6786 * that the plane may generate whilst we perform load detection.
6787 * We can not rely on the fbcon either being present (we get called
6788 * during its initialisation to detect all boot displays, or it may
6789 * not even exist) or that it is large enough to satisfy the
6790 * requested mode.
6791 */
6792 fb = mode_fits_in_fbdev(dev, mode);
6793 if (fb == NULL) {
6794 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
6795 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
6796 old->release_fb = fb;
6797 } else
6798 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
6799 if (IS_ERR(fb)) {
6800 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
6801 mutex_unlock(&crtc->mutex);
6802 return false;
6803 }
6804
6805 if (intel_set_mode(crtc, mode, 0, 0, fb)) {
6806 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
6807 if (old->release_fb)
6808 old->release_fb->funcs->destroy(old->release_fb);
6809 mutex_unlock(&crtc->mutex);
6810 return false;
6811 }
6812
6813 /* let the connector get through one full cycle before testing */
6814 intel_wait_for_vblank(dev, intel_crtc->pipe);
6815 return true;
6816 }
6817
6818 void intel_release_load_detect_pipe(struct drm_connector *connector,
6819 struct intel_load_detect_pipe *old)
6820 {
6821 struct intel_encoder *intel_encoder =
6822 intel_attached_encoder(connector);
6823 struct drm_encoder *encoder = &intel_encoder->base;
6824 struct drm_crtc *crtc = encoder->crtc;
6825
6826 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6827 connector->base.id, drm_get_connector_name(connector),
6828 encoder->base.id, drm_get_encoder_name(encoder));
6829
6830 if (old->load_detect_temp) {
6831 to_intel_connector(connector)->new_encoder = NULL;
6832 intel_encoder->new_crtc = NULL;
6833 intel_set_mode(crtc, NULL, 0, 0, NULL);
6834
6835 if (old->release_fb) {
6836 drm_framebuffer_unregister_private(old->release_fb);
6837 drm_framebuffer_unreference(old->release_fb);
6838 }
6839
6840 mutex_unlock(&crtc->mutex);
6841 return;
6842 }
6843
6844 /* Switch crtc and encoder back off if necessary */
6845 if (old->dpms_mode != DRM_MODE_DPMS_ON)
6846 connector->funcs->dpms(connector, old->dpms_mode);
6847
6848 mutex_unlock(&crtc->mutex);
6849 }
6850
6851 /* Returns the clock of the currently programmed mode of the given pipe. */
6852 static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
6853 {
6854 struct drm_i915_private *dev_priv = dev->dev_private;
6855 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6856 int pipe = intel_crtc->pipe;
6857 u32 dpll = I915_READ(DPLL(pipe));
6858 u32 fp;
6859 intel_clock_t clock;
6860
6861 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
6862 fp = I915_READ(FP0(pipe));
6863 else
6864 fp = I915_READ(FP1(pipe));
6865
6866 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
6867 if (IS_PINEVIEW(dev)) {
6868 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
6869 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
6870 } else {
6871 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
6872 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
6873 }
6874
6875 if (!IS_GEN2(dev)) {
6876 if (IS_PINEVIEW(dev))
6877 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
6878 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
6879 else
6880 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
6881 DPLL_FPA01_P1_POST_DIV_SHIFT);
6882
6883 switch (dpll & DPLL_MODE_MASK) {
6884 case DPLLB_MODE_DAC_SERIAL:
6885 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
6886 5 : 10;
6887 break;
6888 case DPLLB_MODE_LVDS:
6889 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
6890 7 : 14;
6891 break;
6892 default:
6893 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
6894 "mode\n", (int)(dpll & DPLL_MODE_MASK));
6895 return 0;
6896 }
6897
6898 /* XXX: Handle the 100Mhz refclk */
6899 intel_clock(dev, 96000, &clock);
6900 } else {
6901 bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
6902
6903 if (is_lvds) {
6904 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
6905 DPLL_FPA01_P1_POST_DIV_SHIFT);
6906 clock.p2 = 14;
6907
6908 if ((dpll & PLL_REF_INPUT_MASK) ==
6909 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
6910 /* XXX: might not be 66MHz */
6911 intel_clock(dev, 66000, &clock);
6912 } else
6913 intel_clock(dev, 48000, &clock);
6914 } else {
6915 if (dpll & PLL_P1_DIVIDE_BY_TWO)
6916 clock.p1 = 2;
6917 else {
6918 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
6919 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
6920 }
6921 if (dpll & PLL_P2_DIVIDE_BY_4)
6922 clock.p2 = 4;
6923 else
6924 clock.p2 = 2;
6925
6926 intel_clock(dev, 48000, &clock);
6927 }
6928 }
6929
6930 /* XXX: It would be nice to validate the clocks, but we can't reuse
6931 * i830PllIsValid() because it relies on the xf86_config connector
6932 * configuration being accurate, which it isn't necessarily.
6933 */
6934
6935 return clock.dot;
6936 }
6937
6938 /** Returns the currently programmed mode of the given pipe. */
6939 struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
6940 struct drm_crtc *crtc)
6941 {
6942 struct drm_i915_private *dev_priv = dev->dev_private;
6943 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6944 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
6945 struct drm_display_mode *mode;
6946 int htot = I915_READ(HTOTAL(cpu_transcoder));
6947 int hsync = I915_READ(HSYNC(cpu_transcoder));
6948 int vtot = I915_READ(VTOTAL(cpu_transcoder));
6949 int vsync = I915_READ(VSYNC(cpu_transcoder));
6950
6951 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
6952 if (!mode)
6953 return NULL;
6954
6955 mode->clock = intel_crtc_clock_get(dev, crtc);
6956 mode->hdisplay = (htot & 0xffff) + 1;
6957 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
6958 mode->hsync_start = (hsync & 0xffff) + 1;
6959 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
6960 mode->vdisplay = (vtot & 0xffff) + 1;
6961 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
6962 mode->vsync_start = (vsync & 0xffff) + 1;
6963 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
6964
6965 drm_mode_set_name(mode);
6966
6967 return mode;
6968 }
6969
6970 static void intel_increase_pllclock(struct drm_crtc *crtc)
6971 {
6972 struct drm_device *dev = crtc->dev;
6973 drm_i915_private_t *dev_priv = dev->dev_private;
6974 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6975 int pipe = intel_crtc->pipe;
6976 int dpll_reg = DPLL(pipe);
6977 int dpll;
6978
6979 if (HAS_PCH_SPLIT(dev))
6980 return;
6981
6982 if (!dev_priv->lvds_downclock_avail)
6983 return;
6984
6985 dpll = I915_READ(dpll_reg);
6986 if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
6987 DRM_DEBUG_DRIVER("upclocking LVDS\n");
6988
6989 assert_panel_unlocked(dev_priv, pipe);
6990
6991 dpll &= ~DISPLAY_RATE_SELECT_FPA1;
6992 I915_WRITE(dpll_reg, dpll);
6993 intel_wait_for_vblank(dev, pipe);
6994
6995 dpll = I915_READ(dpll_reg);
6996 if (dpll & DISPLAY_RATE_SELECT_FPA1)
6997 DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
6998 }
6999 }
7000
7001 static void intel_decrease_pllclock(struct drm_crtc *crtc)
7002 {
7003 struct drm_device *dev = crtc->dev;
7004 drm_i915_private_t *dev_priv = dev->dev_private;
7005 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7006
7007 if (HAS_PCH_SPLIT(dev))
7008 return;
7009
7010 if (!dev_priv->lvds_downclock_avail)
7011 return;
7012
7013 /*
7014 * Since this is called by a timer, we should never get here in
7015 * the manual case.
7016 */
7017 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
7018 int pipe = intel_crtc->pipe;
7019 int dpll_reg = DPLL(pipe);
7020 int dpll;
7021
7022 DRM_DEBUG_DRIVER("downclocking LVDS\n");
7023
7024 assert_panel_unlocked(dev_priv, pipe);
7025
7026 dpll = I915_READ(dpll_reg);
7027 dpll |= DISPLAY_RATE_SELECT_FPA1;
7028 I915_WRITE(dpll_reg, dpll);
7029 intel_wait_for_vblank(dev, pipe);
7030 dpll = I915_READ(dpll_reg);
7031 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
7032 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
7033 }
7034
7035 }
7036
7037 void intel_mark_busy(struct drm_device *dev)
7038 {
7039 i915_update_gfx_val(dev->dev_private);
7040 }
7041
7042 void intel_mark_idle(struct drm_device *dev)
7043 {
7044 struct drm_crtc *crtc;
7045
7046 if (!i915_powersave)
7047 return;
7048
7049 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
7050 if (!crtc->fb)
7051 continue;
7052
7053 intel_decrease_pllclock(crtc);
7054 }
7055 }
7056
7057 void intel_mark_fb_busy(struct drm_i915_gem_object *obj)
7058 {
7059 struct drm_device *dev = obj->base.dev;
7060 struct drm_crtc *crtc;
7061
7062 if (!i915_powersave)
7063 return;
7064
7065 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
7066 if (!crtc->fb)
7067 continue;
7068
7069 if (to_intel_framebuffer(crtc->fb)->obj == obj)
7070 intel_increase_pllclock(crtc);
7071 }
7072 }
7073
7074 static void intel_crtc_destroy(struct drm_crtc *crtc)
7075 {
7076 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7077 struct drm_device *dev = crtc->dev;
7078 struct intel_unpin_work *work;
7079 unsigned long flags;
7080
7081 spin_lock_irqsave(&dev->event_lock, flags);
7082 work = intel_crtc->unpin_work;
7083 intel_crtc->unpin_work = NULL;
7084 spin_unlock_irqrestore(&dev->event_lock, flags);
7085
7086 if (work) {
7087 cancel_work_sync(&work->work);
7088 kfree(work);
7089 }
7090
7091 drm_crtc_cleanup(crtc);
7092
7093 kfree(intel_crtc);
7094 }
7095
7096 static void intel_unpin_work_fn(struct work_struct *__work)
7097 {
7098 struct intel_unpin_work *work =
7099 container_of(__work, struct intel_unpin_work, work);
7100 struct drm_device *dev = work->crtc->dev;
7101
7102 mutex_lock(&dev->struct_mutex);
7103 intel_unpin_fb_obj(work->old_fb_obj);
7104 drm_gem_object_unreference(&work->pending_flip_obj->base);
7105 drm_gem_object_unreference(&work->old_fb_obj->base);
7106
7107 intel_update_fbc(dev);
7108 mutex_unlock(&dev->struct_mutex);
7109
7110 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
7111 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
7112
7113 kfree(work);
7114 }
7115
7116 static void do_intel_finish_page_flip(struct drm_device *dev,
7117 struct drm_crtc *crtc)
7118 {
7119 drm_i915_private_t *dev_priv = dev->dev_private;
7120 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7121 struct intel_unpin_work *work;
7122 unsigned long flags;
7123
7124 /* Ignore early vblank irqs */
7125 if (intel_crtc == NULL)
7126 return;
7127
7128 spin_lock_irqsave(&dev->event_lock, flags);
7129 work = intel_crtc->unpin_work;
7130
7131 /* Ensure we don't miss a work->pending update ... */
7132 smp_rmb();
7133
7134 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
7135 spin_unlock_irqrestore(&dev->event_lock, flags);
7136 return;
7137 }
7138
7139 /* and that the unpin work is consistent wrt ->pending. */
7140 smp_rmb();
7141
7142 intel_crtc->unpin_work = NULL;
7143
7144 if (work->event)
7145 drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
7146
7147 drm_vblank_put(dev, intel_crtc->pipe);
7148
7149 spin_unlock_irqrestore(&dev->event_lock, flags);
7150
7151 wake_up_all(&dev_priv->pending_flip_queue);
7152
7153 queue_work(dev_priv->wq, &work->work);
7154
7155 trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
7156 }
7157
7158 void intel_finish_page_flip(struct drm_device *dev, int pipe)
7159 {
7160 drm_i915_private_t *dev_priv = dev->dev_private;
7161 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
7162
7163 do_intel_finish_page_flip(dev, crtc);
7164 }
7165
7166 void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
7167 {
7168 drm_i915_private_t *dev_priv = dev->dev_private;
7169 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
7170
7171 do_intel_finish_page_flip(dev, crtc);
7172 }
7173
7174 void intel_prepare_page_flip(struct drm_device *dev, int plane)
7175 {
7176 drm_i915_private_t *dev_priv = dev->dev_private;
7177 struct intel_crtc *intel_crtc =
7178 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
7179 unsigned long flags;
7180
7181 /* NB: An MMIO update of the plane base pointer will also
7182 * generate a page-flip completion irq, i.e. every modeset
7183 * is also accompanied by a spurious intel_prepare_page_flip().
7184 */
7185 spin_lock_irqsave(&dev->event_lock, flags);
7186 if (intel_crtc->unpin_work)
7187 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
7188 spin_unlock_irqrestore(&dev->event_lock, flags);
7189 }
7190
7191 inline static void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
7192 {
7193 /* Ensure that the work item is consistent when activating it ... */
7194 smp_wmb();
7195 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
7196 /* and that it is marked active as soon as the irq could fire. */
7197 smp_wmb();
7198 }
7199
7200 static int intel_gen2_queue_flip(struct drm_device *dev,
7201 struct drm_crtc *crtc,
7202 struct drm_framebuffer *fb,
7203 struct drm_i915_gem_object *obj)
7204 {
7205 struct drm_i915_private *dev_priv = dev->dev_private;
7206 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7207 u32 flip_mask;
7208 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
7209 int ret;
7210
7211 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
7212 if (ret)
7213 goto err;
7214
7215 ret = intel_ring_begin(ring, 6);
7216 if (ret)
7217 goto err_unpin;
7218
7219 /* Can't queue multiple flips, so wait for the previous
7220 * one to finish before executing the next.
7221 */
7222 if (intel_crtc->plane)
7223 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
7224 else
7225 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
7226 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
7227 intel_ring_emit(ring, MI_NOOP);
7228 intel_ring_emit(ring, MI_DISPLAY_FLIP |
7229 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7230 intel_ring_emit(ring, fb->pitches[0]);
7231 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
7232 intel_ring_emit(ring, 0); /* aux display base address, unused */
7233
7234 intel_mark_page_flip_active(intel_crtc);
7235 intel_ring_advance(ring);
7236 return 0;
7237
7238 err_unpin:
7239 intel_unpin_fb_obj(obj);
7240 err:
7241 return ret;
7242 }
7243
7244 static int intel_gen3_queue_flip(struct drm_device *dev,
7245 struct drm_crtc *crtc,
7246 struct drm_framebuffer *fb,
7247 struct drm_i915_gem_object *obj)
7248 {
7249 struct drm_i915_private *dev_priv = dev->dev_private;
7250 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7251 u32 flip_mask;
7252 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
7253 int ret;
7254
7255 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
7256 if (ret)
7257 goto err;
7258
7259 ret = intel_ring_begin(ring, 6);
7260 if (ret)
7261 goto err_unpin;
7262
7263 if (intel_crtc->plane)
7264 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
7265 else
7266 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
7267 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
7268 intel_ring_emit(ring, MI_NOOP);
7269 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
7270 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7271 intel_ring_emit(ring, fb->pitches[0]);
7272 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
7273 intel_ring_emit(ring, MI_NOOP);
7274
7275 intel_mark_page_flip_active(intel_crtc);
7276 intel_ring_advance(ring);
7277 return 0;
7278
7279 err_unpin:
7280 intel_unpin_fb_obj(obj);
7281 err:
7282 return ret;
7283 }
7284
7285 static int intel_gen4_queue_flip(struct drm_device *dev,
7286 struct drm_crtc *crtc,
7287 struct drm_framebuffer *fb,
7288 struct drm_i915_gem_object *obj)
7289 {
7290 struct drm_i915_private *dev_priv = dev->dev_private;
7291 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7292 uint32_t pf, pipesrc;
7293 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
7294 int ret;
7295
7296 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
7297 if (ret)
7298 goto err;
7299
7300 ret = intel_ring_begin(ring, 4);
7301 if (ret)
7302 goto err_unpin;
7303
7304 /* i965+ uses the linear or tiled offsets from the
7305 * Display Registers (which do not change across a page-flip)
7306 * so we need only reprogram the base address.
7307 */
7308 intel_ring_emit(ring, MI_DISPLAY_FLIP |
7309 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7310 intel_ring_emit(ring, fb->pitches[0]);
7311 intel_ring_emit(ring,
7312 (obj->gtt_offset + intel_crtc->dspaddr_offset) |
7313 obj->tiling_mode);
7314
7315 /* XXX Enabling the panel-fitter across page-flip is so far
7316 * untested on non-native modes, so ignore it for now.
7317 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
7318 */
7319 pf = 0;
7320 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
7321 intel_ring_emit(ring, pf | pipesrc);
7322
7323 intel_mark_page_flip_active(intel_crtc);
7324 intel_ring_advance(ring);
7325 return 0;
7326
7327 err_unpin:
7328 intel_unpin_fb_obj(obj);
7329 err:
7330 return ret;
7331 }
7332
7333 static int intel_gen6_queue_flip(struct drm_device *dev,
7334 struct drm_crtc *crtc,
7335 struct drm_framebuffer *fb,
7336 struct drm_i915_gem_object *obj)
7337 {
7338 struct drm_i915_private *dev_priv = dev->dev_private;
7339 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7340 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
7341 uint32_t pf, pipesrc;
7342 int ret;
7343
7344 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
7345 if (ret)
7346 goto err;
7347
7348 ret = intel_ring_begin(ring, 4);
7349 if (ret)
7350 goto err_unpin;
7351
7352 intel_ring_emit(ring, MI_DISPLAY_FLIP |
7353 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7354 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
7355 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
7356
7357 /* Contrary to the suggestions in the documentation,
7358 * "Enable Panel Fitter" does not seem to be required when page
7359 * flipping with a non-native mode, and worse causes a normal
7360 * modeset to fail.
7361 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
7362 */
7363 pf = 0;
7364 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
7365 intel_ring_emit(ring, pf | pipesrc);
7366
7367 intel_mark_page_flip_active(intel_crtc);
7368 intel_ring_advance(ring);
7369 return 0;
7370
7371 err_unpin:
7372 intel_unpin_fb_obj(obj);
7373 err:
7374 return ret;
7375 }
7376
7377 /*
7378 * On gen7 we currently use the blit ring because (in early silicon at least)
7379 * the render ring doesn't give us interrpts for page flip completion, which
7380 * means clients will hang after the first flip is queued. Fortunately the
7381 * blit ring generates interrupts properly, so use it instead.
7382 */
7383 static int intel_gen7_queue_flip(struct drm_device *dev,
7384 struct drm_crtc *crtc,
7385 struct drm_framebuffer *fb,
7386 struct drm_i915_gem_object *obj)
7387 {
7388 struct drm_i915_private *dev_priv = dev->dev_private;
7389 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7390 struct intel_ring_buffer *ring = &dev_priv->ring[BCS];
7391 uint32_t plane_bit = 0;
7392 int ret;
7393
7394 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
7395 if (ret)
7396 goto err;
7397
7398 switch(intel_crtc->plane) {
7399 case PLANE_A:
7400 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
7401 break;
7402 case PLANE_B:
7403 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
7404 break;
7405 case PLANE_C:
7406 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
7407 break;
7408 default:
7409 WARN_ONCE(1, "unknown plane in flip command\n");
7410 ret = -ENODEV;
7411 goto err_unpin;
7412 }
7413
7414 ret = intel_ring_begin(ring, 4);
7415 if (ret)
7416 goto err_unpin;
7417
7418 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
7419 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
7420 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
7421 intel_ring_emit(ring, (MI_NOOP));
7422
7423 intel_mark_page_flip_active(intel_crtc);
7424 intel_ring_advance(ring);
7425 return 0;
7426
7427 err_unpin:
7428 intel_unpin_fb_obj(obj);
7429 err:
7430 return ret;
7431 }
7432
7433 static int intel_default_queue_flip(struct drm_device *dev,
7434 struct drm_crtc *crtc,
7435 struct drm_framebuffer *fb,
7436 struct drm_i915_gem_object *obj)
7437 {
7438 return -ENODEV;
7439 }
7440
7441 static int intel_crtc_page_flip(struct drm_crtc *crtc,
7442 struct drm_framebuffer *fb,
7443 struct drm_pending_vblank_event *event)
7444 {
7445 struct drm_device *dev = crtc->dev;
7446 struct drm_i915_private *dev_priv = dev->dev_private;
7447 struct drm_framebuffer *old_fb = crtc->fb;
7448 struct drm_i915_gem_object *obj = to_intel_framebuffer(fb)->obj;
7449 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7450 struct intel_unpin_work *work;
7451 unsigned long flags;
7452 int ret;
7453
7454 /* Can't change pixel format via MI display flips. */
7455 if (fb->pixel_format != crtc->fb->pixel_format)
7456 return -EINVAL;
7457
7458 /*
7459 * TILEOFF/LINOFF registers can't be changed via MI display flips.
7460 * Note that pitch changes could also affect these register.
7461 */
7462 if (INTEL_INFO(dev)->gen > 3 &&
7463 (fb->offsets[0] != crtc->fb->offsets[0] ||
7464 fb->pitches[0] != crtc->fb->pitches[0]))
7465 return -EINVAL;
7466
7467 work = kzalloc(sizeof *work, GFP_KERNEL);
7468 if (work == NULL)
7469 return -ENOMEM;
7470
7471 work->event = event;
7472 work->crtc = crtc;
7473 work->old_fb_obj = to_intel_framebuffer(old_fb)->obj;
7474 INIT_WORK(&work->work, intel_unpin_work_fn);
7475
7476 ret = drm_vblank_get(dev, intel_crtc->pipe);
7477 if (ret)
7478 goto free_work;
7479
7480 /* We borrow the event spin lock for protecting unpin_work */
7481 spin_lock_irqsave(&dev->event_lock, flags);
7482 if (intel_crtc->unpin_work) {
7483 spin_unlock_irqrestore(&dev->event_lock, flags);
7484 kfree(work);
7485 drm_vblank_put(dev, intel_crtc->pipe);
7486
7487 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
7488 return -EBUSY;
7489 }
7490 intel_crtc->unpin_work = work;
7491 spin_unlock_irqrestore(&dev->event_lock, flags);
7492
7493 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
7494 flush_workqueue(dev_priv->wq);
7495
7496 ret = i915_mutex_lock_interruptible(dev);
7497 if (ret)
7498 goto cleanup;
7499
7500 /* Reference the objects for the scheduled work. */
7501 drm_gem_object_reference(&work->old_fb_obj->base);
7502 drm_gem_object_reference(&obj->base);
7503
7504 crtc->fb = fb;
7505
7506 work->pending_flip_obj = obj;
7507
7508 work->enable_stall_check = true;
7509
7510 atomic_inc(&intel_crtc->unpin_work_count);
7511 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
7512
7513 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj);
7514 if (ret)
7515 goto cleanup_pending;
7516
7517 intel_disable_fbc(dev);
7518 intel_mark_fb_busy(obj);
7519 mutex_unlock(&dev->struct_mutex);
7520
7521 trace_i915_flip_request(intel_crtc->plane, obj);
7522
7523 return 0;
7524
7525 cleanup_pending:
7526 atomic_dec(&intel_crtc->unpin_work_count);
7527 crtc->fb = old_fb;
7528 drm_gem_object_unreference(&work->old_fb_obj->base);
7529 drm_gem_object_unreference(&obj->base);
7530 mutex_unlock(&dev->struct_mutex);
7531
7532 cleanup:
7533 spin_lock_irqsave(&dev->event_lock, flags);
7534 intel_crtc->unpin_work = NULL;
7535 spin_unlock_irqrestore(&dev->event_lock, flags);
7536
7537 drm_vblank_put(dev, intel_crtc->pipe);
7538 free_work:
7539 kfree(work);
7540
7541 return ret;
7542 }
7543
7544 static struct drm_crtc_helper_funcs intel_helper_funcs = {
7545 .mode_set_base_atomic = intel_pipe_set_base_atomic,
7546 .load_lut = intel_crtc_load_lut,
7547 };
7548
7549 bool intel_encoder_check_is_cloned(struct intel_encoder *encoder)
7550 {
7551 struct intel_encoder *other_encoder;
7552 struct drm_crtc *crtc = &encoder->new_crtc->base;
7553
7554 if (WARN_ON(!crtc))
7555 return false;
7556
7557 list_for_each_entry(other_encoder,
7558 &crtc->dev->mode_config.encoder_list,
7559 base.head) {
7560
7561 if (&other_encoder->new_crtc->base != crtc ||
7562 encoder == other_encoder)
7563 continue;
7564 else
7565 return true;
7566 }
7567
7568 return false;
7569 }
7570
7571 static bool intel_encoder_crtc_ok(struct drm_encoder *encoder,
7572 struct drm_crtc *crtc)
7573 {
7574 struct drm_device *dev;
7575 struct drm_crtc *tmp;
7576 int crtc_mask = 1;
7577
7578 WARN(!crtc, "checking null crtc?\n");
7579
7580 dev = crtc->dev;
7581
7582 list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
7583 if (tmp == crtc)
7584 break;
7585 crtc_mask <<= 1;
7586 }
7587
7588 if (encoder->possible_crtcs & crtc_mask)
7589 return true;
7590 return false;
7591 }
7592
7593 /**
7594 * intel_modeset_update_staged_output_state
7595 *
7596 * Updates the staged output configuration state, e.g. after we've read out the
7597 * current hw state.
7598 */
7599 static void intel_modeset_update_staged_output_state(struct drm_device *dev)
7600 {
7601 struct intel_encoder *encoder;
7602 struct intel_connector *connector;
7603
7604 list_for_each_entry(connector, &dev->mode_config.connector_list,
7605 base.head) {
7606 connector->new_encoder =
7607 to_intel_encoder(connector->base.encoder);
7608 }
7609
7610 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7611 base.head) {
7612 encoder->new_crtc =
7613 to_intel_crtc(encoder->base.crtc);
7614 }
7615 }
7616
7617 /**
7618 * intel_modeset_commit_output_state
7619 *
7620 * This function copies the stage display pipe configuration to the real one.
7621 */
7622 static void intel_modeset_commit_output_state(struct drm_device *dev)
7623 {
7624 struct intel_encoder *encoder;
7625 struct intel_connector *connector;
7626
7627 list_for_each_entry(connector, &dev->mode_config.connector_list,
7628 base.head) {
7629 connector->base.encoder = &connector->new_encoder->base;
7630 }
7631
7632 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7633 base.head) {
7634 encoder->base.crtc = &encoder->new_crtc->base;
7635 }
7636 }
7637
7638 static int
7639 pipe_config_set_bpp(struct drm_crtc *crtc,
7640 struct drm_framebuffer *fb,
7641 struct intel_crtc_config *pipe_config)
7642 {
7643 struct drm_device *dev = crtc->dev;
7644 struct drm_connector *connector;
7645 int bpp;
7646
7647 switch (fb->pixel_format) {
7648 case DRM_FORMAT_C8:
7649 bpp = 8*3; /* since we go through a colormap */
7650 break;
7651 case DRM_FORMAT_XRGB1555:
7652 case DRM_FORMAT_ARGB1555:
7653 /* checked in intel_framebuffer_init already */
7654 if (WARN_ON(INTEL_INFO(dev)->gen > 3))
7655 return -EINVAL;
7656 case DRM_FORMAT_RGB565:
7657 bpp = 6*3; /* min is 18bpp */
7658 break;
7659 case DRM_FORMAT_XBGR8888:
7660 case DRM_FORMAT_ABGR8888:
7661 /* checked in intel_framebuffer_init already */
7662 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
7663 return -EINVAL;
7664 case DRM_FORMAT_XRGB8888:
7665 case DRM_FORMAT_ARGB8888:
7666 bpp = 8*3;
7667 break;
7668 case DRM_FORMAT_XRGB2101010:
7669 case DRM_FORMAT_ARGB2101010:
7670 case DRM_FORMAT_XBGR2101010:
7671 case DRM_FORMAT_ABGR2101010:
7672 /* checked in intel_framebuffer_init already */
7673 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
7674 return -EINVAL;
7675 bpp = 10*3;
7676 break;
7677 /* TODO: gen4+ supports 16 bpc floating point, too. */
7678 default:
7679 DRM_DEBUG_KMS("unsupported depth\n");
7680 return -EINVAL;
7681 }
7682
7683 pipe_config->pipe_bpp = bpp;
7684
7685 /* Clamp display bpp to EDID value */
7686 list_for_each_entry(connector, &dev->mode_config.connector_list,
7687 head) {
7688 if (connector->encoder && connector->encoder->crtc != crtc)
7689 continue;
7690
7691 /* Don't use an invalid EDID bpc value */
7692 if (connector->display_info.bpc &&
7693 connector->display_info.bpc * 3 < bpp) {
7694 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
7695 bpp, connector->display_info.bpc*3);
7696 pipe_config->pipe_bpp = connector->display_info.bpc*3;
7697 }
7698
7699 /* Clamp bpp to 8 on screens without EDID 1.4 */
7700 if (connector->display_info.bpc == 0 && bpp > 24) {
7701 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
7702 bpp);
7703 pipe_config->pipe_bpp = 24;
7704 }
7705 }
7706
7707 return bpp;
7708 }
7709
7710 static struct intel_crtc_config *
7711 intel_modeset_pipe_config(struct drm_crtc *crtc,
7712 struct drm_framebuffer *fb,
7713 struct drm_display_mode *mode)
7714 {
7715 struct drm_device *dev = crtc->dev;
7716 struct drm_encoder_helper_funcs *encoder_funcs;
7717 struct intel_encoder *encoder;
7718 struct intel_crtc_config *pipe_config;
7719 int plane_bpp;
7720
7721 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
7722 if (!pipe_config)
7723 return ERR_PTR(-ENOMEM);
7724
7725 drm_mode_copy(&pipe_config->adjusted_mode, mode);
7726 drm_mode_copy(&pipe_config->requested_mode, mode);
7727
7728 plane_bpp = pipe_config_set_bpp(crtc, fb, pipe_config);
7729 if (plane_bpp < 0)
7730 goto fail;
7731
7732 /* Pass our mode to the connectors and the CRTC to give them a chance to
7733 * adjust it according to limitations or connector properties, and also
7734 * a chance to reject the mode entirely.
7735 */
7736 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7737 base.head) {
7738
7739 if (&encoder->new_crtc->base != crtc)
7740 continue;
7741
7742 if (encoder->compute_config) {
7743 if (!(encoder->compute_config(encoder, pipe_config))) {
7744 DRM_DEBUG_KMS("Encoder config failure\n");
7745 goto fail;
7746 }
7747
7748 continue;
7749 }
7750
7751 encoder_funcs = encoder->base.helper_private;
7752 if (!(encoder_funcs->mode_fixup(&encoder->base,
7753 &pipe_config->requested_mode,
7754 &pipe_config->adjusted_mode))) {
7755 DRM_DEBUG_KMS("Encoder fixup failed\n");
7756 goto fail;
7757 }
7758 }
7759
7760 if (!(intel_crtc_compute_config(crtc, pipe_config))) {
7761 DRM_DEBUG_KMS("CRTC fixup failed\n");
7762 goto fail;
7763 }
7764 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
7765
7766 pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
7767 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
7768 plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
7769
7770 return pipe_config;
7771 fail:
7772 kfree(pipe_config);
7773 return ERR_PTR(-EINVAL);
7774 }
7775
7776 /* Computes which crtcs are affected and sets the relevant bits in the mask. For
7777 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
7778 static void
7779 intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
7780 unsigned *prepare_pipes, unsigned *disable_pipes)
7781 {
7782 struct intel_crtc *intel_crtc;
7783 struct drm_device *dev = crtc->dev;
7784 struct intel_encoder *encoder;
7785 struct intel_connector *connector;
7786 struct drm_crtc *tmp_crtc;
7787
7788 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
7789
7790 /* Check which crtcs have changed outputs connected to them, these need
7791 * to be part of the prepare_pipes mask. We don't (yet) support global
7792 * modeset across multiple crtcs, so modeset_pipes will only have one
7793 * bit set at most. */
7794 list_for_each_entry(connector, &dev->mode_config.connector_list,
7795 base.head) {
7796 if (connector->base.encoder == &connector->new_encoder->base)
7797 continue;
7798
7799 if (connector->base.encoder) {
7800 tmp_crtc = connector->base.encoder->crtc;
7801
7802 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
7803 }
7804
7805 if (connector->new_encoder)
7806 *prepare_pipes |=
7807 1 << connector->new_encoder->new_crtc->pipe;
7808 }
7809
7810 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7811 base.head) {
7812 if (encoder->base.crtc == &encoder->new_crtc->base)
7813 continue;
7814
7815 if (encoder->base.crtc) {
7816 tmp_crtc = encoder->base.crtc;
7817
7818 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
7819 }
7820
7821 if (encoder->new_crtc)
7822 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
7823 }
7824
7825 /* Check for any pipes that will be fully disabled ... */
7826 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
7827 base.head) {
7828 bool used = false;
7829
7830 /* Don't try to disable disabled crtcs. */
7831 if (!intel_crtc->base.enabled)
7832 continue;
7833
7834 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7835 base.head) {
7836 if (encoder->new_crtc == intel_crtc)
7837 used = true;
7838 }
7839
7840 if (!used)
7841 *disable_pipes |= 1 << intel_crtc->pipe;
7842 }
7843
7844
7845 /* set_mode is also used to update properties on life display pipes. */
7846 intel_crtc = to_intel_crtc(crtc);
7847 if (crtc->enabled)
7848 *prepare_pipes |= 1 << intel_crtc->pipe;
7849
7850 /*
7851 * For simplicity do a full modeset on any pipe where the output routing
7852 * changed. We could be more clever, but that would require us to be
7853 * more careful with calling the relevant encoder->mode_set functions.
7854 */
7855 if (*prepare_pipes)
7856 *modeset_pipes = *prepare_pipes;
7857
7858 /* ... and mask these out. */
7859 *modeset_pipes &= ~(*disable_pipes);
7860 *prepare_pipes &= ~(*disable_pipes);
7861
7862 /*
7863 * HACK: We don't (yet) fully support global modesets. intel_set_config
7864 * obies this rule, but the modeset restore mode of
7865 * intel_modeset_setup_hw_state does not.
7866 */
7867 *modeset_pipes &= 1 << intel_crtc->pipe;
7868 *prepare_pipes &= 1 << intel_crtc->pipe;
7869
7870 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
7871 *modeset_pipes, *prepare_pipes, *disable_pipes);
7872 }
7873
7874 static bool intel_crtc_in_use(struct drm_crtc *crtc)
7875 {
7876 struct drm_encoder *encoder;
7877 struct drm_device *dev = crtc->dev;
7878
7879 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
7880 if (encoder->crtc == crtc)
7881 return true;
7882
7883 return false;
7884 }
7885
7886 static void
7887 intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
7888 {
7889 struct intel_encoder *intel_encoder;
7890 struct intel_crtc *intel_crtc;
7891 struct drm_connector *connector;
7892
7893 list_for_each_entry(intel_encoder, &dev->mode_config.encoder_list,
7894 base.head) {
7895 if (!intel_encoder->base.crtc)
7896 continue;
7897
7898 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
7899
7900 if (prepare_pipes & (1 << intel_crtc->pipe))
7901 intel_encoder->connectors_active = false;
7902 }
7903
7904 intel_modeset_commit_output_state(dev);
7905
7906 /* Update computed state. */
7907 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
7908 base.head) {
7909 intel_crtc->base.enabled = intel_crtc_in_use(&intel_crtc->base);
7910 }
7911
7912 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
7913 if (!connector->encoder || !connector->encoder->crtc)
7914 continue;
7915
7916 intel_crtc = to_intel_crtc(connector->encoder->crtc);
7917
7918 if (prepare_pipes & (1 << intel_crtc->pipe)) {
7919 struct drm_property *dpms_property =
7920 dev->mode_config.dpms_property;
7921
7922 connector->dpms = DRM_MODE_DPMS_ON;
7923 drm_object_property_set_value(&connector->base,
7924 dpms_property,
7925 DRM_MODE_DPMS_ON);
7926
7927 intel_encoder = to_intel_encoder(connector->encoder);
7928 intel_encoder->connectors_active = true;
7929 }
7930 }
7931
7932 }
7933
7934 #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
7935 list_for_each_entry((intel_crtc), \
7936 &(dev)->mode_config.crtc_list, \
7937 base.head) \
7938 if (mask & (1 <<(intel_crtc)->pipe)) \
7939
7940 static bool
7941 intel_pipe_config_compare(struct intel_crtc_config *current_config,
7942 struct intel_crtc_config *pipe_config)
7943 {
7944 if (current_config->has_pch_encoder != pipe_config->has_pch_encoder) {
7945 DRM_ERROR("mismatch in has_pch_encoder "
7946 "(expected %i, found %i)\n",
7947 current_config->has_pch_encoder,
7948 pipe_config->has_pch_encoder);
7949 return false;
7950 }
7951
7952 return true;
7953 }
7954
7955 void
7956 intel_modeset_check_state(struct drm_device *dev)
7957 {
7958 drm_i915_private_t *dev_priv = dev->dev_private;
7959 struct intel_crtc *crtc;
7960 struct intel_encoder *encoder;
7961 struct intel_connector *connector;
7962 struct intel_crtc_config pipe_config;
7963
7964 list_for_each_entry(connector, &dev->mode_config.connector_list,
7965 base.head) {
7966 /* This also checks the encoder/connector hw state with the
7967 * ->get_hw_state callbacks. */
7968 intel_connector_check_state(connector);
7969
7970 WARN(&connector->new_encoder->base != connector->base.encoder,
7971 "connector's staged encoder doesn't match current encoder\n");
7972 }
7973
7974 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7975 base.head) {
7976 bool enabled = false;
7977 bool active = false;
7978 enum pipe pipe, tracked_pipe;
7979
7980 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
7981 encoder->base.base.id,
7982 drm_get_encoder_name(&encoder->base));
7983
7984 WARN(&encoder->new_crtc->base != encoder->base.crtc,
7985 "encoder's stage crtc doesn't match current crtc\n");
7986 WARN(encoder->connectors_active && !encoder->base.crtc,
7987 "encoder's active_connectors set, but no crtc\n");
7988
7989 list_for_each_entry(connector, &dev->mode_config.connector_list,
7990 base.head) {
7991 if (connector->base.encoder != &encoder->base)
7992 continue;
7993 enabled = true;
7994 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
7995 active = true;
7996 }
7997 WARN(!!encoder->base.crtc != enabled,
7998 "encoder's enabled state mismatch "
7999 "(expected %i, found %i)\n",
8000 !!encoder->base.crtc, enabled);
8001 WARN(active && !encoder->base.crtc,
8002 "active encoder with no crtc\n");
8003
8004 WARN(encoder->connectors_active != active,
8005 "encoder's computed active state doesn't match tracked active state "
8006 "(expected %i, found %i)\n", active, encoder->connectors_active);
8007
8008 active = encoder->get_hw_state(encoder, &pipe);
8009 WARN(active != encoder->connectors_active,
8010 "encoder's hw state doesn't match sw tracking "
8011 "(expected %i, found %i)\n",
8012 encoder->connectors_active, active);
8013
8014 if (!encoder->base.crtc)
8015 continue;
8016
8017 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
8018 WARN(active && pipe != tracked_pipe,
8019 "active encoder's pipe doesn't match"
8020 "(expected %i, found %i)\n",
8021 tracked_pipe, pipe);
8022
8023 }
8024
8025 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
8026 base.head) {
8027 bool enabled = false;
8028 bool active = false;
8029
8030 DRM_DEBUG_KMS("[CRTC:%d]\n",
8031 crtc->base.base.id);
8032
8033 WARN(crtc->active && !crtc->base.enabled,
8034 "active crtc, but not enabled in sw tracking\n");
8035
8036 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8037 base.head) {
8038 if (encoder->base.crtc != &crtc->base)
8039 continue;
8040 enabled = true;
8041 if (encoder->connectors_active)
8042 active = true;
8043 }
8044 WARN(active != crtc->active,
8045 "crtc's computed active state doesn't match tracked active state "
8046 "(expected %i, found %i)\n", active, crtc->active);
8047 WARN(enabled != crtc->base.enabled,
8048 "crtc's computed enabled state doesn't match tracked enabled state "
8049 "(expected %i, found %i)\n", enabled, crtc->base.enabled);
8050
8051 memset(&pipe_config, 0, sizeof(pipe_config));
8052 active = dev_priv->display.get_pipe_config(crtc,
8053 &pipe_config);
8054 WARN(crtc->active != active,
8055 "crtc active state doesn't match with hw state "
8056 "(expected %i, found %i)\n", crtc->active, active);
8057
8058 WARN(active &&
8059 !intel_pipe_config_compare(&crtc->config, &pipe_config),
8060 "pipe state doesn't match!\n");
8061 }
8062 }
8063
8064 static int __intel_set_mode(struct drm_crtc *crtc,
8065 struct drm_display_mode *mode,
8066 int x, int y, struct drm_framebuffer *fb)
8067 {
8068 struct drm_device *dev = crtc->dev;
8069 drm_i915_private_t *dev_priv = dev->dev_private;
8070 struct drm_display_mode *saved_mode, *saved_hwmode;
8071 struct intel_crtc_config *pipe_config = NULL;
8072 struct intel_crtc *intel_crtc;
8073 unsigned disable_pipes, prepare_pipes, modeset_pipes;
8074 int ret = 0;
8075
8076 saved_mode = kmalloc(2 * sizeof(*saved_mode), GFP_KERNEL);
8077 if (!saved_mode)
8078 return -ENOMEM;
8079 saved_hwmode = saved_mode + 1;
8080
8081 intel_modeset_affected_pipes(crtc, &modeset_pipes,
8082 &prepare_pipes, &disable_pipes);
8083
8084 *saved_hwmode = crtc->hwmode;
8085 *saved_mode = crtc->mode;
8086
8087 /* Hack: Because we don't (yet) support global modeset on multiple
8088 * crtcs, we don't keep track of the new mode for more than one crtc.
8089 * Hence simply check whether any bit is set in modeset_pipes in all the
8090 * pieces of code that are not yet converted to deal with mutliple crtcs
8091 * changing their mode at the same time. */
8092 if (modeset_pipes) {
8093 pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
8094 if (IS_ERR(pipe_config)) {
8095 ret = PTR_ERR(pipe_config);
8096 pipe_config = NULL;
8097
8098 goto out;
8099 }
8100 }
8101
8102 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
8103 intel_crtc_disable(&intel_crtc->base);
8104
8105 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
8106 if (intel_crtc->base.enabled)
8107 dev_priv->display.crtc_disable(&intel_crtc->base);
8108 }
8109
8110 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
8111 * to set it here already despite that we pass it down the callchain.
8112 */
8113 if (modeset_pipes) {
8114 enum transcoder tmp = to_intel_crtc(crtc)->config.cpu_transcoder;
8115 crtc->mode = *mode;
8116 /* mode_set/enable/disable functions rely on a correct pipe
8117 * config. */
8118 to_intel_crtc(crtc)->config = *pipe_config;
8119 to_intel_crtc(crtc)->config.cpu_transcoder = tmp;
8120 }
8121
8122 /* Only after disabling all output pipelines that will be changed can we
8123 * update the the output configuration. */
8124 intel_modeset_update_state(dev, prepare_pipes);
8125
8126 if (dev_priv->display.modeset_global_resources)
8127 dev_priv->display.modeset_global_resources(dev);
8128
8129 /* Set up the DPLL and any encoders state that needs to adjust or depend
8130 * on the DPLL.
8131 */
8132 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
8133 ret = intel_crtc_mode_set(&intel_crtc->base,
8134 x, y, fb);
8135 if (ret)
8136 goto done;
8137 }
8138
8139 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
8140 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc)
8141 dev_priv->display.crtc_enable(&intel_crtc->base);
8142
8143 if (modeset_pipes) {
8144 /* Store real post-adjustment hardware mode. */
8145 crtc->hwmode = pipe_config->adjusted_mode;
8146
8147 /* Calculate and store various constants which
8148 * are later needed by vblank and swap-completion
8149 * timestamping. They are derived from true hwmode.
8150 */
8151 drm_calc_timestamping_constants(crtc);
8152 }
8153
8154 /* FIXME: add subpixel order */
8155 done:
8156 if (ret && crtc->enabled) {
8157 crtc->hwmode = *saved_hwmode;
8158 crtc->mode = *saved_mode;
8159 }
8160
8161 out:
8162 kfree(pipe_config);
8163 kfree(saved_mode);
8164 return ret;
8165 }
8166
8167 int intel_set_mode(struct drm_crtc *crtc,
8168 struct drm_display_mode *mode,
8169 int x, int y, struct drm_framebuffer *fb)
8170 {
8171 int ret;
8172
8173 ret = __intel_set_mode(crtc, mode, x, y, fb);
8174
8175 if (ret == 0)
8176 intel_modeset_check_state(crtc->dev);
8177
8178 return ret;
8179 }
8180
8181 void intel_crtc_restore_mode(struct drm_crtc *crtc)
8182 {
8183 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->fb);
8184 }
8185
8186 #undef for_each_intel_crtc_masked
8187
8188 static void intel_set_config_free(struct intel_set_config *config)
8189 {
8190 if (!config)
8191 return;
8192
8193 kfree(config->save_connector_encoders);
8194 kfree(config->save_encoder_crtcs);
8195 kfree(config);
8196 }
8197
8198 static int intel_set_config_save_state(struct drm_device *dev,
8199 struct intel_set_config *config)
8200 {
8201 struct drm_encoder *encoder;
8202 struct drm_connector *connector;
8203 int count;
8204
8205 config->save_encoder_crtcs =
8206 kcalloc(dev->mode_config.num_encoder,
8207 sizeof(struct drm_crtc *), GFP_KERNEL);
8208 if (!config->save_encoder_crtcs)
8209 return -ENOMEM;
8210
8211 config->save_connector_encoders =
8212 kcalloc(dev->mode_config.num_connector,
8213 sizeof(struct drm_encoder *), GFP_KERNEL);
8214 if (!config->save_connector_encoders)
8215 return -ENOMEM;
8216
8217 /* Copy data. Note that driver private data is not affected.
8218 * Should anything bad happen only the expected state is
8219 * restored, not the drivers personal bookkeeping.
8220 */
8221 count = 0;
8222 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
8223 config->save_encoder_crtcs[count++] = encoder->crtc;
8224 }
8225
8226 count = 0;
8227 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
8228 config->save_connector_encoders[count++] = connector->encoder;
8229 }
8230
8231 return 0;
8232 }
8233
8234 static void intel_set_config_restore_state(struct drm_device *dev,
8235 struct intel_set_config *config)
8236 {
8237 struct intel_encoder *encoder;
8238 struct intel_connector *connector;
8239 int count;
8240
8241 count = 0;
8242 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
8243 encoder->new_crtc =
8244 to_intel_crtc(config->save_encoder_crtcs[count++]);
8245 }
8246
8247 count = 0;
8248 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
8249 connector->new_encoder =
8250 to_intel_encoder(config->save_connector_encoders[count++]);
8251 }
8252 }
8253
8254 static void
8255 intel_set_config_compute_mode_changes(struct drm_mode_set *set,
8256 struct intel_set_config *config)
8257 {
8258
8259 /* We should be able to check here if the fb has the same properties
8260 * and then just flip_or_move it */
8261 if (set->crtc->fb != set->fb) {
8262 /* If we have no fb then treat it as a full mode set */
8263 if (set->crtc->fb == NULL) {
8264 DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
8265 config->mode_changed = true;
8266 } else if (set->fb == NULL) {
8267 config->mode_changed = true;
8268 } else if (set->fb->pixel_format !=
8269 set->crtc->fb->pixel_format) {
8270 config->mode_changed = true;
8271 } else
8272 config->fb_changed = true;
8273 }
8274
8275 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
8276 config->fb_changed = true;
8277
8278 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
8279 DRM_DEBUG_KMS("modes are different, full mode set\n");
8280 drm_mode_debug_printmodeline(&set->crtc->mode);
8281 drm_mode_debug_printmodeline(set->mode);
8282 config->mode_changed = true;
8283 }
8284 }
8285
8286 static int
8287 intel_modeset_stage_output_state(struct drm_device *dev,
8288 struct drm_mode_set *set,
8289 struct intel_set_config *config)
8290 {
8291 struct drm_crtc *new_crtc;
8292 struct intel_connector *connector;
8293 struct intel_encoder *encoder;
8294 int count, ro;
8295
8296 /* The upper layers ensure that we either disable a crtc or have a list
8297 * of connectors. For paranoia, double-check this. */
8298 WARN_ON(!set->fb && (set->num_connectors != 0));
8299 WARN_ON(set->fb && (set->num_connectors == 0));
8300
8301 count = 0;
8302 list_for_each_entry(connector, &dev->mode_config.connector_list,
8303 base.head) {
8304 /* Otherwise traverse passed in connector list and get encoders
8305 * for them. */
8306 for (ro = 0; ro < set->num_connectors; ro++) {
8307 if (set->connectors[ro] == &connector->base) {
8308 connector->new_encoder = connector->encoder;
8309 break;
8310 }
8311 }
8312
8313 /* If we disable the crtc, disable all its connectors. Also, if
8314 * the connector is on the changing crtc but not on the new
8315 * connector list, disable it. */
8316 if ((!set->fb || ro == set->num_connectors) &&
8317 connector->base.encoder &&
8318 connector->base.encoder->crtc == set->crtc) {
8319 connector->new_encoder = NULL;
8320
8321 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
8322 connector->base.base.id,
8323 drm_get_connector_name(&connector->base));
8324 }
8325
8326
8327 if (&connector->new_encoder->base != connector->base.encoder) {
8328 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
8329 config->mode_changed = true;
8330 }
8331 }
8332 /* connector->new_encoder is now updated for all connectors. */
8333
8334 /* Update crtc of enabled connectors. */
8335 count = 0;
8336 list_for_each_entry(connector, &dev->mode_config.connector_list,
8337 base.head) {
8338 if (!connector->new_encoder)
8339 continue;
8340
8341 new_crtc = connector->new_encoder->base.crtc;
8342
8343 for (ro = 0; ro < set->num_connectors; ro++) {
8344 if (set->connectors[ro] == &connector->base)
8345 new_crtc = set->crtc;
8346 }
8347
8348 /* Make sure the new CRTC will work with the encoder */
8349 if (!intel_encoder_crtc_ok(&connector->new_encoder->base,
8350 new_crtc)) {
8351 return -EINVAL;
8352 }
8353 connector->encoder->new_crtc = to_intel_crtc(new_crtc);
8354
8355 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
8356 connector->base.base.id,
8357 drm_get_connector_name(&connector->base),
8358 new_crtc->base.id);
8359 }
8360
8361 /* Check for any encoders that needs to be disabled. */
8362 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8363 base.head) {
8364 list_for_each_entry(connector,
8365 &dev->mode_config.connector_list,
8366 base.head) {
8367 if (connector->new_encoder == encoder) {
8368 WARN_ON(!connector->new_encoder->new_crtc);
8369
8370 goto next_encoder;
8371 }
8372 }
8373 encoder->new_crtc = NULL;
8374 next_encoder:
8375 /* Only now check for crtc changes so we don't miss encoders
8376 * that will be disabled. */
8377 if (&encoder->new_crtc->base != encoder->base.crtc) {
8378 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
8379 config->mode_changed = true;
8380 }
8381 }
8382 /* Now we've also updated encoder->new_crtc for all encoders. */
8383
8384 return 0;
8385 }
8386
8387 static int intel_crtc_set_config(struct drm_mode_set *set)
8388 {
8389 struct drm_device *dev;
8390 struct drm_mode_set save_set;
8391 struct intel_set_config *config;
8392 int ret;
8393
8394 BUG_ON(!set);
8395 BUG_ON(!set->crtc);
8396 BUG_ON(!set->crtc->helper_private);
8397
8398 /* Enforce sane interface api - has been abused by the fb helper. */
8399 BUG_ON(!set->mode && set->fb);
8400 BUG_ON(set->fb && set->num_connectors == 0);
8401
8402 if (set->fb) {
8403 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
8404 set->crtc->base.id, set->fb->base.id,
8405 (int)set->num_connectors, set->x, set->y);
8406 } else {
8407 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
8408 }
8409
8410 dev = set->crtc->dev;
8411
8412 ret = -ENOMEM;
8413 config = kzalloc(sizeof(*config), GFP_KERNEL);
8414 if (!config)
8415 goto out_config;
8416
8417 ret = intel_set_config_save_state(dev, config);
8418 if (ret)
8419 goto out_config;
8420
8421 save_set.crtc = set->crtc;
8422 save_set.mode = &set->crtc->mode;
8423 save_set.x = set->crtc->x;
8424 save_set.y = set->crtc->y;
8425 save_set.fb = set->crtc->fb;
8426
8427 /* Compute whether we need a full modeset, only an fb base update or no
8428 * change at all. In the future we might also check whether only the
8429 * mode changed, e.g. for LVDS where we only change the panel fitter in
8430 * such cases. */
8431 intel_set_config_compute_mode_changes(set, config);
8432
8433 ret = intel_modeset_stage_output_state(dev, set, config);
8434 if (ret)
8435 goto fail;
8436
8437 if (config->mode_changed) {
8438 if (set->mode) {
8439 DRM_DEBUG_KMS("attempting to set mode from"
8440 " userspace\n");
8441 drm_mode_debug_printmodeline(set->mode);
8442 }
8443
8444 ret = intel_set_mode(set->crtc, set->mode,
8445 set->x, set->y, set->fb);
8446 if (ret) {
8447 DRM_ERROR("failed to set mode on [CRTC:%d], err = %d\n",
8448 set->crtc->base.id, ret);
8449 goto fail;
8450 }
8451 } else if (config->fb_changed) {
8452 intel_crtc_wait_for_pending_flips(set->crtc);
8453
8454 ret = intel_pipe_set_base(set->crtc,
8455 set->x, set->y, set->fb);
8456 }
8457
8458 intel_set_config_free(config);
8459
8460 return 0;
8461
8462 fail:
8463 intel_set_config_restore_state(dev, config);
8464
8465 /* Try to restore the config */
8466 if (config->mode_changed &&
8467 intel_set_mode(save_set.crtc, save_set.mode,
8468 save_set.x, save_set.y, save_set.fb))
8469 DRM_ERROR("failed to restore config after modeset failure\n");
8470
8471 out_config:
8472 intel_set_config_free(config);
8473 return ret;
8474 }
8475
8476 static const struct drm_crtc_funcs intel_crtc_funcs = {
8477 .cursor_set = intel_crtc_cursor_set,
8478 .cursor_move = intel_crtc_cursor_move,
8479 .gamma_set = intel_crtc_gamma_set,
8480 .set_config = intel_crtc_set_config,
8481 .destroy = intel_crtc_destroy,
8482 .page_flip = intel_crtc_page_flip,
8483 };
8484
8485 static void intel_cpu_pll_init(struct drm_device *dev)
8486 {
8487 if (HAS_DDI(dev))
8488 intel_ddi_pll_init(dev);
8489 }
8490
8491 static void intel_pch_pll_init(struct drm_device *dev)
8492 {
8493 drm_i915_private_t *dev_priv = dev->dev_private;
8494 int i;
8495
8496 if (dev_priv->num_pch_pll == 0) {
8497 DRM_DEBUG_KMS("No PCH PLLs on this hardware, skipping initialisation\n");
8498 return;
8499 }
8500
8501 for (i = 0; i < dev_priv->num_pch_pll; i++) {
8502 dev_priv->pch_plls[i].pll_reg = _PCH_DPLL(i);
8503 dev_priv->pch_plls[i].fp0_reg = _PCH_FP0(i);
8504 dev_priv->pch_plls[i].fp1_reg = _PCH_FP1(i);
8505 }
8506 }
8507
8508 static void intel_crtc_init(struct drm_device *dev, int pipe)
8509 {
8510 drm_i915_private_t *dev_priv = dev->dev_private;
8511 struct intel_crtc *intel_crtc;
8512 int i;
8513
8514 intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
8515 if (intel_crtc == NULL)
8516 return;
8517
8518 drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
8519
8520 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
8521 for (i = 0; i < 256; i++) {
8522 intel_crtc->lut_r[i] = i;
8523 intel_crtc->lut_g[i] = i;
8524 intel_crtc->lut_b[i] = i;
8525 }
8526
8527 /* Swap pipes & planes for FBC on pre-965 */
8528 intel_crtc->pipe = pipe;
8529 intel_crtc->plane = pipe;
8530 intel_crtc->config.cpu_transcoder = pipe;
8531 if (IS_MOBILE(dev) && IS_GEN3(dev)) {
8532 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
8533 intel_crtc->plane = !pipe;
8534 }
8535
8536 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
8537 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
8538 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
8539 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
8540
8541 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
8542 }
8543
8544 int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
8545 struct drm_file *file)
8546 {
8547 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
8548 struct drm_mode_object *drmmode_obj;
8549 struct intel_crtc *crtc;
8550
8551 if (!drm_core_check_feature(dev, DRIVER_MODESET))
8552 return -ENODEV;
8553
8554 drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
8555 DRM_MODE_OBJECT_CRTC);
8556
8557 if (!drmmode_obj) {
8558 DRM_ERROR("no such CRTC id\n");
8559 return -EINVAL;
8560 }
8561
8562 crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
8563 pipe_from_crtc_id->pipe = crtc->pipe;
8564
8565 return 0;
8566 }
8567
8568 static int intel_encoder_clones(struct intel_encoder *encoder)
8569 {
8570 struct drm_device *dev = encoder->base.dev;
8571 struct intel_encoder *source_encoder;
8572 int index_mask = 0;
8573 int entry = 0;
8574
8575 list_for_each_entry(source_encoder,
8576 &dev->mode_config.encoder_list, base.head) {
8577
8578 if (encoder == source_encoder)
8579 index_mask |= (1 << entry);
8580
8581 /* Intel hw has only one MUX where enocoders could be cloned. */
8582 if (encoder->cloneable && source_encoder->cloneable)
8583 index_mask |= (1 << entry);
8584
8585 entry++;
8586 }
8587
8588 return index_mask;
8589 }
8590
8591 static bool has_edp_a(struct drm_device *dev)
8592 {
8593 struct drm_i915_private *dev_priv = dev->dev_private;
8594
8595 if (!IS_MOBILE(dev))
8596 return false;
8597
8598 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
8599 return false;
8600
8601 if (IS_GEN5(dev) &&
8602 (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
8603 return false;
8604
8605 return true;
8606 }
8607
8608 static void intel_setup_outputs(struct drm_device *dev)
8609 {
8610 struct drm_i915_private *dev_priv = dev->dev_private;
8611 struct intel_encoder *encoder;
8612 bool dpd_is_edp = false;
8613 bool has_lvds;
8614
8615 has_lvds = intel_lvds_init(dev);
8616 if (!has_lvds && !HAS_PCH_SPLIT(dev)) {
8617 /* disable the panel fitter on everything but LVDS */
8618 I915_WRITE(PFIT_CONTROL, 0);
8619 }
8620
8621 if (!IS_ULT(dev))
8622 intel_crt_init(dev);
8623
8624 if (HAS_DDI(dev)) {
8625 int found;
8626
8627 /* Haswell uses DDI functions to detect digital outputs */
8628 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
8629 /* DDI A only supports eDP */
8630 if (found)
8631 intel_ddi_init(dev, PORT_A);
8632
8633 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
8634 * register */
8635 found = I915_READ(SFUSE_STRAP);
8636
8637 if (found & SFUSE_STRAP_DDIB_DETECTED)
8638 intel_ddi_init(dev, PORT_B);
8639 if (found & SFUSE_STRAP_DDIC_DETECTED)
8640 intel_ddi_init(dev, PORT_C);
8641 if (found & SFUSE_STRAP_DDID_DETECTED)
8642 intel_ddi_init(dev, PORT_D);
8643 } else if (HAS_PCH_SPLIT(dev)) {
8644 int found;
8645 dpd_is_edp = intel_dpd_is_edp(dev);
8646
8647 if (has_edp_a(dev))
8648 intel_dp_init(dev, DP_A, PORT_A);
8649
8650 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
8651 /* PCH SDVOB multiplex with HDMIB */
8652 found = intel_sdvo_init(dev, PCH_SDVOB, true);
8653 if (!found)
8654 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
8655 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
8656 intel_dp_init(dev, PCH_DP_B, PORT_B);
8657 }
8658
8659 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
8660 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
8661
8662 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
8663 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
8664
8665 if (I915_READ(PCH_DP_C) & DP_DETECTED)
8666 intel_dp_init(dev, PCH_DP_C, PORT_C);
8667
8668 if (I915_READ(PCH_DP_D) & DP_DETECTED)
8669 intel_dp_init(dev, PCH_DP_D, PORT_D);
8670 } else if (IS_VALLEYVIEW(dev)) {
8671 /* Check for built-in panel first. Shares lanes with HDMI on SDVOC */
8672 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED)
8673 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
8674
8675 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED) {
8676 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
8677 PORT_B);
8678 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED)
8679 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
8680 }
8681 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
8682 bool found = false;
8683
8684 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
8685 DRM_DEBUG_KMS("probing SDVOB\n");
8686 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
8687 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
8688 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
8689 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
8690 }
8691
8692 if (!found && SUPPORTS_INTEGRATED_DP(dev)) {
8693 DRM_DEBUG_KMS("probing DP_B\n");
8694 intel_dp_init(dev, DP_B, PORT_B);
8695 }
8696 }
8697
8698 /* Before G4X SDVOC doesn't have its own detect register */
8699
8700 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
8701 DRM_DEBUG_KMS("probing SDVOC\n");
8702 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
8703 }
8704
8705 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
8706
8707 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
8708 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
8709 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
8710 }
8711 if (SUPPORTS_INTEGRATED_DP(dev)) {
8712 DRM_DEBUG_KMS("probing DP_C\n");
8713 intel_dp_init(dev, DP_C, PORT_C);
8714 }
8715 }
8716
8717 if (SUPPORTS_INTEGRATED_DP(dev) &&
8718 (I915_READ(DP_D) & DP_DETECTED)) {
8719 DRM_DEBUG_KMS("probing DP_D\n");
8720 intel_dp_init(dev, DP_D, PORT_D);
8721 }
8722 } else if (IS_GEN2(dev))
8723 intel_dvo_init(dev);
8724
8725 if (SUPPORTS_TV(dev))
8726 intel_tv_init(dev);
8727
8728 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
8729 encoder->base.possible_crtcs = encoder->crtc_mask;
8730 encoder->base.possible_clones =
8731 intel_encoder_clones(encoder);
8732 }
8733
8734 intel_init_pch_refclk(dev);
8735
8736 drm_helper_move_panel_connectors_to_head(dev);
8737 }
8738
8739 static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
8740 {
8741 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
8742
8743 drm_framebuffer_cleanup(fb);
8744 drm_gem_object_unreference_unlocked(&intel_fb->obj->base);
8745
8746 kfree(intel_fb);
8747 }
8748
8749 static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
8750 struct drm_file *file,
8751 unsigned int *handle)
8752 {
8753 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
8754 struct drm_i915_gem_object *obj = intel_fb->obj;
8755
8756 return drm_gem_handle_create(file, &obj->base, handle);
8757 }
8758
8759 static const struct drm_framebuffer_funcs intel_fb_funcs = {
8760 .destroy = intel_user_framebuffer_destroy,
8761 .create_handle = intel_user_framebuffer_create_handle,
8762 };
8763
8764 int intel_framebuffer_init(struct drm_device *dev,
8765 struct intel_framebuffer *intel_fb,
8766 struct drm_mode_fb_cmd2 *mode_cmd,
8767 struct drm_i915_gem_object *obj)
8768 {
8769 int ret;
8770
8771 if (obj->tiling_mode == I915_TILING_Y) {
8772 DRM_DEBUG("hardware does not support tiling Y\n");
8773 return -EINVAL;
8774 }
8775
8776 if (mode_cmd->pitches[0] & 63) {
8777 DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
8778 mode_cmd->pitches[0]);
8779 return -EINVAL;
8780 }
8781
8782 /* FIXME <= Gen4 stride limits are bit unclear */
8783 if (mode_cmd->pitches[0] > 32768) {
8784 DRM_DEBUG("pitch (%d) must be at less than 32768\n",
8785 mode_cmd->pitches[0]);
8786 return -EINVAL;
8787 }
8788
8789 if (obj->tiling_mode != I915_TILING_NONE &&
8790 mode_cmd->pitches[0] != obj->stride) {
8791 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
8792 mode_cmd->pitches[0], obj->stride);
8793 return -EINVAL;
8794 }
8795
8796 /* Reject formats not supported by any plane early. */
8797 switch (mode_cmd->pixel_format) {
8798 case DRM_FORMAT_C8:
8799 case DRM_FORMAT_RGB565:
8800 case DRM_FORMAT_XRGB8888:
8801 case DRM_FORMAT_ARGB8888:
8802 break;
8803 case DRM_FORMAT_XRGB1555:
8804 case DRM_FORMAT_ARGB1555:
8805 if (INTEL_INFO(dev)->gen > 3) {
8806 DRM_DEBUG("invalid format: 0x%08x\n", mode_cmd->pixel_format);
8807 return -EINVAL;
8808 }
8809 break;
8810 case DRM_FORMAT_XBGR8888:
8811 case DRM_FORMAT_ABGR8888:
8812 case DRM_FORMAT_XRGB2101010:
8813 case DRM_FORMAT_ARGB2101010:
8814 case DRM_FORMAT_XBGR2101010:
8815 case DRM_FORMAT_ABGR2101010:
8816 if (INTEL_INFO(dev)->gen < 4) {
8817 DRM_DEBUG("invalid format: 0x%08x\n", mode_cmd->pixel_format);
8818 return -EINVAL;
8819 }
8820 break;
8821 case DRM_FORMAT_YUYV:
8822 case DRM_FORMAT_UYVY:
8823 case DRM_FORMAT_YVYU:
8824 case DRM_FORMAT_VYUY:
8825 if (INTEL_INFO(dev)->gen < 5) {
8826 DRM_DEBUG("invalid format: 0x%08x\n", mode_cmd->pixel_format);
8827 return -EINVAL;
8828 }
8829 break;
8830 default:
8831 DRM_DEBUG("unsupported pixel format 0x%08x\n", mode_cmd->pixel_format);
8832 return -EINVAL;
8833 }
8834
8835 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
8836 if (mode_cmd->offsets[0] != 0)
8837 return -EINVAL;
8838
8839 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
8840 intel_fb->obj = obj;
8841
8842 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
8843 if (ret) {
8844 DRM_ERROR("framebuffer init failed %d\n", ret);
8845 return ret;
8846 }
8847
8848 return 0;
8849 }
8850
8851 static struct drm_framebuffer *
8852 intel_user_framebuffer_create(struct drm_device *dev,
8853 struct drm_file *filp,
8854 struct drm_mode_fb_cmd2 *mode_cmd)
8855 {
8856 struct drm_i915_gem_object *obj;
8857
8858 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
8859 mode_cmd->handles[0]));
8860 if (&obj->base == NULL)
8861 return ERR_PTR(-ENOENT);
8862
8863 return intel_framebuffer_create(dev, mode_cmd, obj);
8864 }
8865
8866 static const struct drm_mode_config_funcs intel_mode_funcs = {
8867 .fb_create = intel_user_framebuffer_create,
8868 .output_poll_changed = intel_fb_output_poll_changed,
8869 };
8870
8871 /* Set up chip specific display functions */
8872 static void intel_init_display(struct drm_device *dev)
8873 {
8874 struct drm_i915_private *dev_priv = dev->dev_private;
8875
8876 if (HAS_DDI(dev)) {
8877 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
8878 dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
8879 dev_priv->display.crtc_enable = haswell_crtc_enable;
8880 dev_priv->display.crtc_disable = haswell_crtc_disable;
8881 dev_priv->display.off = haswell_crtc_off;
8882 dev_priv->display.update_plane = ironlake_update_plane;
8883 } else if (HAS_PCH_SPLIT(dev)) {
8884 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
8885 dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
8886 dev_priv->display.crtc_enable = ironlake_crtc_enable;
8887 dev_priv->display.crtc_disable = ironlake_crtc_disable;
8888 dev_priv->display.off = ironlake_crtc_off;
8889 dev_priv->display.update_plane = ironlake_update_plane;
8890 } else if (IS_VALLEYVIEW(dev)) {
8891 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
8892 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
8893 dev_priv->display.crtc_enable = valleyview_crtc_enable;
8894 dev_priv->display.crtc_disable = i9xx_crtc_disable;
8895 dev_priv->display.off = i9xx_crtc_off;
8896 dev_priv->display.update_plane = i9xx_update_plane;
8897 } else {
8898 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
8899 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
8900 dev_priv->display.crtc_enable = i9xx_crtc_enable;
8901 dev_priv->display.crtc_disable = i9xx_crtc_disable;
8902 dev_priv->display.off = i9xx_crtc_off;
8903 dev_priv->display.update_plane = i9xx_update_plane;
8904 }
8905
8906 /* Returns the core display clock speed */
8907 if (IS_VALLEYVIEW(dev))
8908 dev_priv->display.get_display_clock_speed =
8909 valleyview_get_display_clock_speed;
8910 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
8911 dev_priv->display.get_display_clock_speed =
8912 i945_get_display_clock_speed;
8913 else if (IS_I915G(dev))
8914 dev_priv->display.get_display_clock_speed =
8915 i915_get_display_clock_speed;
8916 else if (IS_I945GM(dev) || IS_845G(dev) || IS_PINEVIEW_M(dev))
8917 dev_priv->display.get_display_clock_speed =
8918 i9xx_misc_get_display_clock_speed;
8919 else if (IS_I915GM(dev))
8920 dev_priv->display.get_display_clock_speed =
8921 i915gm_get_display_clock_speed;
8922 else if (IS_I865G(dev))
8923 dev_priv->display.get_display_clock_speed =
8924 i865_get_display_clock_speed;
8925 else if (IS_I85X(dev))
8926 dev_priv->display.get_display_clock_speed =
8927 i855_get_display_clock_speed;
8928 else /* 852, 830 */
8929 dev_priv->display.get_display_clock_speed =
8930 i830_get_display_clock_speed;
8931
8932 if (HAS_PCH_SPLIT(dev)) {
8933 if (IS_GEN5(dev)) {
8934 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
8935 dev_priv->display.write_eld = ironlake_write_eld;
8936 } else if (IS_GEN6(dev)) {
8937 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
8938 dev_priv->display.write_eld = ironlake_write_eld;
8939 } else if (IS_IVYBRIDGE(dev)) {
8940 /* FIXME: detect B0+ stepping and use auto training */
8941 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
8942 dev_priv->display.write_eld = ironlake_write_eld;
8943 dev_priv->display.modeset_global_resources =
8944 ivb_modeset_global_resources;
8945 } else if (IS_HASWELL(dev)) {
8946 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
8947 dev_priv->display.write_eld = haswell_write_eld;
8948 dev_priv->display.modeset_global_resources =
8949 haswell_modeset_global_resources;
8950 }
8951 } else if (IS_G4X(dev)) {
8952 dev_priv->display.write_eld = g4x_write_eld;
8953 }
8954
8955 /* Default just returns -ENODEV to indicate unsupported */
8956 dev_priv->display.queue_flip = intel_default_queue_flip;
8957
8958 switch (INTEL_INFO(dev)->gen) {
8959 case 2:
8960 dev_priv->display.queue_flip = intel_gen2_queue_flip;
8961 break;
8962
8963 case 3:
8964 dev_priv->display.queue_flip = intel_gen3_queue_flip;
8965 break;
8966
8967 case 4:
8968 case 5:
8969 dev_priv->display.queue_flip = intel_gen4_queue_flip;
8970 break;
8971
8972 case 6:
8973 dev_priv->display.queue_flip = intel_gen6_queue_flip;
8974 break;
8975 case 7:
8976 dev_priv->display.queue_flip = intel_gen7_queue_flip;
8977 break;
8978 }
8979 }
8980
8981 /*
8982 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
8983 * resume, or other times. This quirk makes sure that's the case for
8984 * affected systems.
8985 */
8986 static void quirk_pipea_force(struct drm_device *dev)
8987 {
8988 struct drm_i915_private *dev_priv = dev->dev_private;
8989
8990 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
8991 DRM_INFO("applying pipe a force quirk\n");
8992 }
8993
8994 /*
8995 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
8996 */
8997 static void quirk_ssc_force_disable(struct drm_device *dev)
8998 {
8999 struct drm_i915_private *dev_priv = dev->dev_private;
9000 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
9001 DRM_INFO("applying lvds SSC disable quirk\n");
9002 }
9003
9004 /*
9005 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
9006 * brightness value
9007 */
9008 static void quirk_invert_brightness(struct drm_device *dev)
9009 {
9010 struct drm_i915_private *dev_priv = dev->dev_private;
9011 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
9012 DRM_INFO("applying inverted panel brightness quirk\n");
9013 }
9014
9015 struct intel_quirk {
9016 int device;
9017 int subsystem_vendor;
9018 int subsystem_device;
9019 void (*hook)(struct drm_device *dev);
9020 };
9021
9022 /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
9023 struct intel_dmi_quirk {
9024 void (*hook)(struct drm_device *dev);
9025 const struct dmi_system_id (*dmi_id_list)[];
9026 };
9027
9028 static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
9029 {
9030 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
9031 return 1;
9032 }
9033
9034 static const struct intel_dmi_quirk intel_dmi_quirks[] = {
9035 {
9036 .dmi_id_list = &(const struct dmi_system_id[]) {
9037 {
9038 .callback = intel_dmi_reverse_brightness,
9039 .ident = "NCR Corporation",
9040 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
9041 DMI_MATCH(DMI_PRODUCT_NAME, ""),
9042 },
9043 },
9044 { } /* terminating entry */
9045 },
9046 .hook = quirk_invert_brightness,
9047 },
9048 };
9049
9050 static struct intel_quirk intel_quirks[] = {
9051 /* HP Mini needs pipe A force quirk (LP: #322104) */
9052 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
9053
9054 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
9055 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
9056
9057 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
9058 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
9059
9060 /* 830/845 need to leave pipe A & dpll A up */
9061 { 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
9062 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
9063
9064 /* Lenovo U160 cannot use SSC on LVDS */
9065 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
9066
9067 /* Sony Vaio Y cannot use SSC on LVDS */
9068 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
9069
9070 /* Acer Aspire 5734Z must invert backlight brightness */
9071 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
9072
9073 /* Acer/eMachines G725 */
9074 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
9075
9076 /* Acer/eMachines e725 */
9077 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
9078
9079 /* Acer/Packard Bell NCL20 */
9080 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
9081
9082 /* Acer Aspire 4736Z */
9083 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
9084 };
9085
9086 static void intel_init_quirks(struct drm_device *dev)
9087 {
9088 struct pci_dev *d = dev->pdev;
9089 int i;
9090
9091 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
9092 struct intel_quirk *q = &intel_quirks[i];
9093
9094 if (d->device == q->device &&
9095 (d->subsystem_vendor == q->subsystem_vendor ||
9096 q->subsystem_vendor == PCI_ANY_ID) &&
9097 (d->subsystem_device == q->subsystem_device ||
9098 q->subsystem_device == PCI_ANY_ID))
9099 q->hook(dev);
9100 }
9101 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
9102 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
9103 intel_dmi_quirks[i].hook(dev);
9104 }
9105 }
9106
9107 /* Disable the VGA plane that we never use */
9108 static void i915_disable_vga(struct drm_device *dev)
9109 {
9110 struct drm_i915_private *dev_priv = dev->dev_private;
9111 u8 sr1;
9112 u32 vga_reg = i915_vgacntrl_reg(dev);
9113
9114 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
9115 outb(SR01, VGA_SR_INDEX);
9116 sr1 = inb(VGA_SR_DATA);
9117 outb(sr1 | 1<<5, VGA_SR_DATA);
9118 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
9119 udelay(300);
9120
9121 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9122 POSTING_READ(vga_reg);
9123 }
9124
9125 void intel_modeset_init_hw(struct drm_device *dev)
9126 {
9127 intel_init_power_well(dev);
9128
9129 intel_prepare_ddi(dev);
9130
9131 intel_init_clock_gating(dev);
9132
9133 mutex_lock(&dev->struct_mutex);
9134 intel_enable_gt_powersave(dev);
9135 mutex_unlock(&dev->struct_mutex);
9136 }
9137
9138 void intel_modeset_init(struct drm_device *dev)
9139 {
9140 struct drm_i915_private *dev_priv = dev->dev_private;
9141 int i, j, ret;
9142
9143 drm_mode_config_init(dev);
9144
9145 dev->mode_config.min_width = 0;
9146 dev->mode_config.min_height = 0;
9147
9148 dev->mode_config.preferred_depth = 24;
9149 dev->mode_config.prefer_shadow = 1;
9150
9151 dev->mode_config.funcs = &intel_mode_funcs;
9152
9153 intel_init_quirks(dev);
9154
9155 intel_init_pm(dev);
9156
9157 if (INTEL_INFO(dev)->num_pipes == 0)
9158 return;
9159
9160 intel_init_display(dev);
9161
9162 if (IS_GEN2(dev)) {
9163 dev->mode_config.max_width = 2048;
9164 dev->mode_config.max_height = 2048;
9165 } else if (IS_GEN3(dev)) {
9166 dev->mode_config.max_width = 4096;
9167 dev->mode_config.max_height = 4096;
9168 } else {
9169 dev->mode_config.max_width = 8192;
9170 dev->mode_config.max_height = 8192;
9171 }
9172 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
9173
9174 DRM_DEBUG_KMS("%d display pipe%s available.\n",
9175 INTEL_INFO(dev)->num_pipes,
9176 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
9177
9178 for (i = 0; i < INTEL_INFO(dev)->num_pipes; i++) {
9179 intel_crtc_init(dev, i);
9180 for (j = 0; j < dev_priv->num_plane; j++) {
9181 ret = intel_plane_init(dev, i, j);
9182 if (ret)
9183 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
9184 pipe_name(i), sprite_name(i, j), ret);
9185 }
9186 }
9187
9188 intel_cpu_pll_init(dev);
9189 intel_pch_pll_init(dev);
9190
9191 /* Just disable it once at startup */
9192 i915_disable_vga(dev);
9193 intel_setup_outputs(dev);
9194
9195 /* Just in case the BIOS is doing something questionable. */
9196 intel_disable_fbc(dev);
9197 }
9198
9199 static void
9200 intel_connector_break_all_links(struct intel_connector *connector)
9201 {
9202 connector->base.dpms = DRM_MODE_DPMS_OFF;
9203 connector->base.encoder = NULL;
9204 connector->encoder->connectors_active = false;
9205 connector->encoder->base.crtc = NULL;
9206 }
9207
9208 static void intel_enable_pipe_a(struct drm_device *dev)
9209 {
9210 struct intel_connector *connector;
9211 struct drm_connector *crt = NULL;
9212 struct intel_load_detect_pipe load_detect_temp;
9213
9214 /* We can't just switch on the pipe A, we need to set things up with a
9215 * proper mode and output configuration. As a gross hack, enable pipe A
9216 * by enabling the load detect pipe once. */
9217 list_for_each_entry(connector,
9218 &dev->mode_config.connector_list,
9219 base.head) {
9220 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
9221 crt = &connector->base;
9222 break;
9223 }
9224 }
9225
9226 if (!crt)
9227 return;
9228
9229 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp))
9230 intel_release_load_detect_pipe(crt, &load_detect_temp);
9231
9232
9233 }
9234
9235 static bool
9236 intel_check_plane_mapping(struct intel_crtc *crtc)
9237 {
9238 struct drm_device *dev = crtc->base.dev;
9239 struct drm_i915_private *dev_priv = dev->dev_private;
9240 u32 reg, val;
9241
9242 if (INTEL_INFO(dev)->num_pipes == 1)
9243 return true;
9244
9245 reg = DSPCNTR(!crtc->plane);
9246 val = I915_READ(reg);
9247
9248 if ((val & DISPLAY_PLANE_ENABLE) &&
9249 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
9250 return false;
9251
9252 return true;
9253 }
9254
9255 static void intel_sanitize_crtc(struct intel_crtc *crtc)
9256 {
9257 struct drm_device *dev = crtc->base.dev;
9258 struct drm_i915_private *dev_priv = dev->dev_private;
9259 u32 reg;
9260
9261 /* Clear any frame start delays used for debugging left by the BIOS */
9262 reg = PIPECONF(crtc->config.cpu_transcoder);
9263 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
9264
9265 /* We need to sanitize the plane -> pipe mapping first because this will
9266 * disable the crtc (and hence change the state) if it is wrong. Note
9267 * that gen4+ has a fixed plane -> pipe mapping. */
9268 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
9269 struct intel_connector *connector;
9270 bool plane;
9271
9272 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
9273 crtc->base.base.id);
9274
9275 /* Pipe has the wrong plane attached and the plane is active.
9276 * Temporarily change the plane mapping and disable everything
9277 * ... */
9278 plane = crtc->plane;
9279 crtc->plane = !plane;
9280 dev_priv->display.crtc_disable(&crtc->base);
9281 crtc->plane = plane;
9282
9283 /* ... and break all links. */
9284 list_for_each_entry(connector, &dev->mode_config.connector_list,
9285 base.head) {
9286 if (connector->encoder->base.crtc != &crtc->base)
9287 continue;
9288
9289 intel_connector_break_all_links(connector);
9290 }
9291
9292 WARN_ON(crtc->active);
9293 crtc->base.enabled = false;
9294 }
9295
9296 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
9297 crtc->pipe == PIPE_A && !crtc->active) {
9298 /* BIOS forgot to enable pipe A, this mostly happens after
9299 * resume. Force-enable the pipe to fix this, the update_dpms
9300 * call below we restore the pipe to the right state, but leave
9301 * the required bits on. */
9302 intel_enable_pipe_a(dev);
9303 }
9304
9305 /* Adjust the state of the output pipe according to whether we
9306 * have active connectors/encoders. */
9307 intel_crtc_update_dpms(&crtc->base);
9308
9309 if (crtc->active != crtc->base.enabled) {
9310 struct intel_encoder *encoder;
9311
9312 /* This can happen either due to bugs in the get_hw_state
9313 * functions or because the pipe is force-enabled due to the
9314 * pipe A quirk. */
9315 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
9316 crtc->base.base.id,
9317 crtc->base.enabled ? "enabled" : "disabled",
9318 crtc->active ? "enabled" : "disabled");
9319
9320 crtc->base.enabled = crtc->active;
9321
9322 /* Because we only establish the connector -> encoder ->
9323 * crtc links if something is active, this means the
9324 * crtc is now deactivated. Break the links. connector
9325 * -> encoder links are only establish when things are
9326 * actually up, hence no need to break them. */
9327 WARN_ON(crtc->active);
9328
9329 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
9330 WARN_ON(encoder->connectors_active);
9331 encoder->base.crtc = NULL;
9332 }
9333 }
9334 }
9335
9336 static void intel_sanitize_encoder(struct intel_encoder *encoder)
9337 {
9338 struct intel_connector *connector;
9339 struct drm_device *dev = encoder->base.dev;
9340
9341 /* We need to check both for a crtc link (meaning that the
9342 * encoder is active and trying to read from a pipe) and the
9343 * pipe itself being active. */
9344 bool has_active_crtc = encoder->base.crtc &&
9345 to_intel_crtc(encoder->base.crtc)->active;
9346
9347 if (encoder->connectors_active && !has_active_crtc) {
9348 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
9349 encoder->base.base.id,
9350 drm_get_encoder_name(&encoder->base));
9351
9352 /* Connector is active, but has no active pipe. This is
9353 * fallout from our resume register restoring. Disable
9354 * the encoder manually again. */
9355 if (encoder->base.crtc) {
9356 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
9357 encoder->base.base.id,
9358 drm_get_encoder_name(&encoder->base));
9359 encoder->disable(encoder);
9360 }
9361
9362 /* Inconsistent output/port/pipe state happens presumably due to
9363 * a bug in one of the get_hw_state functions. Or someplace else
9364 * in our code, like the register restore mess on resume. Clamp
9365 * things to off as a safer default. */
9366 list_for_each_entry(connector,
9367 &dev->mode_config.connector_list,
9368 base.head) {
9369 if (connector->encoder != encoder)
9370 continue;
9371
9372 intel_connector_break_all_links(connector);
9373 }
9374 }
9375 /* Enabled encoders without active connectors will be fixed in
9376 * the crtc fixup. */
9377 }
9378
9379 void i915_redisable_vga(struct drm_device *dev)
9380 {
9381 struct drm_i915_private *dev_priv = dev->dev_private;
9382 u32 vga_reg = i915_vgacntrl_reg(dev);
9383
9384 if (I915_READ(vga_reg) != VGA_DISP_DISABLE) {
9385 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
9386 i915_disable_vga(dev);
9387 }
9388 }
9389
9390 /* Scan out the current hw modeset state, sanitizes it and maps it into the drm
9391 * and i915 state tracking structures. */
9392 void intel_modeset_setup_hw_state(struct drm_device *dev,
9393 bool force_restore)
9394 {
9395 struct drm_i915_private *dev_priv = dev->dev_private;
9396 enum pipe pipe;
9397 u32 tmp;
9398 struct drm_plane *plane;
9399 struct intel_crtc *crtc;
9400 struct intel_encoder *encoder;
9401 struct intel_connector *connector;
9402
9403 if (HAS_DDI(dev)) {
9404 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9405
9406 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9407 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9408 case TRANS_DDI_EDP_INPUT_A_ON:
9409 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9410 pipe = PIPE_A;
9411 break;
9412 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9413 pipe = PIPE_B;
9414 break;
9415 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9416 pipe = PIPE_C;
9417 break;
9418 default:
9419 /* A bogus value has been programmed, disable
9420 * the transcoder */
9421 WARN(1, "Bogus eDP source %08x\n", tmp);
9422 intel_ddi_disable_transcoder_func(dev_priv,
9423 TRANSCODER_EDP);
9424 goto setup_pipes;
9425 }
9426
9427 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
9428 crtc->config.cpu_transcoder = TRANSCODER_EDP;
9429
9430 DRM_DEBUG_KMS("Pipe %c using transcoder EDP\n",
9431 pipe_name(pipe));
9432 }
9433 }
9434
9435 setup_pipes:
9436 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
9437 base.head) {
9438 enum transcoder tmp = crtc->config.cpu_transcoder;
9439 memset(&crtc->config, 0, sizeof(crtc->config));
9440 crtc->config.cpu_transcoder = tmp;
9441
9442 crtc->active = dev_priv->display.get_pipe_config(crtc,
9443 &crtc->config);
9444
9445 crtc->base.enabled = crtc->active;
9446
9447 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
9448 crtc->base.base.id,
9449 crtc->active ? "enabled" : "disabled");
9450 }
9451
9452 if (HAS_DDI(dev))
9453 intel_ddi_setup_hw_pll_state(dev);
9454
9455 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9456 base.head) {
9457 pipe = 0;
9458
9459 if (encoder->get_hw_state(encoder, &pipe)) {
9460 encoder->base.crtc =
9461 dev_priv->pipe_to_crtc_mapping[pipe];
9462 } else {
9463 encoder->base.crtc = NULL;
9464 }
9465
9466 encoder->connectors_active = false;
9467 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe=%i\n",
9468 encoder->base.base.id,
9469 drm_get_encoder_name(&encoder->base),
9470 encoder->base.crtc ? "enabled" : "disabled",
9471 pipe);
9472 }
9473
9474 list_for_each_entry(connector, &dev->mode_config.connector_list,
9475 base.head) {
9476 if (connector->get_hw_state(connector)) {
9477 connector->base.dpms = DRM_MODE_DPMS_ON;
9478 connector->encoder->connectors_active = true;
9479 connector->base.encoder = &connector->encoder->base;
9480 } else {
9481 connector->base.dpms = DRM_MODE_DPMS_OFF;
9482 connector->base.encoder = NULL;
9483 }
9484 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
9485 connector->base.base.id,
9486 drm_get_connector_name(&connector->base),
9487 connector->base.encoder ? "enabled" : "disabled");
9488 }
9489
9490 /* HW state is read out, now we need to sanitize this mess. */
9491 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9492 base.head) {
9493 intel_sanitize_encoder(encoder);
9494 }
9495
9496 for_each_pipe(pipe) {
9497 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
9498 intel_sanitize_crtc(crtc);
9499 }
9500
9501 if (force_restore) {
9502 /*
9503 * We need to use raw interfaces for restoring state to avoid
9504 * checking (bogus) intermediate states.
9505 */
9506 for_each_pipe(pipe) {
9507 struct drm_crtc *crtc =
9508 dev_priv->pipe_to_crtc_mapping[pipe];
9509
9510 __intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
9511 crtc->fb);
9512 }
9513 list_for_each_entry(plane, &dev->mode_config.plane_list, head)
9514 intel_plane_restore(plane);
9515
9516 i915_redisable_vga(dev);
9517 } else {
9518 intel_modeset_update_staged_output_state(dev);
9519 }
9520
9521 intel_modeset_check_state(dev);
9522
9523 drm_mode_config_reset(dev);
9524 }
9525
9526 void intel_modeset_gem_init(struct drm_device *dev)
9527 {
9528 intel_modeset_init_hw(dev);
9529
9530 intel_setup_overlay(dev);
9531
9532 intel_modeset_setup_hw_state(dev, false);
9533 }
9534
9535 void intel_modeset_cleanup(struct drm_device *dev)
9536 {
9537 struct drm_i915_private *dev_priv = dev->dev_private;
9538 struct drm_crtc *crtc;
9539 struct intel_crtc *intel_crtc;
9540
9541 /*
9542 * Interrupts and polling as the first thing to avoid creating havoc.
9543 * Too much stuff here (turning of rps, connectors, ...) would
9544 * experience fancy races otherwise.
9545 */
9546 drm_irq_uninstall(dev);
9547 cancel_work_sync(&dev_priv->hotplug_work);
9548 /*
9549 * Due to the hpd irq storm handling the hotplug work can re-arm the
9550 * poll handlers. Hence disable polling after hpd handling is shut down.
9551 */
9552 drm_kms_helper_poll_fini(dev);
9553
9554 mutex_lock(&dev->struct_mutex);
9555
9556 intel_unregister_dsm_handler();
9557
9558 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
9559 /* Skip inactive CRTCs */
9560 if (!crtc->fb)
9561 continue;
9562
9563 intel_crtc = to_intel_crtc(crtc);
9564 intel_increase_pllclock(crtc);
9565 }
9566
9567 intel_disable_fbc(dev);
9568
9569 intel_disable_gt_powersave(dev);
9570
9571 ironlake_teardown_rc6(dev);
9572
9573 mutex_unlock(&dev->struct_mutex);
9574
9575 /* flush any delayed tasks or pending work */
9576 flush_scheduled_work();
9577
9578 /* destroy backlight, if any, before the connectors */
9579 intel_panel_destroy_backlight(dev);
9580
9581 drm_mode_config_cleanup(dev);
9582
9583 intel_cleanup_overlay(dev);
9584 }
9585
9586 /*
9587 * Return which encoder is currently attached for connector.
9588 */
9589 struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
9590 {
9591 return &intel_attached_encoder(connector)->base;
9592 }
9593
9594 void intel_connector_attach_encoder(struct intel_connector *connector,
9595 struct intel_encoder *encoder)
9596 {
9597 connector->encoder = encoder;
9598 drm_mode_connector_attach_encoder(&connector->base,
9599 &encoder->base);
9600 }
9601
9602 /*
9603 * set vga decode state - true == enable VGA decode
9604 */
9605 int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
9606 {
9607 struct drm_i915_private *dev_priv = dev->dev_private;
9608 u16 gmch_ctrl;
9609
9610 pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
9611 if (state)
9612 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
9613 else
9614 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
9615 pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
9616 return 0;
9617 }
9618
9619 #ifdef CONFIG_DEBUG_FS
9620 #include <linux/seq_file.h>
9621
9622 struct intel_display_error_state {
9623 struct intel_cursor_error_state {
9624 u32 control;
9625 u32 position;
9626 u32 base;
9627 u32 size;
9628 } cursor[I915_MAX_PIPES];
9629
9630 struct intel_pipe_error_state {
9631 u32 conf;
9632 u32 source;
9633
9634 u32 htotal;
9635 u32 hblank;
9636 u32 hsync;
9637 u32 vtotal;
9638 u32 vblank;
9639 u32 vsync;
9640 } pipe[I915_MAX_PIPES];
9641
9642 struct intel_plane_error_state {
9643 u32 control;
9644 u32 stride;
9645 u32 size;
9646 u32 pos;
9647 u32 addr;
9648 u32 surface;
9649 u32 tile_offset;
9650 } plane[I915_MAX_PIPES];
9651 };
9652
9653 struct intel_display_error_state *
9654 intel_display_capture_error_state(struct drm_device *dev)
9655 {
9656 drm_i915_private_t *dev_priv = dev->dev_private;
9657 struct intel_display_error_state *error;
9658 enum transcoder cpu_transcoder;
9659 int i;
9660
9661 error = kmalloc(sizeof(*error), GFP_ATOMIC);
9662 if (error == NULL)
9663 return NULL;
9664
9665 for_each_pipe(i) {
9666 cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv, i);
9667
9668 if (INTEL_INFO(dev)->gen <= 6 || IS_VALLEYVIEW(dev)) {
9669 error->cursor[i].control = I915_READ(CURCNTR(i));
9670 error->cursor[i].position = I915_READ(CURPOS(i));
9671 error->cursor[i].base = I915_READ(CURBASE(i));
9672 } else {
9673 error->cursor[i].control = I915_READ(CURCNTR_IVB(i));
9674 error->cursor[i].position = I915_READ(CURPOS_IVB(i));
9675 error->cursor[i].base = I915_READ(CURBASE_IVB(i));
9676 }
9677
9678 error->plane[i].control = I915_READ(DSPCNTR(i));
9679 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
9680 if (INTEL_INFO(dev)->gen <= 3) {
9681 error->plane[i].size = I915_READ(DSPSIZE(i));
9682 error->plane[i].pos = I915_READ(DSPPOS(i));
9683 }
9684 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
9685 error->plane[i].addr = I915_READ(DSPADDR(i));
9686 if (INTEL_INFO(dev)->gen >= 4) {
9687 error->plane[i].surface = I915_READ(DSPSURF(i));
9688 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
9689 }
9690
9691 error->pipe[i].conf = I915_READ(PIPECONF(cpu_transcoder));
9692 error->pipe[i].source = I915_READ(PIPESRC(i));
9693 error->pipe[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
9694 error->pipe[i].hblank = I915_READ(HBLANK(cpu_transcoder));
9695 error->pipe[i].hsync = I915_READ(HSYNC(cpu_transcoder));
9696 error->pipe[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
9697 error->pipe[i].vblank = I915_READ(VBLANK(cpu_transcoder));
9698 error->pipe[i].vsync = I915_READ(VSYNC(cpu_transcoder));
9699 }
9700
9701 return error;
9702 }
9703
9704 void
9705 intel_display_print_error_state(struct seq_file *m,
9706 struct drm_device *dev,
9707 struct intel_display_error_state *error)
9708 {
9709 int i;
9710
9711 seq_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
9712 for_each_pipe(i) {
9713 seq_printf(m, "Pipe [%d]:\n", i);
9714 seq_printf(m, " CONF: %08x\n", error->pipe[i].conf);
9715 seq_printf(m, " SRC: %08x\n", error->pipe[i].source);
9716 seq_printf(m, " HTOTAL: %08x\n", error->pipe[i].htotal);
9717 seq_printf(m, " HBLANK: %08x\n", error->pipe[i].hblank);
9718 seq_printf(m, " HSYNC: %08x\n", error->pipe[i].hsync);
9719 seq_printf(m, " VTOTAL: %08x\n", error->pipe[i].vtotal);
9720 seq_printf(m, " VBLANK: %08x\n", error->pipe[i].vblank);
9721 seq_printf(m, " VSYNC: %08x\n", error->pipe[i].vsync);
9722
9723 seq_printf(m, "Plane [%d]:\n", i);
9724 seq_printf(m, " CNTR: %08x\n", error->plane[i].control);
9725 seq_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
9726 if (INTEL_INFO(dev)->gen <= 3) {
9727 seq_printf(m, " SIZE: %08x\n", error->plane[i].size);
9728 seq_printf(m, " POS: %08x\n", error->plane[i].pos);
9729 }
9730 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
9731 seq_printf(m, " ADDR: %08x\n", error->plane[i].addr);
9732 if (INTEL_INFO(dev)->gen >= 4) {
9733 seq_printf(m, " SURF: %08x\n", error->plane[i].surface);
9734 seq_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
9735 }
9736
9737 seq_printf(m, "Cursor [%d]:\n", i);
9738 seq_printf(m, " CNTR: %08x\n", error->cursor[i].control);
9739 seq_printf(m, " POS: %08x\n", error->cursor[i].position);
9740 seq_printf(m, " BASE: %08x\n", error->cursor[i].base);
9741 }
9742 }
9743 #endif
This page took 0.431724 seconds and 5 git commands to generate.