9a29fd67c3a1bd9c3acc75735e7bd8472ca690bb
[linux-2.6.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/cpufreq.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 "drmP.h"
36 #include "intel_drv.h"
37 #include "i915_drm.h"
38 #include "i915_drv.h"
39 #include "i915_trace.h"
40 #include "drm_dp_helper.h"
41 #include "drm_crtc_helper.h"
42 #include <linux/dma_remapping.h>
43
44 #define HAS_eDP (intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
45
46 bool intel_pipe_has_type(struct drm_crtc *crtc, int type);
47 static void intel_update_watermarks(struct drm_device *dev);
48 static void intel_increase_pllclock(struct drm_crtc *crtc);
49 static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
50
51 typedef struct {
52         /* given values */
53         int n;
54         int m1, m2;
55         int p1, p2;
56         /* derived values */
57         int     dot;
58         int     vco;
59         int     m;
60         int     p;
61 } intel_clock_t;
62
63 typedef struct {
64         int     min, max;
65 } intel_range_t;
66
67 typedef struct {
68         int     dot_limit;
69         int     p2_slow, p2_fast;
70 } intel_p2_t;
71
72 #define INTEL_P2_NUM                  2
73 typedef struct intel_limit intel_limit_t;
74 struct intel_limit {
75         intel_range_t   dot, vco, n, m, m1, m2, p, p1;
76         intel_p2_t          p2;
77         bool (* find_pll)(const intel_limit_t *, struct drm_crtc *,
78                         int, int, intel_clock_t *, intel_clock_t *);
79 };
80
81 /* FDI */
82 #define IRONLAKE_FDI_FREQ               2700000 /* in kHz for mode->clock */
83
84 static bool
85 intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
86                     int target, int refclk, intel_clock_t *match_clock,
87                     intel_clock_t *best_clock);
88 static bool
89 intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
90                         int target, int refclk, intel_clock_t *match_clock,
91                         intel_clock_t *best_clock);
92
93 static bool
94 intel_find_pll_g4x_dp(const intel_limit_t *, struct drm_crtc *crtc,
95                       int target, int refclk, intel_clock_t *match_clock,
96                       intel_clock_t *best_clock);
97 static bool
98 intel_find_pll_ironlake_dp(const intel_limit_t *, struct drm_crtc *crtc,
99                            int target, int refclk, intel_clock_t *match_clock,
100                            intel_clock_t *best_clock);
101
102 static inline u32 /* units of 100MHz */
103 intel_fdi_link_freq(struct drm_device *dev)
104 {
105         if (IS_GEN5(dev)) {
106                 struct drm_i915_private *dev_priv = dev->dev_private;
107                 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
108         } else
109                 return 27;
110 }
111
112 static const intel_limit_t intel_limits_i8xx_dvo = {
113         .dot = { .min = 25000, .max = 350000 },
114         .vco = { .min = 930000, .max = 1400000 },
115         .n = { .min = 3, .max = 16 },
116         .m = { .min = 96, .max = 140 },
117         .m1 = { .min = 18, .max = 26 },
118         .m2 = { .min = 6, .max = 16 },
119         .p = { .min = 4, .max = 128 },
120         .p1 = { .min = 2, .max = 33 },
121         .p2 = { .dot_limit = 165000,
122                 .p2_slow = 4, .p2_fast = 2 },
123         .find_pll = intel_find_best_PLL,
124 };
125
126 static const intel_limit_t intel_limits_i8xx_lvds = {
127         .dot = { .min = 25000, .max = 350000 },
128         .vco = { .min = 930000, .max = 1400000 },
129         .n = { .min = 3, .max = 16 },
130         .m = { .min = 96, .max = 140 },
131         .m1 = { .min = 18, .max = 26 },
132         .m2 = { .min = 6, .max = 16 },
133         .p = { .min = 4, .max = 128 },
134         .p1 = { .min = 1, .max = 6 },
135         .p2 = { .dot_limit = 165000,
136                 .p2_slow = 14, .p2_fast = 7 },
137         .find_pll = intel_find_best_PLL,
138 };
139
140 static const intel_limit_t intel_limits_i9xx_sdvo = {
141         .dot = { .min = 20000, .max = 400000 },
142         .vco = { .min = 1400000, .max = 2800000 },
143         .n = { .min = 1, .max = 6 },
144         .m = { .min = 70, .max = 120 },
145         .m1 = { .min = 10, .max = 22 },
146         .m2 = { .min = 5, .max = 9 },
147         .p = { .min = 5, .max = 80 },
148         .p1 = { .min = 1, .max = 8 },
149         .p2 = { .dot_limit = 200000,
150                 .p2_slow = 10, .p2_fast = 5 },
151         .find_pll = intel_find_best_PLL,
152 };
153
154 static const intel_limit_t intel_limits_i9xx_lvds = {
155         .dot = { .min = 20000, .max = 400000 },
156         .vco = { .min = 1400000, .max = 2800000 },
157         .n = { .min = 1, .max = 6 },
158         .m = { .min = 70, .max = 120 },
159         .m1 = { .min = 10, .max = 22 },
160         .m2 = { .min = 5, .max = 9 },
161         .p = { .min = 7, .max = 98 },
162         .p1 = { .min = 1, .max = 8 },
163         .p2 = { .dot_limit = 112000,
164                 .p2_slow = 14, .p2_fast = 7 },
165         .find_pll = intel_find_best_PLL,
166 };
167
168
169 static const intel_limit_t intel_limits_g4x_sdvo = {
170         .dot = { .min = 25000, .max = 270000 },
171         .vco = { .min = 1750000, .max = 3500000},
172         .n = { .min = 1, .max = 4 },
173         .m = { .min = 104, .max = 138 },
174         .m1 = { .min = 17, .max = 23 },
175         .m2 = { .min = 5, .max = 11 },
176         .p = { .min = 10, .max = 30 },
177         .p1 = { .min = 1, .max = 3},
178         .p2 = { .dot_limit = 270000,
179                 .p2_slow = 10,
180                 .p2_fast = 10
181         },
182         .find_pll = intel_g4x_find_best_PLL,
183 };
184
185 static const intel_limit_t intel_limits_g4x_hdmi = {
186         .dot = { .min = 22000, .max = 400000 },
187         .vco = { .min = 1750000, .max = 3500000},
188         .n = { .min = 1, .max = 4 },
189         .m = { .min = 104, .max = 138 },
190         .m1 = { .min = 16, .max = 23 },
191         .m2 = { .min = 5, .max = 11 },
192         .p = { .min = 5, .max = 80 },
193         .p1 = { .min = 1, .max = 8},
194         .p2 = { .dot_limit = 165000,
195                 .p2_slow = 10, .p2_fast = 5 },
196         .find_pll = intel_g4x_find_best_PLL,
197 };
198
199 static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
200         .dot = { .min = 20000, .max = 115000 },
201         .vco = { .min = 1750000, .max = 3500000 },
202         .n = { .min = 1, .max = 3 },
203         .m = { .min = 104, .max = 138 },
204         .m1 = { .min = 17, .max = 23 },
205         .m2 = { .min = 5, .max = 11 },
206         .p = { .min = 28, .max = 112 },
207         .p1 = { .min = 2, .max = 8 },
208         .p2 = { .dot_limit = 0,
209                 .p2_slow = 14, .p2_fast = 14
210         },
211         .find_pll = intel_g4x_find_best_PLL,
212 };
213
214 static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
215         .dot = { .min = 80000, .max = 224000 },
216         .vco = { .min = 1750000, .max = 3500000 },
217         .n = { .min = 1, .max = 3 },
218         .m = { .min = 104, .max = 138 },
219         .m1 = { .min = 17, .max = 23 },
220         .m2 = { .min = 5, .max = 11 },
221         .p = { .min = 14, .max = 42 },
222         .p1 = { .min = 2, .max = 6 },
223         .p2 = { .dot_limit = 0,
224                 .p2_slow = 7, .p2_fast = 7
225         },
226         .find_pll = intel_g4x_find_best_PLL,
227 };
228
229 static const intel_limit_t intel_limits_g4x_display_port = {
230         .dot = { .min = 161670, .max = 227000 },
231         .vco = { .min = 1750000, .max = 3500000},
232         .n = { .min = 1, .max = 2 },
233         .m = { .min = 97, .max = 108 },
234         .m1 = { .min = 0x10, .max = 0x12 },
235         .m2 = { .min = 0x05, .max = 0x06 },
236         .p = { .min = 10, .max = 20 },
237         .p1 = { .min = 1, .max = 2},
238         .p2 = { .dot_limit = 0,
239                 .p2_slow = 10, .p2_fast = 10 },
240         .find_pll = intel_find_pll_g4x_dp,
241 };
242
243 static const intel_limit_t intel_limits_pineview_sdvo = {
244         .dot = { .min = 20000, .max = 400000},
245         .vco = { .min = 1700000, .max = 3500000 },
246         /* Pineview's Ncounter is a ring counter */
247         .n = { .min = 3, .max = 6 },
248         .m = { .min = 2, .max = 256 },
249         /* Pineview only has one combined m divider, which we treat as m2. */
250         .m1 = { .min = 0, .max = 0 },
251         .m2 = { .min = 0, .max = 254 },
252         .p = { .min = 5, .max = 80 },
253         .p1 = { .min = 1, .max = 8 },
254         .p2 = { .dot_limit = 200000,
255                 .p2_slow = 10, .p2_fast = 5 },
256         .find_pll = intel_find_best_PLL,
257 };
258
259 static const intel_limit_t intel_limits_pineview_lvds = {
260         .dot = { .min = 20000, .max = 400000 },
261         .vco = { .min = 1700000, .max = 3500000 },
262         .n = { .min = 3, .max = 6 },
263         .m = { .min = 2, .max = 256 },
264         .m1 = { .min = 0, .max = 0 },
265         .m2 = { .min = 0, .max = 254 },
266         .p = { .min = 7, .max = 112 },
267         .p1 = { .min = 1, .max = 8 },
268         .p2 = { .dot_limit = 112000,
269                 .p2_slow = 14, .p2_fast = 14 },
270         .find_pll = intel_find_best_PLL,
271 };
272
273 /* Ironlake / Sandybridge
274  *
275  * We calculate clock using (register_value + 2) for N/M1/M2, so here
276  * the range value for them is (actual_value - 2).
277  */
278 static const intel_limit_t intel_limits_ironlake_dac = {
279         .dot = { .min = 25000, .max = 350000 },
280         .vco = { .min = 1760000, .max = 3510000 },
281         .n = { .min = 1, .max = 5 },
282         .m = { .min = 79, .max = 127 },
283         .m1 = { .min = 12, .max = 22 },
284         .m2 = { .min = 5, .max = 9 },
285         .p = { .min = 5, .max = 80 },
286         .p1 = { .min = 1, .max = 8 },
287         .p2 = { .dot_limit = 225000,
288                 .p2_slow = 10, .p2_fast = 5 },
289         .find_pll = intel_g4x_find_best_PLL,
290 };
291
292 static const intel_limit_t intel_limits_ironlake_single_lvds = {
293         .dot = { .min = 25000, .max = 350000 },
294         .vco = { .min = 1760000, .max = 3510000 },
295         .n = { .min = 1, .max = 3 },
296         .m = { .min = 79, .max = 118 },
297         .m1 = { .min = 12, .max = 22 },
298         .m2 = { .min = 5, .max = 9 },
299         .p = { .min = 28, .max = 112 },
300         .p1 = { .min = 2, .max = 8 },
301         .p2 = { .dot_limit = 225000,
302                 .p2_slow = 14, .p2_fast = 14 },
303         .find_pll = intel_g4x_find_best_PLL,
304 };
305
306 static const intel_limit_t intel_limits_ironlake_dual_lvds = {
307         .dot = { .min = 25000, .max = 350000 },
308         .vco = { .min = 1760000, .max = 3510000 },
309         .n = { .min = 1, .max = 3 },
310         .m = { .min = 79, .max = 127 },
311         .m1 = { .min = 12, .max = 22 },
312         .m2 = { .min = 5, .max = 9 },
313         .p = { .min = 14, .max = 56 },
314         .p1 = { .min = 2, .max = 8 },
315         .p2 = { .dot_limit = 225000,
316                 .p2_slow = 7, .p2_fast = 7 },
317         .find_pll = intel_g4x_find_best_PLL,
318 };
319
320 /* LVDS 100mhz refclk limits. */
321 static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
322         .dot = { .min = 25000, .max = 350000 },
323         .vco = { .min = 1760000, .max = 3510000 },
324         .n = { .min = 1, .max = 2 },
325         .m = { .min = 79, .max = 126 },
326         .m1 = { .min = 12, .max = 22 },
327         .m2 = { .min = 5, .max = 9 },
328         .p = { .min = 28, .max = 112 },
329         .p1 = { .min = 2, .max = 8 },
330         .p2 = { .dot_limit = 225000,
331                 .p2_slow = 14, .p2_fast = 14 },
332         .find_pll = intel_g4x_find_best_PLL,
333 };
334
335 static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
336         .dot = { .min = 25000, .max = 350000 },
337         .vco = { .min = 1760000, .max = 3510000 },
338         .n = { .min = 1, .max = 3 },
339         .m = { .min = 79, .max = 126 },
340         .m1 = { .min = 12, .max = 22 },
341         .m2 = { .min = 5, .max = 9 },
342         .p = { .min = 14, .max = 42 },
343         .p1 = { .min = 2, .max = 6 },
344         .p2 = { .dot_limit = 225000,
345                 .p2_slow = 7, .p2_fast = 7 },
346         .find_pll = intel_g4x_find_best_PLL,
347 };
348
349 static const intel_limit_t intel_limits_ironlake_display_port = {
350         .dot = { .min = 25000, .max = 350000 },
351         .vco = { .min = 1760000, .max = 3510000},
352         .n = { .min = 1, .max = 2 },
353         .m = { .min = 81, .max = 90 },
354         .m1 = { .min = 12, .max = 22 },
355         .m2 = { .min = 5, .max = 9 },
356         .p = { .min = 10, .max = 20 },
357         .p1 = { .min = 1, .max = 2},
358         .p2 = { .dot_limit = 0,
359                 .p2_slow = 10, .p2_fast = 10 },
360         .find_pll = intel_find_pll_ironlake_dp,
361 };
362
363 static bool is_dual_link_lvds(struct drm_i915_private *dev_priv,
364                               unsigned int reg)
365 {
366         unsigned int val;
367
368         if (dev_priv->lvds_val)
369                 val = dev_priv->lvds_val;
370         else {
371                 /* BIOS should set the proper LVDS register value at boot, but
372                  * in reality, it doesn't set the value when the lid is closed;
373                  * we need to check "the value to be set" in VBT when LVDS
374                  * register is uninitialized.
375                  */
376                 val = I915_READ(reg);
377                 if (!(val & ~LVDS_DETECTED))
378                         val = dev_priv->bios_lvds_val;
379                 dev_priv->lvds_val = val;
380         }
381         return (val & LVDS_CLKB_POWER_MASK) == LVDS_CLKB_POWER_UP;
382 }
383
384 static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
385                                                 int refclk)
386 {
387         struct drm_device *dev = crtc->dev;
388         struct drm_i915_private *dev_priv = dev->dev_private;
389         const intel_limit_t *limit;
390
391         if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
392                 if (is_dual_link_lvds(dev_priv, PCH_LVDS)) {
393                         /* LVDS dual channel */
394                         if (refclk == 100000)
395                                 limit = &intel_limits_ironlake_dual_lvds_100m;
396                         else
397                                 limit = &intel_limits_ironlake_dual_lvds;
398                 } else {
399                         if (refclk == 100000)
400                                 limit = &intel_limits_ironlake_single_lvds_100m;
401                         else
402                                 limit = &intel_limits_ironlake_single_lvds;
403                 }
404         } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
405                         HAS_eDP)
406                 limit = &intel_limits_ironlake_display_port;
407         else
408                 limit = &intel_limits_ironlake_dac;
409
410         return limit;
411 }
412
413 static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
414 {
415         struct drm_device *dev = crtc->dev;
416         struct drm_i915_private *dev_priv = dev->dev_private;
417         const intel_limit_t *limit;
418
419         if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
420                 if (is_dual_link_lvds(dev_priv, LVDS))
421                         /* LVDS with dual channel */
422                         limit = &intel_limits_g4x_dual_channel_lvds;
423                 else
424                         /* LVDS with dual channel */
425                         limit = &intel_limits_g4x_single_channel_lvds;
426         } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
427                    intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
428                 limit = &intel_limits_g4x_hdmi;
429         } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
430                 limit = &intel_limits_g4x_sdvo;
431         } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
432                 limit = &intel_limits_g4x_display_port;
433         } else /* The option is for other outputs */
434                 limit = &intel_limits_i9xx_sdvo;
435
436         return limit;
437 }
438
439 static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
440 {
441         struct drm_device *dev = crtc->dev;
442         const intel_limit_t *limit;
443
444         if (HAS_PCH_SPLIT(dev))
445                 limit = intel_ironlake_limit(crtc, refclk);
446         else if (IS_G4X(dev)) {
447                 limit = intel_g4x_limit(crtc);
448         } else if (IS_PINEVIEW(dev)) {
449                 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
450                         limit = &intel_limits_pineview_lvds;
451                 else
452                         limit = &intel_limits_pineview_sdvo;
453         } else if (!IS_GEN2(dev)) {
454                 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
455                         limit = &intel_limits_i9xx_lvds;
456                 else
457                         limit = &intel_limits_i9xx_sdvo;
458         } else {
459                 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
460                         limit = &intel_limits_i8xx_lvds;
461                 else
462                         limit = &intel_limits_i8xx_dvo;
463         }
464         return limit;
465 }
466
467 /* m1 is reserved as 0 in Pineview, n is a ring counter */
468 static void pineview_clock(int refclk, intel_clock_t *clock)
469 {
470         clock->m = clock->m2 + 2;
471         clock->p = clock->p1 * clock->p2;
472         clock->vco = refclk * clock->m / clock->n;
473         clock->dot = clock->vco / clock->p;
474 }
475
476 static void intel_clock(struct drm_device *dev, int refclk, intel_clock_t *clock)
477 {
478         if (IS_PINEVIEW(dev)) {
479                 pineview_clock(refclk, clock);
480                 return;
481         }
482         clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
483         clock->p = clock->p1 * clock->p2;
484         clock->vco = refclk * clock->m / (clock->n + 2);
485         clock->dot = clock->vco / clock->p;
486 }
487
488 /**
489  * Returns whether any output on the specified pipe is of the specified type
490  */
491 bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
492 {
493         struct drm_device *dev = crtc->dev;
494         struct drm_mode_config *mode_config = &dev->mode_config;
495         struct intel_encoder *encoder;
496
497         list_for_each_entry(encoder, &mode_config->encoder_list, base.head)
498                 if (encoder->base.crtc == crtc && encoder->type == type)
499                         return true;
500
501         return false;
502 }
503
504 #define INTELPllInvalid(s)   do { /* DRM_DEBUG(s); */ return false; } while (0)
505 /**
506  * Returns whether the given set of divisors are valid for a given refclk with
507  * the given connectors.
508  */
509
510 static bool intel_PLL_is_valid(struct drm_device *dev,
511                                const intel_limit_t *limit,
512                                const intel_clock_t *clock)
513 {
514         if (clock->p1  < limit->p1.min  || limit->p1.max  < clock->p1)
515                 INTELPllInvalid("p1 out of range\n");
516         if (clock->p   < limit->p.min   || limit->p.max   < clock->p)
517                 INTELPllInvalid("p out of range\n");
518         if (clock->m2  < limit->m2.min  || limit->m2.max  < clock->m2)
519                 INTELPllInvalid("m2 out of range\n");
520         if (clock->m1  < limit->m1.min  || limit->m1.max  < clock->m1)
521                 INTELPllInvalid("m1 out of range\n");
522         if (clock->m1 <= clock->m2 && !IS_PINEVIEW(dev))
523                 INTELPllInvalid("m1 <= m2\n");
524         if (clock->m   < limit->m.min   || limit->m.max   < clock->m)
525                 INTELPllInvalid("m out of range\n");
526         if (clock->n   < limit->n.min   || limit->n.max   < clock->n)
527                 INTELPllInvalid("n out of range\n");
528         if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
529                 INTELPllInvalid("vco out of range\n");
530         /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
531          * connector, etc., rather than just a single range.
532          */
533         if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
534                 INTELPllInvalid("dot out of range\n");
535
536         return true;
537 }
538
539 static bool
540 intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
541                     int target, int refclk, intel_clock_t *match_clock,
542                     intel_clock_t *best_clock)
543
544 {
545         struct drm_device *dev = crtc->dev;
546         struct drm_i915_private *dev_priv = dev->dev_private;
547         intel_clock_t clock;
548         int err = target;
549
550         if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
551             (I915_READ(LVDS)) != 0) {
552                 /*
553                  * For LVDS, if the panel is on, just rely on its current
554                  * settings for dual-channel.  We haven't figured out how to
555                  * reliably set up different single/dual channel state, if we
556                  * even can.
557                  */
558                 if (is_dual_link_lvds(dev_priv, LVDS))
559                         clock.p2 = limit->p2.p2_fast;
560                 else
561                         clock.p2 = limit->p2.p2_slow;
562         } else {
563                 if (target < limit->p2.dot_limit)
564                         clock.p2 = limit->p2.p2_slow;
565                 else
566                         clock.p2 = limit->p2.p2_fast;
567         }
568
569         memset(best_clock, 0, sizeof(*best_clock));
570
571         for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
572              clock.m1++) {
573                 for (clock.m2 = limit->m2.min;
574                      clock.m2 <= limit->m2.max; clock.m2++) {
575                         /* m1 is always 0 in Pineview */
576                         if (clock.m2 >= clock.m1 && !IS_PINEVIEW(dev))
577                                 break;
578                         for (clock.n = limit->n.min;
579                              clock.n <= limit->n.max; clock.n++) {
580                                 for (clock.p1 = limit->p1.min;
581                                         clock.p1 <= limit->p1.max; clock.p1++) {
582                                         int this_err;
583
584                                         intel_clock(dev, refclk, &clock);
585                                         if (!intel_PLL_is_valid(dev, limit,
586                                                                 &clock))
587                                                 continue;
588                                         if (match_clock &&
589                                             clock.p != match_clock->p)
590                                                 continue;
591
592                                         this_err = abs(clock.dot - target);
593                                         if (this_err < err) {
594                                                 *best_clock = clock;
595                                                 err = this_err;
596                                         }
597                                 }
598                         }
599                 }
600         }
601
602         return (err != target);
603 }
604
605 static bool
606 intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
607                         int target, int refclk, intel_clock_t *match_clock,
608                         intel_clock_t *best_clock)
609 {
610         struct drm_device *dev = crtc->dev;
611         struct drm_i915_private *dev_priv = dev->dev_private;
612         intel_clock_t clock;
613         int max_n;
614         bool found;
615         /* approximately equals target * 0.00585 */
616         int err_most = (target >> 8) + (target >> 9);
617         found = false;
618
619         if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
620                 int lvds_reg;
621
622                 if (HAS_PCH_SPLIT(dev))
623                         lvds_reg = PCH_LVDS;
624                 else
625                         lvds_reg = LVDS;
626                 if ((I915_READ(lvds_reg) & LVDS_CLKB_POWER_MASK) ==
627                     LVDS_CLKB_POWER_UP)
628                         clock.p2 = limit->p2.p2_fast;
629                 else
630                         clock.p2 = limit->p2.p2_slow;
631         } else {
632                 if (target < limit->p2.dot_limit)
633                         clock.p2 = limit->p2.p2_slow;
634                 else
635                         clock.p2 = limit->p2.p2_fast;
636         }
637
638         memset(best_clock, 0, sizeof(*best_clock));
639         max_n = limit->n.max;
640         /* based on hardware requirement, prefer smaller n to precision */
641         for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
642                 /* based on hardware requirement, prefere larger m1,m2 */
643                 for (clock.m1 = limit->m1.max;
644                      clock.m1 >= limit->m1.min; clock.m1--) {
645                         for (clock.m2 = limit->m2.max;
646                              clock.m2 >= limit->m2.min; clock.m2--) {
647                                 for (clock.p1 = limit->p1.max;
648                                      clock.p1 >= limit->p1.min; clock.p1--) {
649                                         int this_err;
650
651                                         intel_clock(dev, refclk, &clock);
652                                         if (!intel_PLL_is_valid(dev, limit,
653                                                                 &clock))
654                                                 continue;
655                                         if (match_clock &&
656                                             clock.p != match_clock->p)
657                                                 continue;
658
659                                         this_err = abs(clock.dot - target);
660                                         if (this_err < err_most) {
661                                                 *best_clock = clock;
662                                                 err_most = this_err;
663                                                 max_n = clock.n;
664                                                 found = true;
665                                         }
666                                 }
667                         }
668                 }
669         }
670         return found;
671 }
672
673 static bool
674 intel_find_pll_ironlake_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
675                            int target, int refclk, intel_clock_t *match_clock,
676                            intel_clock_t *best_clock)
677 {
678         struct drm_device *dev = crtc->dev;
679         intel_clock_t clock;
680
681         if (target < 200000) {
682                 clock.n = 1;
683                 clock.p1 = 2;
684                 clock.p2 = 10;
685                 clock.m1 = 12;
686                 clock.m2 = 9;
687         } else {
688                 clock.n = 2;
689                 clock.p1 = 1;
690                 clock.p2 = 10;
691                 clock.m1 = 14;
692                 clock.m2 = 8;
693         }
694         intel_clock(dev, refclk, &clock);
695         memcpy(best_clock, &clock, sizeof(intel_clock_t));
696         return true;
697 }
698
699 /* DisplayPort has only two frequencies, 162MHz and 270MHz */
700 static bool
701 intel_find_pll_g4x_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
702                       int target, int refclk, intel_clock_t *match_clock,
703                       intel_clock_t *best_clock)
704 {
705         intel_clock_t clock;
706         if (target < 200000) {
707                 clock.p1 = 2;
708                 clock.p2 = 10;
709                 clock.n = 2;
710                 clock.m1 = 23;
711                 clock.m2 = 8;
712         } else {
713                 clock.p1 = 1;
714                 clock.p2 = 10;
715                 clock.n = 1;
716                 clock.m1 = 14;
717                 clock.m2 = 2;
718         }
719         clock.m = 5 * (clock.m1 + 2) + (clock.m2 + 2);
720         clock.p = (clock.p1 * clock.p2);
721         clock.dot = 96000 * clock.m / (clock.n + 2) / clock.p;
722         clock.vco = 0;
723         memcpy(best_clock, &clock, sizeof(intel_clock_t));
724         return true;
725 }
726
727 /**
728  * intel_wait_for_vblank - wait for vblank on a given pipe
729  * @dev: drm device
730  * @pipe: pipe to wait for
731  *
732  * Wait for vblank to occur on a given pipe.  Needed for various bits of
733  * mode setting code.
734  */
735 void intel_wait_for_vblank(struct drm_device *dev, int pipe)
736 {
737         struct drm_i915_private *dev_priv = dev->dev_private;
738         int pipestat_reg = PIPESTAT(pipe);
739
740         /* Clear existing vblank status. Note this will clear any other
741          * sticky status fields as well.
742          *
743          * This races with i915_driver_irq_handler() with the result
744          * that either function could miss a vblank event.  Here it is not
745          * fatal, as we will either wait upon the next vblank interrupt or
746          * timeout.  Generally speaking intel_wait_for_vblank() is only
747          * called during modeset at which time the GPU should be idle and
748          * should *not* be performing page flips and thus not waiting on
749          * vblanks...
750          * Currently, the result of us stealing a vblank from the irq
751          * handler is that a single frame will be skipped during swapbuffers.
752          */
753         I915_WRITE(pipestat_reg,
754                    I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
755
756         /* Wait for vblank interrupt bit to set */
757         if (wait_for(I915_READ(pipestat_reg) &
758                      PIPE_VBLANK_INTERRUPT_STATUS,
759                      50))
760                 DRM_DEBUG_KMS("vblank wait timed out\n");
761 }
762
763 /*
764  * intel_wait_for_pipe_off - wait for pipe to turn off
765  * @dev: drm device
766  * @pipe: pipe to wait for
767  *
768  * After disabling a pipe, we can't wait for vblank in the usual way,
769  * spinning on the vblank interrupt status bit, since we won't actually
770  * see an interrupt when the pipe is disabled.
771  *
772  * On Gen4 and above:
773  *   wait for the pipe register state bit to turn off
774  *
775  * Otherwise:
776  *   wait for the display line value to settle (it usually
777  *   ends up stopping at the start of the next frame).
778  *
779  */
780 void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
781 {
782         struct drm_i915_private *dev_priv = dev->dev_private;
783
784         if (INTEL_INFO(dev)->gen >= 4) {
785                 int reg = PIPECONF(pipe);
786
787                 /* Wait for the Pipe State to go off */
788                 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
789                              100))
790                         DRM_DEBUG_KMS("pipe_off wait timed out\n");
791         } else {
792                 u32 last_line;
793                 int reg = PIPEDSL(pipe);
794                 unsigned long timeout = jiffies + msecs_to_jiffies(100);
795
796                 /* Wait for the display line to settle */
797                 do {
798                         last_line = I915_READ(reg) & DSL_LINEMASK;
799                         mdelay(5);
800                 } while (((I915_READ(reg) & DSL_LINEMASK) != last_line) &&
801                          time_after(timeout, jiffies));
802                 if (time_after(jiffies, timeout))
803                         DRM_DEBUG_KMS("pipe_off wait timed out\n");
804         }
805 }
806
807 static const char *state_string(bool enabled)
808 {
809         return enabled ? "on" : "off";
810 }
811
812 /* Only for pre-ILK configs */
813 static void assert_pll(struct drm_i915_private *dev_priv,
814                        enum pipe pipe, bool state)
815 {
816         int reg;
817         u32 val;
818         bool cur_state;
819
820         reg = DPLL(pipe);
821         val = I915_READ(reg);
822         cur_state = !!(val & DPLL_VCO_ENABLE);
823         WARN(cur_state != state,
824              "PLL state assertion failure (expected %s, current %s)\n",
825              state_string(state), state_string(cur_state));
826 }
827 #define assert_pll_enabled(d, p) assert_pll(d, p, true)
828 #define assert_pll_disabled(d, p) assert_pll(d, p, false)
829
830 /* For ILK+ */
831 static void assert_pch_pll(struct drm_i915_private *dev_priv,
832                            enum pipe pipe, bool state)
833 {
834         int reg;
835         u32 val;
836         bool cur_state;
837
838         if (HAS_PCH_CPT(dev_priv->dev)) {
839                 u32 pch_dpll;
840
841                 pch_dpll = I915_READ(PCH_DPLL_SEL);
842
843                 /* Make sure the selected PLL is enabled to the transcoder */
844                 WARN(!((pch_dpll >> (4 * pipe)) & 8),
845                      "transcoder %d PLL not enabled\n", pipe);
846
847                 /* Convert the transcoder pipe number to a pll pipe number */
848                 pipe = (pch_dpll >> (4 * pipe)) & 1;
849         }
850
851         reg = PCH_DPLL(pipe);
852         val = I915_READ(reg);
853         cur_state = !!(val & DPLL_VCO_ENABLE);
854         WARN(cur_state != state,
855              "PCH PLL state assertion failure (expected %s, current %s)\n",
856              state_string(state), state_string(cur_state));
857 }
858 #define assert_pch_pll_enabled(d, p) assert_pch_pll(d, p, true)
859 #define assert_pch_pll_disabled(d, p) assert_pch_pll(d, p, false)
860
861 static void assert_fdi_tx(struct drm_i915_private *dev_priv,
862                           enum pipe pipe, bool state)
863 {
864         int reg;
865         u32 val;
866         bool cur_state;
867
868         reg = FDI_TX_CTL(pipe);
869         val = I915_READ(reg);
870         cur_state = !!(val & FDI_TX_ENABLE);
871         WARN(cur_state != state,
872              "FDI TX state assertion failure (expected %s, current %s)\n",
873              state_string(state), state_string(cur_state));
874 }
875 #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
876 #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
877
878 static void assert_fdi_rx(struct drm_i915_private *dev_priv,
879                           enum pipe pipe, bool state)
880 {
881         int reg;
882         u32 val;
883         bool cur_state;
884
885         reg = FDI_RX_CTL(pipe);
886         val = I915_READ(reg);
887         cur_state = !!(val & FDI_RX_ENABLE);
888         WARN(cur_state != state,
889              "FDI RX state assertion failure (expected %s, current %s)\n",
890              state_string(state), state_string(cur_state));
891 }
892 #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
893 #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
894
895 static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
896                                       enum pipe pipe)
897 {
898         int reg;
899         u32 val;
900
901         /* ILK FDI PLL is always enabled */
902         if (dev_priv->info->gen == 5)
903                 return;
904
905         reg = FDI_TX_CTL(pipe);
906         val = I915_READ(reg);
907         WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
908 }
909
910 static void assert_fdi_rx_pll_enabled(struct drm_i915_private *dev_priv,
911                                       enum pipe pipe)
912 {
913         int reg;
914         u32 val;
915
916         reg = FDI_RX_CTL(pipe);
917         val = I915_READ(reg);
918         WARN(!(val & FDI_RX_PLL_ENABLE), "FDI RX PLL assertion failure, should be active but is disabled\n");
919 }
920
921 static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
922                                   enum pipe pipe)
923 {
924         int pp_reg, lvds_reg;
925         u32 val;
926         enum pipe panel_pipe = PIPE_A;
927         bool locked = true;
928
929         if (HAS_PCH_SPLIT(dev_priv->dev)) {
930                 pp_reg = PCH_PP_CONTROL;
931                 lvds_reg = PCH_LVDS;
932         } else {
933                 pp_reg = PP_CONTROL;
934                 lvds_reg = LVDS;
935         }
936
937         val = I915_READ(pp_reg);
938         if (!(val & PANEL_POWER_ON) ||
939             ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
940                 locked = false;
941
942         if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
943                 panel_pipe = PIPE_B;
944
945         WARN(panel_pipe == pipe && locked,
946              "panel assertion failure, pipe %c regs locked\n",
947              pipe_name(pipe));
948 }
949
950 void assert_pipe(struct drm_i915_private *dev_priv,
951                  enum pipe pipe, bool state)
952 {
953         int reg;
954         u32 val;
955         bool cur_state;
956
957         /* if we need the pipe A quirk it must be always on */
958         if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
959                 state = true;
960
961         reg = PIPECONF(pipe);
962         val = I915_READ(reg);
963         cur_state = !!(val & PIPECONF_ENABLE);
964         WARN(cur_state != state,
965              "pipe %c assertion failure (expected %s, current %s)\n",
966              pipe_name(pipe), state_string(state), state_string(cur_state));
967 }
968
969 static void assert_plane(struct drm_i915_private *dev_priv,
970                          enum plane plane, bool state)
971 {
972         int reg;
973         u32 val;
974         bool cur_state;
975
976         reg = DSPCNTR(plane);
977         val = I915_READ(reg);
978         cur_state = !!(val & DISPLAY_PLANE_ENABLE);
979         WARN(cur_state != state,
980              "plane %c assertion failure (expected %s, current %s)\n",
981              plane_name(plane), state_string(state), state_string(cur_state));
982 }
983
984 #define assert_plane_enabled(d, p) assert_plane(d, p, true)
985 #define assert_plane_disabled(d, p) assert_plane(d, p, false)
986
987 static void assert_planes_disabled(struct drm_i915_private *dev_priv,
988                                    enum pipe pipe)
989 {
990         int reg, i;
991         u32 val;
992         int cur_pipe;
993
994         /* Planes are fixed to pipes on ILK+ */
995         if (HAS_PCH_SPLIT(dev_priv->dev)) {
996                 reg = DSPCNTR(pipe);
997                 val = I915_READ(reg);
998                 WARN((val & DISPLAY_PLANE_ENABLE),
999                      "plane %c assertion failure, should be disabled but not\n",
1000                      plane_name(pipe));
1001                 return;
1002         }
1003
1004         /* Need to check both planes against the pipe */
1005         for (i = 0; i < 2; i++) {
1006                 reg = DSPCNTR(i);
1007                 val = I915_READ(reg);
1008                 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1009                         DISPPLANE_SEL_PIPE_SHIFT;
1010                 WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
1011                      "plane %c assertion failure, should be off on pipe %c but is still active\n",
1012                      plane_name(i), pipe_name(pipe));
1013         }
1014 }
1015
1016 static void assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
1017 {
1018         u32 val;
1019         bool enabled;
1020
1021         val = I915_READ(PCH_DREF_CONTROL);
1022         enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1023                             DREF_SUPERSPREAD_SOURCE_MASK));
1024         WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
1025 }
1026
1027 static void assert_transcoder_disabled(struct drm_i915_private *dev_priv,
1028                                        enum pipe pipe)
1029 {
1030         int reg;
1031         u32 val;
1032         bool enabled;
1033
1034         reg = TRANSCONF(pipe);
1035         val = I915_READ(reg);
1036         enabled = !!(val & TRANS_ENABLE);
1037         WARN(enabled,
1038              "transcoder assertion failed, should be off on pipe %c but is still active\n",
1039              pipe_name(pipe));
1040 }
1041
1042 static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1043                             enum pipe pipe, u32 port_sel, u32 val)
1044 {
1045         if ((val & DP_PORT_EN) == 0)
1046                 return false;
1047
1048         if (HAS_PCH_CPT(dev_priv->dev)) {
1049                 u32     trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1050                 u32     trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1051                 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1052                         return false;
1053         } else {
1054                 if ((val & DP_PIPE_MASK) != (pipe << 30))
1055                         return false;
1056         }
1057         return true;
1058 }
1059
1060 static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1061                               enum pipe pipe, u32 val)
1062 {
1063         if ((val & PORT_ENABLE) == 0)
1064                 return false;
1065
1066         if (HAS_PCH_CPT(dev_priv->dev)) {
1067                 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1068                         return false;
1069         } else {
1070                 if ((val & TRANSCODER_MASK) != TRANSCODER(pipe))
1071                         return false;
1072         }
1073         return true;
1074 }
1075
1076 static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1077                               enum pipe pipe, u32 val)
1078 {
1079         if ((val & LVDS_PORT_EN) == 0)
1080                 return false;
1081
1082         if (HAS_PCH_CPT(dev_priv->dev)) {
1083                 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1084                         return false;
1085         } else {
1086                 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1087                         return false;
1088         }
1089         return true;
1090 }
1091
1092 static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1093                               enum pipe pipe, u32 val)
1094 {
1095         if ((val & ADPA_DAC_ENABLE) == 0)
1096                 return false;
1097         if (HAS_PCH_CPT(dev_priv->dev)) {
1098                 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1099                         return false;
1100         } else {
1101                 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1102                         return false;
1103         }
1104         return true;
1105 }
1106
1107 static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
1108                                    enum pipe pipe, int reg, u32 port_sel)
1109 {
1110         u32 val = I915_READ(reg);
1111         WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
1112              "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
1113              reg, pipe_name(pipe));
1114 }
1115
1116 static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1117                                      enum pipe pipe, int reg)
1118 {
1119         u32 val = I915_READ(reg);
1120         WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
1121              "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
1122              reg, pipe_name(pipe));
1123 }
1124
1125 static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1126                                       enum pipe pipe)
1127 {
1128         int reg;
1129         u32 val;
1130
1131         assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1132         assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1133         assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
1134
1135         reg = PCH_ADPA;
1136         val = I915_READ(reg);
1137         WARN(adpa_pipe_enabled(dev_priv, pipe, val),
1138              "PCH VGA enabled on transcoder %c, should be disabled\n",
1139              pipe_name(pipe));
1140
1141         reg = PCH_LVDS;
1142         val = I915_READ(reg);
1143         WARN(lvds_pipe_enabled(dev_priv, pipe, val),
1144              "PCH LVDS enabled on transcoder %c, should be disabled\n",
1145              pipe_name(pipe));
1146
1147         assert_pch_hdmi_disabled(dev_priv, pipe, HDMIB);
1148         assert_pch_hdmi_disabled(dev_priv, pipe, HDMIC);
1149         assert_pch_hdmi_disabled(dev_priv, pipe, HDMID);
1150 }
1151
1152 /**
1153  * intel_enable_pll - enable a PLL
1154  * @dev_priv: i915 private structure
1155  * @pipe: pipe PLL to enable
1156  *
1157  * Enable @pipe's PLL so we can start pumping pixels from a plane.  Check to
1158  * make sure the PLL reg is writable first though, since the panel write
1159  * protect mechanism may be enabled.
1160  *
1161  * Note!  This is for pre-ILK only.
1162  */
1163 static void intel_enable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1164 {
1165         int reg;
1166         u32 val;
1167
1168         /* No really, not for ILK+ */
1169         BUG_ON(dev_priv->info->gen >= 5);
1170
1171         /* PLL is protected by panel, make sure we can write it */
1172         if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
1173                 assert_panel_unlocked(dev_priv, pipe);
1174
1175         reg = DPLL(pipe);
1176         val = I915_READ(reg);
1177         val |= DPLL_VCO_ENABLE;
1178
1179         /* We do this three times for luck */
1180         I915_WRITE(reg, val);
1181         POSTING_READ(reg);
1182         udelay(150); /* wait for warmup */
1183         I915_WRITE(reg, val);
1184         POSTING_READ(reg);
1185         udelay(150); /* wait for warmup */
1186         I915_WRITE(reg, val);
1187         POSTING_READ(reg);
1188         udelay(150); /* wait for warmup */
1189 }
1190
1191 /**
1192  * intel_disable_pll - disable a PLL
1193  * @dev_priv: i915 private structure
1194  * @pipe: pipe PLL to disable
1195  *
1196  * Disable the PLL for @pipe, making sure the pipe is off first.
1197  *
1198  * Note!  This is for pre-ILK only.
1199  */
1200 static void intel_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1201 {
1202         int reg;
1203         u32 val;
1204
1205         /* Don't disable pipe A or pipe A PLLs if needed */
1206         if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1207                 return;
1208
1209         /* Make sure the pipe isn't still relying on us */
1210         assert_pipe_disabled(dev_priv, pipe);
1211
1212         reg = DPLL(pipe);
1213         val = I915_READ(reg);
1214         val &= ~DPLL_VCO_ENABLE;
1215         I915_WRITE(reg, val);
1216         POSTING_READ(reg);
1217 }
1218
1219 /**
1220  * intel_enable_pch_pll - enable PCH PLL
1221  * @dev_priv: i915 private structure
1222  * @pipe: pipe PLL to enable
1223  *
1224  * The PCH PLL needs to be enabled before the PCH transcoder, since it
1225  * drives the transcoder clock.
1226  */
1227 static void intel_enable_pch_pll(struct drm_i915_private *dev_priv,
1228                                  enum pipe pipe)
1229 {
1230         int reg;
1231         u32 val;
1232
1233         if (pipe > 1)
1234                 return;
1235
1236         /* PCH only available on ILK+ */
1237         BUG_ON(dev_priv->info->gen < 5);
1238
1239         /* PCH refclock must be enabled first */
1240         assert_pch_refclk_enabled(dev_priv);
1241
1242         reg = PCH_DPLL(pipe);
1243         val = I915_READ(reg);
1244         val |= DPLL_VCO_ENABLE;
1245         I915_WRITE(reg, val);
1246         POSTING_READ(reg);
1247         udelay(200);
1248 }
1249
1250 static void intel_disable_pch_pll(struct drm_i915_private *dev_priv,
1251                                   enum pipe pipe)
1252 {
1253         int reg;
1254         u32 val, pll_mask = TRANSC_DPLL_ENABLE | TRANSC_DPLLB_SEL,
1255                 pll_sel = TRANSC_DPLL_ENABLE;
1256
1257         if (pipe > 1)
1258                 return;
1259
1260         /* PCH only available on ILK+ */
1261         BUG_ON(dev_priv->info->gen < 5);
1262
1263         /* Make sure transcoder isn't still depending on us */
1264         assert_transcoder_disabled(dev_priv, pipe);
1265
1266         if (pipe == 0)
1267                 pll_sel |= TRANSC_DPLLA_SEL;
1268         else if (pipe == 1)
1269                 pll_sel |= TRANSC_DPLLB_SEL;
1270
1271
1272         if ((I915_READ(PCH_DPLL_SEL) & pll_mask) == pll_sel)
1273                 return;
1274
1275         reg = PCH_DPLL(pipe);
1276         val = I915_READ(reg);
1277         val &= ~DPLL_VCO_ENABLE;
1278         I915_WRITE(reg, val);
1279         POSTING_READ(reg);
1280         udelay(200);
1281 }
1282
1283 static void intel_enable_transcoder(struct drm_i915_private *dev_priv,
1284                                     enum pipe pipe)
1285 {
1286         int reg;
1287         u32 val, pipeconf_val;
1288         struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1289
1290         /* PCH only available on ILK+ */
1291         BUG_ON(dev_priv->info->gen < 5);
1292
1293         /* Make sure PCH DPLL is enabled */
1294         assert_pch_pll_enabled(dev_priv, pipe);
1295
1296         /* FDI must be feeding us bits for PCH ports */
1297         assert_fdi_tx_enabled(dev_priv, pipe);
1298         assert_fdi_rx_enabled(dev_priv, pipe);
1299
1300         reg = TRANSCONF(pipe);
1301         val = I915_READ(reg);
1302         pipeconf_val = I915_READ(PIPECONF(pipe));
1303
1304         if (HAS_PCH_IBX(dev_priv->dev)) {
1305                 /*
1306                  * make the BPC in transcoder be consistent with
1307                  * that in pipeconf reg.
1308                  */
1309                 val &= ~PIPE_BPC_MASK;
1310                 val |= pipeconf_val & PIPE_BPC_MASK;
1311         }
1312
1313         val &= ~TRANS_INTERLACE_MASK;
1314         if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
1315                 if (HAS_PCH_IBX(dev_priv->dev) &&
1316                     intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
1317                         val |= TRANS_LEGACY_INTERLACED_ILK;
1318                 else
1319                         val |= TRANS_INTERLACED;
1320         else
1321                 val |= TRANS_PROGRESSIVE;
1322
1323         I915_WRITE(reg, val | TRANS_ENABLE);
1324         if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
1325                 DRM_ERROR("failed to enable transcoder %d\n", pipe);
1326 }
1327
1328 static void intel_disable_transcoder(struct drm_i915_private *dev_priv,
1329                                      enum pipe pipe)
1330 {
1331         int reg;
1332         u32 val;
1333
1334         /* FDI relies on the transcoder */
1335         assert_fdi_tx_disabled(dev_priv, pipe);
1336         assert_fdi_rx_disabled(dev_priv, pipe);
1337
1338         /* Ports must be off as well */
1339         assert_pch_ports_disabled(dev_priv, pipe);
1340
1341         reg = TRANSCONF(pipe);
1342         val = I915_READ(reg);
1343         val &= ~TRANS_ENABLE;
1344         I915_WRITE(reg, val);
1345         /* wait for PCH transcoder off, transcoder state */
1346         if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
1347                 DRM_ERROR("failed to disable transcoder %d\n", pipe);
1348 }
1349
1350 /**
1351  * intel_enable_pipe - enable a pipe, asserting requirements
1352  * @dev_priv: i915 private structure
1353  * @pipe: pipe to enable
1354  * @pch_port: on ILK+, is this pipe driving a PCH port or not
1355  *
1356  * Enable @pipe, making sure that various hardware specific requirements
1357  * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
1358  *
1359  * @pipe should be %PIPE_A or %PIPE_B.
1360  *
1361  * Will wait until the pipe is actually running (i.e. first vblank) before
1362  * returning.
1363  */
1364 static void intel_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe,
1365                               bool pch_port)
1366 {
1367         int reg;
1368         u32 val;
1369
1370         /*
1371          * A pipe without a PLL won't actually be able to drive bits from
1372          * a plane.  On ILK+ the pipe PLLs are integrated, so we don't
1373          * need the check.
1374          */
1375         if (!HAS_PCH_SPLIT(dev_priv->dev))
1376                 assert_pll_enabled(dev_priv, pipe);
1377         else {
1378                 if (pch_port) {
1379                         /* if driving the PCH, we need FDI enabled */
1380                         assert_fdi_rx_pll_enabled(dev_priv, pipe);
1381                         assert_fdi_tx_pll_enabled(dev_priv, pipe);
1382                 }
1383                 /* FIXME: assert CPU port conditions for SNB+ */
1384         }
1385
1386         reg = PIPECONF(pipe);
1387         val = I915_READ(reg);
1388         if (val & PIPECONF_ENABLE)
1389                 return;
1390
1391         I915_WRITE(reg, val | PIPECONF_ENABLE);
1392         intel_wait_for_vblank(dev_priv->dev, pipe);
1393 }
1394
1395 /**
1396  * intel_disable_pipe - disable a pipe, asserting requirements
1397  * @dev_priv: i915 private structure
1398  * @pipe: pipe to disable
1399  *
1400  * Disable @pipe, making sure that various hardware specific requirements
1401  * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
1402  *
1403  * @pipe should be %PIPE_A or %PIPE_B.
1404  *
1405  * Will wait until the pipe has shut down before returning.
1406  */
1407 static void intel_disable_pipe(struct drm_i915_private *dev_priv,
1408                                enum pipe pipe)
1409 {
1410         int reg;
1411         u32 val;
1412
1413         /*
1414          * Make sure planes won't keep trying to pump pixels to us,
1415          * or we might hang the display.
1416          */
1417         assert_planes_disabled(dev_priv, pipe);
1418
1419         /* Don't disable pipe A or pipe A PLLs if needed */
1420         if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1421                 return;
1422
1423         reg = PIPECONF(pipe);
1424         val = I915_READ(reg);
1425         if ((val & PIPECONF_ENABLE) == 0)
1426                 return;
1427
1428         I915_WRITE(reg, val & ~PIPECONF_ENABLE);
1429         intel_wait_for_pipe_off(dev_priv->dev, pipe);
1430 }
1431
1432 /*
1433  * Plane regs are double buffered, going from enabled->disabled needs a
1434  * trigger in order to latch.  The display address reg provides this.
1435  */
1436 static void intel_flush_display_plane(struct drm_i915_private *dev_priv,
1437                                       enum plane plane)
1438 {
1439         I915_WRITE(DSPADDR(plane), I915_READ(DSPADDR(plane)));
1440         I915_WRITE(DSPSURF(plane), I915_READ(DSPSURF(plane)));
1441 }
1442
1443 /**
1444  * intel_enable_plane - enable a display plane on a given pipe
1445  * @dev_priv: i915 private structure
1446  * @plane: plane to enable
1447  * @pipe: pipe being fed
1448  *
1449  * Enable @plane on @pipe, making sure that @pipe is running first.
1450  */
1451 static void intel_enable_plane(struct drm_i915_private *dev_priv,
1452                                enum plane plane, enum pipe pipe)
1453 {
1454         int reg;
1455         u32 val;
1456
1457         /* If the pipe isn't enabled, we can't pump pixels and may hang */
1458         assert_pipe_enabled(dev_priv, pipe);
1459
1460         reg = DSPCNTR(plane);
1461         val = I915_READ(reg);
1462         if (val & DISPLAY_PLANE_ENABLE)
1463                 return;
1464
1465         I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
1466         intel_flush_display_plane(dev_priv, plane);
1467         intel_wait_for_vblank(dev_priv->dev, pipe);
1468 }
1469
1470 /**
1471  * intel_disable_plane - disable a display plane
1472  * @dev_priv: i915 private structure
1473  * @plane: plane to disable
1474  * @pipe: pipe consuming the data
1475  *
1476  * Disable @plane; should be an independent operation.
1477  */
1478 static void intel_disable_plane(struct drm_i915_private *dev_priv,
1479                                 enum plane plane, enum pipe pipe)
1480 {
1481         int reg;
1482         u32 val;
1483
1484         reg = DSPCNTR(plane);
1485         val = I915_READ(reg);
1486         if ((val & DISPLAY_PLANE_ENABLE) == 0)
1487                 return;
1488
1489         I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
1490         intel_flush_display_plane(dev_priv, plane);
1491         intel_wait_for_vblank(dev_priv->dev, pipe);
1492 }
1493
1494 static void disable_pch_dp(struct drm_i915_private *dev_priv,
1495                            enum pipe pipe, int reg, u32 port_sel)
1496 {
1497         u32 val = I915_READ(reg);
1498         if (dp_pipe_enabled(dev_priv, pipe, port_sel, val)) {
1499                 DRM_DEBUG_KMS("Disabling pch dp %x on pipe %d\n", reg, pipe);
1500                 I915_WRITE(reg, val & ~DP_PORT_EN);
1501         }
1502 }
1503
1504 static void disable_pch_hdmi(struct drm_i915_private *dev_priv,
1505                              enum pipe pipe, int reg)
1506 {
1507         u32 val = I915_READ(reg);
1508         if (hdmi_pipe_enabled(dev_priv, pipe, val)) {
1509                 DRM_DEBUG_KMS("Disabling pch HDMI %x on pipe %d\n",
1510                               reg, pipe);
1511                 I915_WRITE(reg, val & ~PORT_ENABLE);
1512         }
1513 }
1514
1515 /* Disable any ports connected to this transcoder */
1516 static void intel_disable_pch_ports(struct drm_i915_private *dev_priv,
1517                                     enum pipe pipe)
1518 {
1519         u32 reg, val;
1520
1521         val = I915_READ(PCH_PP_CONTROL);
1522         I915_WRITE(PCH_PP_CONTROL, val | PANEL_UNLOCK_REGS);
1523
1524         disable_pch_dp(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1525         disable_pch_dp(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1526         disable_pch_dp(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
1527
1528         reg = PCH_ADPA;
1529         val = I915_READ(reg);
1530         if (adpa_pipe_enabled(dev_priv, pipe, val))
1531                 I915_WRITE(reg, val & ~ADPA_DAC_ENABLE);
1532
1533         reg = PCH_LVDS;
1534         val = I915_READ(reg);
1535         if (lvds_pipe_enabled(dev_priv, pipe, val)) {
1536                 DRM_DEBUG_KMS("disable lvds on pipe %d val 0x%08x\n", pipe, val);
1537                 I915_WRITE(reg, val & ~LVDS_PORT_EN);
1538                 POSTING_READ(reg);
1539                 udelay(100);
1540         }
1541
1542         disable_pch_hdmi(dev_priv, pipe, HDMIB);
1543         disable_pch_hdmi(dev_priv, pipe, HDMIC);
1544         disable_pch_hdmi(dev_priv, pipe, HDMID);
1545 }
1546
1547 static void i8xx_disable_fbc(struct drm_device *dev)
1548 {
1549         struct drm_i915_private *dev_priv = dev->dev_private;
1550         u32 fbc_ctl;
1551
1552         /* Disable compression */
1553         fbc_ctl = I915_READ(FBC_CONTROL);
1554         if ((fbc_ctl & FBC_CTL_EN) == 0)
1555                 return;
1556
1557         fbc_ctl &= ~FBC_CTL_EN;
1558         I915_WRITE(FBC_CONTROL, fbc_ctl);
1559
1560         /* Wait for compressing bit to clear */
1561         if (wait_for((I915_READ(FBC_STATUS) & FBC_STAT_COMPRESSING) == 0, 10)) {
1562                 DRM_DEBUG_KMS("FBC idle timed out\n");
1563                 return;
1564         }
1565
1566         DRM_DEBUG_KMS("disabled FBC\n");
1567 }
1568
1569 static void i8xx_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
1570 {
1571         struct drm_device *dev = crtc->dev;
1572         struct drm_i915_private *dev_priv = dev->dev_private;
1573         struct drm_framebuffer *fb = crtc->fb;
1574         struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
1575         struct drm_i915_gem_object *obj = intel_fb->obj;
1576         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1577         int cfb_pitch;
1578         int plane, i;
1579         u32 fbc_ctl, fbc_ctl2;
1580
1581         cfb_pitch = dev_priv->cfb_size / FBC_LL_SIZE;
1582         if (fb->pitches[0] < cfb_pitch)
1583                 cfb_pitch = fb->pitches[0];
1584
1585         /* FBC_CTL wants 64B units */
1586         cfb_pitch = (cfb_pitch / 64) - 1;
1587         plane = intel_crtc->plane == 0 ? FBC_CTL_PLANEA : FBC_CTL_PLANEB;
1588
1589         /* Clear old tags */
1590         for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++)
1591                 I915_WRITE(FBC_TAG + (i * 4), 0);
1592
1593         /* Set it up... */
1594         fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM | FBC_CTL_CPU_FENCE;
1595         fbc_ctl2 |= plane;
1596         I915_WRITE(FBC_CONTROL2, fbc_ctl2);
1597         I915_WRITE(FBC_FENCE_OFF, crtc->y);
1598
1599         /* enable it... */
1600         fbc_ctl = FBC_CTL_EN | FBC_CTL_PERIODIC;
1601         if (IS_I945GM(dev))
1602                 fbc_ctl |= FBC_CTL_C3_IDLE; /* 945 needs special SR handling */
1603         fbc_ctl |= (cfb_pitch & 0xff) << FBC_CTL_STRIDE_SHIFT;
1604         fbc_ctl |= (interval & 0x2fff) << FBC_CTL_INTERVAL_SHIFT;
1605         fbc_ctl |= obj->fence_reg;
1606         I915_WRITE(FBC_CONTROL, fbc_ctl);
1607
1608         DRM_DEBUG_KMS("enabled FBC, pitch %d, yoff %d, plane %d, ",
1609                       cfb_pitch, crtc->y, intel_crtc->plane);
1610 }
1611
1612 static bool i8xx_fbc_enabled(struct drm_device *dev)
1613 {
1614         struct drm_i915_private *dev_priv = dev->dev_private;
1615
1616         return I915_READ(FBC_CONTROL) & FBC_CTL_EN;
1617 }
1618
1619 static void g4x_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
1620 {
1621         struct drm_device *dev = crtc->dev;
1622         struct drm_i915_private *dev_priv = dev->dev_private;
1623         struct drm_framebuffer *fb = crtc->fb;
1624         struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
1625         struct drm_i915_gem_object *obj = intel_fb->obj;
1626         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1627         int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
1628         unsigned long stall_watermark = 200;
1629         u32 dpfc_ctl;
1630
1631         dpfc_ctl = plane | DPFC_SR_EN | DPFC_CTL_LIMIT_1X;
1632         dpfc_ctl |= DPFC_CTL_FENCE_EN | obj->fence_reg;
1633         I915_WRITE(DPFC_CHICKEN, DPFC_HT_MODIFY);
1634
1635         I915_WRITE(DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
1636                    (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
1637                    (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
1638         I915_WRITE(DPFC_FENCE_YOFF, crtc->y);
1639
1640         /* enable it... */
1641         I915_WRITE(DPFC_CONTROL, I915_READ(DPFC_CONTROL) | DPFC_CTL_EN);
1642
1643         DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
1644 }
1645
1646 static void g4x_disable_fbc(struct drm_device *dev)
1647 {
1648         struct drm_i915_private *dev_priv = dev->dev_private;
1649         u32 dpfc_ctl;
1650
1651         /* Disable compression */
1652         dpfc_ctl = I915_READ(DPFC_CONTROL);
1653         if (dpfc_ctl & DPFC_CTL_EN) {
1654                 dpfc_ctl &= ~DPFC_CTL_EN;
1655                 I915_WRITE(DPFC_CONTROL, dpfc_ctl);
1656
1657                 DRM_DEBUG_KMS("disabled FBC\n");
1658         }
1659 }
1660
1661 static bool g4x_fbc_enabled(struct drm_device *dev)
1662 {
1663         struct drm_i915_private *dev_priv = dev->dev_private;
1664
1665         return I915_READ(DPFC_CONTROL) & DPFC_CTL_EN;
1666 }
1667
1668 static void sandybridge_blit_fbc_update(struct drm_device *dev)
1669 {
1670         struct drm_i915_private *dev_priv = dev->dev_private;
1671         u32 blt_ecoskpd;
1672
1673         /* Make sure blitter notifies FBC of writes */
1674         gen6_gt_force_wake_get(dev_priv);
1675         blt_ecoskpd = I915_READ(GEN6_BLITTER_ECOSKPD);
1676         blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY <<
1677                 GEN6_BLITTER_LOCK_SHIFT;
1678         I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
1679         blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY;
1680         I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
1681         blt_ecoskpd &= ~(GEN6_BLITTER_FBC_NOTIFY <<
1682                          GEN6_BLITTER_LOCK_SHIFT);
1683         I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
1684         POSTING_READ(GEN6_BLITTER_ECOSKPD);
1685         gen6_gt_force_wake_put(dev_priv);
1686 }
1687
1688 static void ironlake_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
1689 {
1690         struct drm_device *dev = crtc->dev;
1691         struct drm_i915_private *dev_priv = dev->dev_private;
1692         struct drm_framebuffer *fb = crtc->fb;
1693         struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
1694         struct drm_i915_gem_object *obj = intel_fb->obj;
1695         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1696         int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
1697         unsigned long stall_watermark = 200;
1698         u32 dpfc_ctl;
1699
1700         dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
1701         dpfc_ctl &= DPFC_RESERVED;
1702         dpfc_ctl |= (plane | DPFC_CTL_LIMIT_1X);
1703         /* Set persistent mode for front-buffer rendering, ala X. */
1704         dpfc_ctl |= DPFC_CTL_PERSISTENT_MODE;
1705         dpfc_ctl |= (DPFC_CTL_FENCE_EN | obj->fence_reg);
1706         I915_WRITE(ILK_DPFC_CHICKEN, DPFC_HT_MODIFY);
1707
1708         I915_WRITE(ILK_DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
1709                    (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
1710                    (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
1711         I915_WRITE(ILK_DPFC_FENCE_YOFF, crtc->y);
1712         I915_WRITE(ILK_FBC_RT_BASE, obj->gtt_offset | ILK_FBC_RT_VALID);
1713         /* enable it... */
1714         I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
1715
1716         if (IS_GEN6(dev)) {
1717                 I915_WRITE(SNB_DPFC_CTL_SA,
1718                            SNB_CPU_FENCE_ENABLE | obj->fence_reg);
1719                 I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y);
1720                 sandybridge_blit_fbc_update(dev);
1721         }
1722
1723         DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
1724 }
1725
1726 static void ironlake_disable_fbc(struct drm_device *dev)
1727 {
1728         struct drm_i915_private *dev_priv = dev->dev_private;
1729         u32 dpfc_ctl;
1730
1731         /* Disable compression */
1732         dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
1733         if (dpfc_ctl & DPFC_CTL_EN) {
1734                 dpfc_ctl &= ~DPFC_CTL_EN;
1735                 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl);
1736
1737                 DRM_DEBUG_KMS("disabled FBC\n");
1738         }
1739 }
1740
1741 static bool ironlake_fbc_enabled(struct drm_device *dev)
1742 {
1743         struct drm_i915_private *dev_priv = dev->dev_private;
1744
1745         return I915_READ(ILK_DPFC_CONTROL) & DPFC_CTL_EN;
1746 }
1747
1748 bool intel_fbc_enabled(struct drm_device *dev)
1749 {
1750         struct drm_i915_private *dev_priv = dev->dev_private;
1751
1752         if (!dev_priv->display.fbc_enabled)
1753                 return false;
1754
1755         return dev_priv->display.fbc_enabled(dev);
1756 }
1757
1758 static void intel_fbc_work_fn(struct work_struct *__work)
1759 {
1760         struct intel_fbc_work *work =
1761                 container_of(to_delayed_work(__work),
1762                              struct intel_fbc_work, work);
1763         struct drm_device *dev = work->crtc->dev;
1764         struct drm_i915_private *dev_priv = dev->dev_private;
1765
1766         mutex_lock(&dev->struct_mutex);
1767         if (work == dev_priv->fbc_work) {
1768                 /* Double check that we haven't switched fb without cancelling
1769                  * the prior work.
1770                  */
1771                 if (work->crtc->fb == work->fb) {
1772                         dev_priv->display.enable_fbc(work->crtc,
1773                                                      work->interval);
1774
1775                         dev_priv->cfb_plane = to_intel_crtc(work->crtc)->plane;
1776                         dev_priv->cfb_fb = work->crtc->fb->base.id;
1777                         dev_priv->cfb_y = work->crtc->y;
1778                 }
1779
1780                 dev_priv->fbc_work = NULL;
1781         }
1782         mutex_unlock(&dev->struct_mutex);
1783
1784         kfree(work);
1785 }
1786
1787 static void intel_cancel_fbc_work(struct drm_i915_private *dev_priv)
1788 {
1789         if (dev_priv->fbc_work == NULL)
1790                 return;
1791
1792         DRM_DEBUG_KMS("cancelling pending FBC enable\n");
1793
1794         /* Synchronisation is provided by struct_mutex and checking of
1795          * dev_priv->fbc_work, so we can perform the cancellation
1796          * entirely asynchronously.
1797          */
1798         if (cancel_delayed_work(&dev_priv->fbc_work->work))
1799                 /* tasklet was killed before being run, clean up */
1800                 kfree(dev_priv->fbc_work);
1801
1802         /* Mark the work as no longer wanted so that if it does
1803          * wake-up (because the work was already running and waiting
1804          * for our mutex), it will discover that is no longer
1805          * necessary to run.
1806          */
1807         dev_priv->fbc_work = NULL;
1808 }
1809
1810 static void intel_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
1811 {
1812         struct intel_fbc_work *work;
1813         struct drm_device *dev = crtc->dev;
1814         struct drm_i915_private *dev_priv = dev->dev_private;
1815
1816         if (!dev_priv->display.enable_fbc)
1817                 return;
1818
1819         intel_cancel_fbc_work(dev_priv);
1820
1821         work = kzalloc(sizeof *work, GFP_KERNEL);
1822         if (work == NULL) {
1823                 dev_priv->display.enable_fbc(crtc, interval);
1824                 return;
1825         }
1826
1827         work->crtc = crtc;
1828         work->fb = crtc->fb;
1829         work->interval = interval;
1830         INIT_DELAYED_WORK(&work->work, intel_fbc_work_fn);
1831
1832         dev_priv->fbc_work = work;
1833
1834         DRM_DEBUG_KMS("scheduling delayed FBC enable\n");
1835
1836         /* Delay the actual enabling to let pageflipping cease and the
1837          * display to settle before starting the compression. Note that
1838          * this delay also serves a second purpose: it allows for a
1839          * vblank to pass after disabling the FBC before we attempt
1840          * to modify the control registers.
1841          *
1842          * A more complicated solution would involve tracking vblanks
1843          * following the termination of the page-flipping sequence
1844          * and indeed performing the enable as a co-routine and not
1845          * waiting synchronously upon the vblank.
1846          */
1847         schedule_delayed_work(&work->work, msecs_to_jiffies(50));
1848 }
1849
1850 void intel_disable_fbc(struct drm_device *dev)
1851 {
1852         struct drm_i915_private *dev_priv = dev->dev_private;
1853
1854         intel_cancel_fbc_work(dev_priv);
1855
1856         if (!dev_priv->display.disable_fbc)
1857                 return;
1858
1859         dev_priv->display.disable_fbc(dev);
1860         dev_priv->cfb_plane = -1;
1861 }
1862
1863 /**
1864  * intel_update_fbc - enable/disable FBC as needed
1865  * @dev: the drm_device
1866  *
1867  * Set up the framebuffer compression hardware at mode set time.  We
1868  * enable it if possible:
1869  *   - plane A only (on pre-965)
1870  *   - no pixel mulitply/line duplication
1871  *   - no alpha buffer discard
1872  *   - no dual wide
1873  *   - framebuffer <= 2048 in width, 1536 in height
1874  *
1875  * We can't assume that any compression will take place (worst case),
1876  * so the compressed buffer has to be the same size as the uncompressed
1877  * one.  It also must reside (along with the line length buffer) in
1878  * stolen memory.
1879  *
1880  * We need to enable/disable FBC on a global basis.
1881  */
1882 static void intel_update_fbc(struct drm_device *dev)
1883 {
1884         struct drm_i915_private *dev_priv = dev->dev_private;
1885         struct drm_crtc *crtc = NULL, *tmp_crtc;
1886         struct intel_crtc *intel_crtc;
1887         struct drm_framebuffer *fb;
1888         struct intel_framebuffer *intel_fb;
1889         struct drm_i915_gem_object *obj;
1890         int enable_fbc;
1891
1892         DRM_DEBUG_KMS("\n");
1893
1894         if (!i915_powersave)
1895                 return;
1896
1897         if (!I915_HAS_FBC(dev))
1898                 return;
1899
1900         /*
1901          * If FBC is already on, we just have to verify that we can
1902          * keep it that way...
1903          * Need to disable if:
1904          *   - more than one pipe is active
1905          *   - changing FBC params (stride, fence, mode)
1906          *   - new fb is too large to fit in compressed buffer
1907          *   - going to an unsupported config (interlace, pixel multiply, etc.)
1908          */
1909         list_for_each_entry(tmp_crtc, &dev->mode_config.crtc_list, head) {
1910                 if (tmp_crtc->enabled && tmp_crtc->fb) {
1911                         if (crtc) {
1912                                 DRM_DEBUG_KMS("more than one pipe active, disabling compression\n");
1913                                 dev_priv->no_fbc_reason = FBC_MULTIPLE_PIPES;
1914                                 goto out_disable;
1915                         }
1916                         crtc = tmp_crtc;
1917                 }
1918         }
1919
1920         if (!crtc || crtc->fb == NULL) {
1921                 DRM_DEBUG_KMS("no output, disabling\n");
1922                 dev_priv->no_fbc_reason = FBC_NO_OUTPUT;
1923                 goto out_disable;
1924         }
1925
1926         intel_crtc = to_intel_crtc(crtc);
1927         fb = crtc->fb;
1928         intel_fb = to_intel_framebuffer(fb);
1929         obj = intel_fb->obj;
1930
1931         enable_fbc = i915_enable_fbc;
1932         if (enable_fbc < 0) {
1933                 DRM_DEBUG_KMS("fbc set to per-chip default\n");
1934                 enable_fbc = 1;
1935                 if (INTEL_INFO(dev)->gen <= 6)
1936                         enable_fbc = 0;
1937         }
1938         if (!enable_fbc) {
1939                 DRM_DEBUG_KMS("fbc disabled per module param\n");
1940                 dev_priv->no_fbc_reason = FBC_MODULE_PARAM;
1941                 goto out_disable;
1942         }
1943         if (intel_fb->obj->base.size > dev_priv->cfb_size) {
1944                 DRM_DEBUG_KMS("framebuffer too large, disabling "
1945                               "compression\n");
1946                 dev_priv->no_fbc_reason = FBC_STOLEN_TOO_SMALL;
1947                 goto out_disable;
1948         }
1949         if ((crtc->mode.flags & DRM_MODE_FLAG_INTERLACE) ||
1950             (crtc->mode.flags & DRM_MODE_FLAG_DBLSCAN)) {
1951                 DRM_DEBUG_KMS("mode incompatible with compression, "
1952                               "disabling\n");
1953                 dev_priv->no_fbc_reason = FBC_UNSUPPORTED_MODE;
1954                 goto out_disable;
1955         }
1956         if ((crtc->mode.hdisplay > 2048) ||
1957             (crtc->mode.vdisplay > 1536)) {
1958                 DRM_DEBUG_KMS("mode too large for compression, disabling\n");
1959                 dev_priv->no_fbc_reason = FBC_MODE_TOO_LARGE;
1960                 goto out_disable;
1961         }
1962         if ((IS_I915GM(dev) || IS_I945GM(dev)) && intel_crtc->plane != 0) {
1963                 DRM_DEBUG_KMS("plane not 0, disabling compression\n");
1964                 dev_priv->no_fbc_reason = FBC_BAD_PLANE;
1965                 goto out_disable;
1966         }
1967
1968         /* The use of a CPU fence is mandatory in order to detect writes
1969          * by the CPU to the scanout and trigger updates to the FBC.
1970          */
1971         if (obj->tiling_mode != I915_TILING_X ||
1972             obj->fence_reg == I915_FENCE_REG_NONE) {
1973                 DRM_DEBUG_KMS("framebuffer not tiled or fenced, disabling compression\n");
1974                 dev_priv->no_fbc_reason = FBC_NOT_TILED;
1975                 goto out_disable;
1976         }
1977
1978         /* If the kernel debugger is active, always disable compression */
1979         if (in_dbg_master())
1980                 goto out_disable;
1981
1982         /* If the scanout has not changed, don't modify the FBC settings.
1983          * Note that we make the fundamental assumption that the fb->obj
1984          * cannot be unpinned (and have its GTT offset and fence revoked)
1985          * without first being decoupled from the scanout and FBC disabled.
1986          */
1987         if (dev_priv->cfb_plane == intel_crtc->plane &&
1988             dev_priv->cfb_fb == fb->base.id &&
1989             dev_priv->cfb_y == crtc->y)
1990                 return;
1991
1992         if (intel_fbc_enabled(dev)) {
1993                 /* We update FBC along two paths, after changing fb/crtc
1994                  * configuration (modeswitching) and after page-flipping
1995                  * finishes. For the latter, we know that not only did
1996                  * we disable the FBC at the start of the page-flip
1997                  * sequence, but also more than one vblank has passed.
1998                  *
1999                  * For the former case of modeswitching, it is possible
2000                  * to switch between two FBC valid configurations
2001                  * instantaneously so we do need to disable the FBC
2002                  * before we can modify its control registers. We also
2003                  * have to wait for the next vblank for that to take
2004                  * effect. However, since we delay enabling FBC we can
2005                  * assume that a vblank has passed since disabling and
2006                  * that we can safely alter the registers in the deferred
2007                  * callback.
2008                  *
2009                  * In the scenario that we go from a valid to invalid
2010                  * and then back to valid FBC configuration we have
2011                  * no strict enforcement that a vblank occurred since
2012                  * disabling the FBC. However, along all current pipe
2013                  * disabling paths we do need to wait for a vblank at
2014                  * some point. And we wait before enabling FBC anyway.
2015                  */
2016                 DRM_DEBUG_KMS("disabling active FBC for update\n");
2017                 intel_disable_fbc(dev);
2018         }
2019
2020         intel_enable_fbc(crtc, 500);
2021         return;
2022
2023 out_disable:
2024         /* Multiple disables should be harmless */
2025         if (intel_fbc_enabled(dev)) {
2026                 DRM_DEBUG_KMS("unsupported config, disabling FBC\n");
2027                 intel_disable_fbc(dev);
2028         }
2029 }
2030
2031 int
2032 intel_pin_and_fence_fb_obj(struct drm_device *dev,
2033                            struct drm_i915_gem_object *obj,
2034                            struct intel_ring_buffer *pipelined)
2035 {
2036         struct drm_i915_private *dev_priv = dev->dev_private;
2037         u32 alignment;
2038         int ret;
2039
2040         switch (obj->tiling_mode) {
2041         case I915_TILING_NONE:
2042                 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
2043                         alignment = 128 * 1024;
2044                 else if (INTEL_INFO(dev)->gen >= 4)
2045                         alignment = 4 * 1024;
2046                 else
2047                         alignment = 64 * 1024;
2048                 break;
2049         case I915_TILING_X:
2050                 /* pin() will align the object as required by fence */
2051                 alignment = 0;
2052                 break;
2053         case I915_TILING_Y:
2054                 /* FIXME: Is this true? */
2055                 DRM_ERROR("Y tiled not allowed for scan out buffers\n");
2056                 return -EINVAL;
2057         default:
2058                 BUG();
2059         }
2060
2061         dev_priv->mm.interruptible = false;
2062         ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
2063         if (ret)
2064                 goto err_interruptible;
2065
2066         /* Install a fence for tiled scan-out. Pre-i965 always needs a
2067          * fence, whereas 965+ only requires a fence if using
2068          * framebuffer compression.  For simplicity, we always install
2069          * a fence as the cost is not that onerous.
2070          */
2071         if (obj->tiling_mode != I915_TILING_NONE) {
2072                 ret = i915_gem_object_get_fence(obj, pipelined);
2073                 if (ret)
2074                         goto err_unpin;
2075
2076                 i915_gem_object_pin_fence(obj);
2077         }
2078
2079         dev_priv->mm.interruptible = true;
2080         return 0;
2081
2082 err_unpin:
2083         i915_gem_object_unpin(obj);
2084 err_interruptible:
2085         dev_priv->mm.interruptible = true;
2086         return ret;
2087 }
2088
2089 void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
2090 {
2091         i915_gem_object_unpin_fence(obj);
2092         i915_gem_object_unpin(obj);
2093 }
2094
2095 static int i9xx_update_plane(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2096                              int x, int y)
2097 {
2098         struct drm_device *dev = crtc->dev;
2099         struct drm_i915_private *dev_priv = dev->dev_private;
2100         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2101         struct intel_framebuffer *intel_fb;
2102         struct drm_i915_gem_object *obj;
2103         int plane = intel_crtc->plane;
2104         unsigned long Start, Offset;
2105         u32 dspcntr;
2106         u32 reg;
2107
2108         switch (plane) {
2109         case 0:
2110         case 1:
2111                 break;
2112         default:
2113                 DRM_ERROR("Can't update plane %d in SAREA\n", plane);
2114                 return -EINVAL;
2115         }
2116
2117         intel_fb = to_intel_framebuffer(fb);
2118         obj = intel_fb->obj;
2119
2120         reg = DSPCNTR(plane);
2121         dspcntr = I915_READ(reg);
2122         /* Mask out pixel format bits in case we change it */
2123         dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
2124         switch (fb->bits_per_pixel) {
2125         case 8:
2126                 dspcntr |= DISPPLANE_8BPP;
2127                 break;
2128         case 16:
2129                 if (fb->depth == 15)
2130                         dspcntr |= DISPPLANE_15_16BPP;
2131                 else
2132                         dspcntr |= DISPPLANE_16BPP;
2133                 break;
2134         case 24:
2135         case 32:
2136                 dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
2137                 break;
2138         default:
2139                 DRM_ERROR("Unknown color depth %d\n", fb->bits_per_pixel);
2140                 return -EINVAL;
2141         }
2142         if (INTEL_INFO(dev)->gen >= 4) {
2143                 if (obj->tiling_mode != I915_TILING_NONE)
2144                         dspcntr |= DISPPLANE_TILED;
2145                 else
2146                         dspcntr &= ~DISPPLANE_TILED;
2147         }
2148
2149         I915_WRITE(reg, dspcntr);
2150
2151         Start = obj->gtt_offset;
2152         Offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
2153
2154         DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2155                       Start, Offset, x, y, fb->pitches[0]);
2156         I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
2157         if (INTEL_INFO(dev)->gen >= 4) {
2158                 I915_WRITE(DSPSURF(plane), Start);
2159                 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2160                 I915_WRITE(DSPADDR(plane), Offset);
2161         } else
2162                 I915_WRITE(DSPADDR(plane), Start + Offset);
2163         POSTING_READ(reg);
2164
2165         return 0;
2166 }
2167
2168 static int ironlake_update_plane(struct drm_crtc *crtc,
2169                                  struct drm_framebuffer *fb, int x, int y)
2170 {
2171         struct drm_device *dev = crtc->dev;
2172         struct drm_i915_private *dev_priv = dev->dev_private;
2173         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2174         struct intel_framebuffer *intel_fb;
2175         struct drm_i915_gem_object *obj;
2176         int plane = intel_crtc->plane;
2177         unsigned long Start, Offset;
2178         u32 dspcntr;
2179         u32 reg;
2180
2181         switch (plane) {
2182         case 0:
2183         case 1:
2184         case 2:
2185                 break;
2186         default:
2187                 DRM_ERROR("Can't update plane %d in SAREA\n", plane);
2188                 return -EINVAL;
2189         }
2190
2191         intel_fb = to_intel_framebuffer(fb);
2192         obj = intel_fb->obj;
2193
2194         reg = DSPCNTR(plane);
2195         dspcntr = I915_READ(reg);
2196         /* Mask out pixel format bits in case we change it */
2197         dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
2198         switch (fb->bits_per_pixel) {
2199         case 8:
2200                 dspcntr |= DISPPLANE_8BPP;
2201                 break;
2202         case 16:
2203                 if (fb->depth != 16)
2204                         return -EINVAL;
2205
2206                 dspcntr |= DISPPLANE_16BPP;
2207                 break;
2208         case 24:
2209         case 32:
2210                 if (fb->depth == 24)
2211                         dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
2212                 else if (fb->depth == 30)
2213                         dspcntr |= DISPPLANE_32BPP_30BIT_NO_ALPHA;
2214                 else
2215                         return -EINVAL;
2216                 break;
2217         default:
2218                 DRM_ERROR("Unknown color depth %d\n", fb->bits_per_pixel);
2219                 return -EINVAL;
2220         }
2221
2222         if (obj->tiling_mode != I915_TILING_NONE)
2223                 dspcntr |= DISPPLANE_TILED;
2224         else
2225                 dspcntr &= ~DISPPLANE_TILED;
2226
2227         /* must disable */
2228         dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2229
2230         I915_WRITE(reg, dspcntr);
2231
2232         Start = obj->gtt_offset;
2233         Offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
2234
2235         DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2236                       Start, Offset, x, y, fb->pitches[0]);
2237         I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
2238         I915_WRITE(DSPSURF(plane), Start);
2239         I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2240         I915_WRITE(DSPADDR(plane), Offset);
2241         POSTING_READ(reg);
2242
2243         return 0;
2244 }
2245
2246 /* Assume fb object is pinned & idle & fenced and just update base pointers */
2247 static int
2248 intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2249                            int x, int y, enum mode_set_atomic state)
2250 {
2251         struct drm_device *dev = crtc->dev;
2252         struct drm_i915_private *dev_priv = dev->dev_private;
2253         int ret;
2254
2255         ret = dev_priv->display.update_plane(crtc, fb, x, y);
2256         if (ret)
2257                 return ret;
2258
2259         intel_update_fbc(dev);
2260         intel_increase_pllclock(crtc);
2261
2262         return 0;
2263 }
2264
2265 static int
2266 intel_finish_fb(struct drm_framebuffer *old_fb)
2267 {
2268         struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
2269         struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2270         bool was_interruptible = dev_priv->mm.interruptible;
2271         int ret;
2272
2273         wait_event(dev_priv->pending_flip_queue,
2274                    atomic_read(&dev_priv->mm.wedged) ||
2275                    atomic_read(&obj->pending_flip) == 0);
2276
2277         /* Big Hammer, we also need to ensure that any pending
2278          * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2279          * current scanout is retired before unpinning the old
2280          * framebuffer.
2281          *
2282          * This should only fail upon a hung GPU, in which case we
2283          * can safely continue.
2284          */
2285         dev_priv->mm.interruptible = false;
2286         ret = i915_gem_object_finish_gpu(obj);
2287         dev_priv->mm.interruptible = was_interruptible;
2288
2289         return ret;
2290 }
2291
2292 static int
2293 intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
2294                     struct drm_framebuffer *old_fb)
2295 {
2296         struct drm_device *dev = crtc->dev;
2297         struct drm_i915_master_private *master_priv;
2298         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2299         int ret;
2300
2301         /* no fb bound */
2302         if (!crtc->fb) {
2303                 DRM_ERROR("No FB bound\n");
2304                 return 0;
2305         }
2306
2307         switch (intel_crtc->plane) {
2308         case 0:
2309         case 1:
2310                 break;
2311         case 2:
2312                 if (IS_IVYBRIDGE(dev))
2313                         break;
2314                 /* fall through otherwise */
2315         default:
2316                 DRM_ERROR("no plane for crtc\n");
2317                 return -EINVAL;
2318         }
2319
2320         mutex_lock(&dev->struct_mutex);
2321         ret = intel_pin_and_fence_fb_obj(dev,
2322                                          to_intel_framebuffer(crtc->fb)->obj,
2323                                          NULL);
2324         if (ret != 0) {
2325                 mutex_unlock(&dev->struct_mutex);
2326                 DRM_ERROR("pin & fence failed\n");
2327                 return ret;
2328         }
2329
2330         if (old_fb)
2331                 intel_finish_fb(old_fb);
2332
2333         ret = intel_pipe_set_base_atomic(crtc, crtc->fb, x, y,
2334                                          LEAVE_ATOMIC_MODE_SET);
2335         if (ret) {
2336                 intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
2337                 mutex_unlock(&dev->struct_mutex);
2338                 DRM_ERROR("failed to update base address\n");
2339                 return ret;
2340         }
2341
2342         if (old_fb) {
2343                 intel_wait_for_vblank(dev, intel_crtc->pipe);
2344                 intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
2345         }
2346
2347         mutex_unlock(&dev->struct_mutex);
2348
2349         if (!dev->primary->master)
2350                 return 0;
2351
2352         master_priv = dev->primary->master->driver_priv;
2353         if (!master_priv->sarea_priv)
2354                 return 0;
2355
2356         if (intel_crtc->pipe) {
2357                 master_priv->sarea_priv->pipeB_x = x;
2358                 master_priv->sarea_priv->pipeB_y = y;
2359         } else {
2360                 master_priv->sarea_priv->pipeA_x = x;
2361                 master_priv->sarea_priv->pipeA_y = y;
2362         }
2363
2364         return 0;
2365 }
2366
2367 static void ironlake_set_pll_edp(struct drm_crtc *crtc, int clock)
2368 {
2369         struct drm_device *dev = crtc->dev;
2370         struct drm_i915_private *dev_priv = dev->dev_private;
2371         u32 dpa_ctl;
2372
2373         DRM_DEBUG_KMS("eDP PLL enable for clock %d\n", clock);
2374         dpa_ctl = I915_READ(DP_A);
2375         dpa_ctl &= ~DP_PLL_FREQ_MASK;
2376
2377         if (clock < 200000) {
2378                 u32 temp;
2379                 dpa_ctl |= DP_PLL_FREQ_160MHZ;
2380                 /* workaround for 160Mhz:
2381                    1) program 0x4600c bits 15:0 = 0x8124
2382                    2) program 0x46010 bit 0 = 1
2383                    3) program 0x46034 bit 24 = 1
2384                    4) program 0x64000 bit 14 = 1
2385                    */
2386                 temp = I915_READ(0x4600c);
2387                 temp &= 0xffff0000;
2388                 I915_WRITE(0x4600c, temp | 0x8124);
2389
2390                 temp = I915_READ(0x46010);
2391                 I915_WRITE(0x46010, temp | 1);
2392
2393                 temp = I915_READ(0x46034);
2394                 I915_WRITE(0x46034, temp | (1 << 24));
2395         } else {
2396                 dpa_ctl |= DP_PLL_FREQ_270MHZ;
2397         }
2398         I915_WRITE(DP_A, dpa_ctl);
2399
2400         POSTING_READ(DP_A);
2401         udelay(500);
2402 }
2403
2404 static void intel_fdi_normal_train(struct drm_crtc *crtc)
2405 {
2406         struct drm_device *dev = crtc->dev;
2407         struct drm_i915_private *dev_priv = dev->dev_private;
2408         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2409         int pipe = intel_crtc->pipe;
2410         u32 reg, temp;
2411
2412         /* enable normal train */
2413         reg = FDI_TX_CTL(pipe);
2414         temp = I915_READ(reg);
2415         if (IS_IVYBRIDGE(dev)) {
2416                 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2417                 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
2418         } else {
2419                 temp &= ~FDI_LINK_TRAIN_NONE;
2420                 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
2421         }
2422         I915_WRITE(reg, temp);
2423
2424         reg = FDI_RX_CTL(pipe);
2425         temp = I915_READ(reg);
2426         if (HAS_PCH_CPT(dev)) {
2427                 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2428                 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2429         } else {
2430                 temp &= ~FDI_LINK_TRAIN_NONE;
2431                 temp |= FDI_LINK_TRAIN_NONE;
2432         }
2433         I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2434
2435         /* wait one idle pattern time */
2436         POSTING_READ(reg);
2437         udelay(1000);
2438
2439         /* IVB wants error correction enabled */
2440         if (IS_IVYBRIDGE(dev))
2441                 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2442                            FDI_FE_ERRC_ENABLE);
2443 }
2444
2445 /* The FDI link training functions for ILK/Ibexpeak. */
2446 static void ironlake_fdi_link_train(struct drm_crtc *crtc)
2447 {
2448         struct drm_device *dev = crtc->dev;
2449         struct drm_i915_private *dev_priv = dev->dev_private;
2450         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2451         int pipe = intel_crtc->pipe;
2452         int plane = intel_crtc->plane;
2453         u32 reg, temp, tries;
2454
2455         /* FDI needs bits from pipe & plane first */
2456         assert_pipe_enabled(dev_priv, pipe);
2457         assert_plane_enabled(dev_priv, plane);
2458
2459         /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2460            for train result */
2461         reg = FDI_RX_IMR(pipe);
2462         temp = I915_READ(reg);
2463         temp &= ~FDI_RX_SYMBOL_LOCK;
2464         temp &= ~FDI_RX_BIT_LOCK;
2465         I915_WRITE(reg, temp);
2466         I915_READ(reg);
2467         udelay(150);
2468
2469         /* enable CPU FDI TX and PCH FDI RX */
2470         reg = FDI_TX_CTL(pipe);
2471         temp = I915_READ(reg);
2472         temp &= ~(7 << 19);
2473         temp |= (intel_crtc->fdi_lanes - 1) << 19;
2474         temp &= ~FDI_LINK_TRAIN_NONE;
2475         temp |= FDI_LINK_TRAIN_PATTERN_1;
2476         I915_WRITE(reg, temp | FDI_TX_ENABLE);
2477
2478         reg = FDI_RX_CTL(pipe);
2479         temp = I915_READ(reg);
2480         temp &= ~FDI_LINK_TRAIN_NONE;
2481         temp |= FDI_LINK_TRAIN_PATTERN_1;
2482         I915_WRITE(reg, temp | FDI_RX_ENABLE);
2483
2484         POSTING_READ(reg);
2485         udelay(150);
2486
2487         /* Ironlake workaround, enable clock pointer after FDI enable*/
2488         if (HAS_PCH_IBX(dev)) {
2489                 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2490                 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
2491                            FDI_RX_PHASE_SYNC_POINTER_EN);
2492         }
2493
2494         reg = FDI_RX_IIR(pipe);
2495         for (tries = 0; tries < 5; tries++) {
2496                 temp = I915_READ(reg);
2497                 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2498
2499                 if ((temp & FDI_RX_BIT_LOCK)) {
2500                         DRM_DEBUG_KMS("FDI train 1 done.\n");
2501                         I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2502                         break;
2503                 }
2504         }
2505         if (tries == 5)
2506                 DRM_ERROR("FDI train 1 fail!\n");
2507
2508         /* Train 2 */
2509         reg = FDI_TX_CTL(pipe);
2510         temp = I915_READ(reg);
2511         temp &= ~FDI_LINK_TRAIN_NONE;
2512         temp |= FDI_LINK_TRAIN_PATTERN_2;
2513         I915_WRITE(reg, temp);
2514
2515         reg = FDI_RX_CTL(pipe);
2516         temp = I915_READ(reg);
2517         temp &= ~FDI_LINK_TRAIN_NONE;
2518         temp |= FDI_LINK_TRAIN_PATTERN_2;
2519         I915_WRITE(reg, temp);
2520
2521         POSTING_READ(reg);
2522         udelay(150);
2523
2524         reg = FDI_RX_IIR(pipe);
2525         for (tries = 0; tries < 5; tries++) {
2526                 temp = I915_READ(reg);
2527                 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2528
2529                 if (temp & FDI_RX_SYMBOL_LOCK) {
2530                         I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2531                         DRM_DEBUG_KMS("FDI train 2 done.\n");
2532                         break;
2533                 }
2534         }
2535         if (tries == 5)
2536                 DRM_ERROR("FDI train 2 fail!\n");
2537
2538         DRM_DEBUG_KMS("FDI train done\n");
2539
2540 }
2541
2542 static const int snb_b_fdi_train_param[] = {
2543         FDI_LINK_TRAIN_400MV_0DB_SNB_B,
2544         FDI_LINK_TRAIN_400MV_6DB_SNB_B,
2545         FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
2546         FDI_LINK_TRAIN_800MV_0DB_SNB_B,
2547 };
2548
2549 /* The FDI link training functions for SNB/Cougarpoint. */
2550 static void gen6_fdi_link_train(struct drm_crtc *crtc)
2551 {
2552         struct drm_device *dev = crtc->dev;
2553         struct drm_i915_private *dev_priv = dev->dev_private;
2554         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2555         int pipe = intel_crtc->pipe;
2556         u32 reg, temp, i;
2557
2558         /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2559            for train result */
2560         reg = FDI_RX_IMR(pipe);
2561         temp = I915_READ(reg);
2562         temp &= ~FDI_RX_SYMBOL_LOCK;
2563         temp &= ~FDI_RX_BIT_LOCK;
2564         I915_WRITE(reg, temp);
2565
2566         POSTING_READ(reg);
2567         udelay(150);
2568
2569         /* enable CPU FDI TX and PCH FDI RX */
2570         reg = FDI_TX_CTL(pipe);
2571         temp = I915_READ(reg);
2572         temp &= ~(7 << 19);
2573         temp |= (intel_crtc->fdi_lanes - 1) << 19;
2574         temp &= ~FDI_LINK_TRAIN_NONE;
2575         temp |= FDI_LINK_TRAIN_PATTERN_1;
2576         temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2577         /* SNB-B */
2578         temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2579         I915_WRITE(reg, temp | FDI_TX_ENABLE);
2580
2581         reg = FDI_RX_CTL(pipe);
2582         temp = I915_READ(reg);
2583         if (HAS_PCH_CPT(dev)) {
2584                 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2585                 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2586         } else {
2587                 temp &= ~FDI_LINK_TRAIN_NONE;
2588                 temp |= FDI_LINK_TRAIN_PATTERN_1;
2589         }
2590         I915_WRITE(reg, temp | FDI_RX_ENABLE);
2591
2592         POSTING_READ(reg);
2593         udelay(150);
2594
2595         for (i = 0; i < 4; i++) {
2596                 reg = FDI_TX_CTL(pipe);
2597                 temp = I915_READ(reg);
2598                 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2599                 temp |= snb_b_fdi_train_param[i];
2600                 I915_WRITE(reg, temp);
2601
2602                 POSTING_READ(reg);
2603                 udelay(500);
2604
2605                 reg = FDI_RX_IIR(pipe);
2606                 temp = I915_READ(reg);
2607                 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2608
2609                 if (temp & FDI_RX_BIT_LOCK) {
2610                         I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2611                         DRM_DEBUG_KMS("FDI train 1 done.\n");
2612                         break;
2613                 }
2614         }
2615         if (i == 4)
2616                 DRM_ERROR("FDI train 1 fail!\n");
2617
2618         /* Train 2 */
2619         reg = FDI_TX_CTL(pipe);
2620         temp = I915_READ(reg);
2621         temp &= ~FDI_LINK_TRAIN_NONE;
2622         temp |= FDI_LINK_TRAIN_PATTERN_2;
2623         if (IS_GEN6(dev)) {
2624                 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2625                 /* SNB-B */
2626                 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2627         }
2628         I915_WRITE(reg, temp);
2629
2630         reg = FDI_RX_CTL(pipe);
2631         temp = I915_READ(reg);
2632         if (HAS_PCH_CPT(dev)) {
2633                 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2634                 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2635         } else {
2636                 temp &= ~FDI_LINK_TRAIN_NONE;
2637                 temp |= FDI_LINK_TRAIN_PATTERN_2;
2638         }
2639         I915_WRITE(reg, temp);
2640
2641         POSTING_READ(reg);
2642         udelay(150);
2643
2644         for (i = 0; i < 4; i++) {
2645                 reg = FDI_TX_CTL(pipe);
2646                 temp = I915_READ(reg);
2647                 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2648                 temp |= snb_b_fdi_train_param[i];
2649                 I915_WRITE(reg, temp);
2650
2651                 POSTING_READ(reg);
2652                 udelay(500);
2653
2654                 reg = FDI_RX_IIR(pipe);
2655                 temp = I915_READ(reg);
2656                 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2657
2658                 if (temp & FDI_RX_SYMBOL_LOCK) {
2659                         I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2660                         DRM_DEBUG_KMS("FDI train 2 done.\n");
2661                         break;
2662                 }
2663         }
2664         if (i == 4)
2665                 DRM_ERROR("FDI train 2 fail!\n");
2666
2667         DRM_DEBUG_KMS("FDI train done.\n");
2668 }
2669
2670 /* Manual link training for Ivy Bridge A0 parts */
2671 static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
2672 {
2673         struct drm_device *dev = crtc->dev;
2674         struct drm_i915_private *dev_priv = dev->dev_private;
2675         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2676         int pipe = intel_crtc->pipe;
2677         u32 reg, temp, i;
2678
2679         /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2680            for train result */
2681         reg = FDI_RX_IMR(pipe);
2682         temp = I915_READ(reg);
2683         temp &= ~FDI_RX_SYMBOL_LOCK;
2684         temp &= ~FDI_RX_BIT_LOCK;
2685         I915_WRITE(reg, temp);
2686
2687         POSTING_READ(reg);
2688         udelay(150);
2689
2690         /* enable CPU FDI TX and PCH FDI RX */
2691         reg = FDI_TX_CTL(pipe);
2692         temp = I915_READ(reg);
2693         temp &= ~(7 << 19);
2694         temp |= (intel_crtc->fdi_lanes - 1) << 19;
2695         temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
2696         temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
2697         temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2698         temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2699         temp |= FDI_COMPOSITE_SYNC;
2700         I915_WRITE(reg, temp | FDI_TX_ENABLE);
2701
2702         reg = FDI_RX_CTL(pipe);
2703         temp = I915_READ(reg);
2704         temp &= ~FDI_LINK_TRAIN_AUTO;
2705         temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2706         temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2707         temp |= FDI_COMPOSITE_SYNC;
2708         I915_WRITE(reg, temp | FDI_RX_ENABLE);
2709
2710         POSTING_READ(reg);
2711         udelay(150);
2712
2713         for (i = 0; i < 4; i++) {
2714                 reg = FDI_TX_CTL(pipe);
2715                 temp = I915_READ(reg);
2716                 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2717                 temp |= snb_b_fdi_train_param[i];
2718                 I915_WRITE(reg, temp);
2719
2720                 POSTING_READ(reg);
2721                 udelay(500);
2722
2723                 reg = FDI_RX_IIR(pipe);
2724                 temp = I915_READ(reg);
2725                 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2726
2727                 if (temp & FDI_RX_BIT_LOCK ||
2728                     (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
2729                         I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2730                         DRM_DEBUG_KMS("FDI train 1 done.\n");
2731                         break;
2732                 }
2733         }
2734         if (i == 4)
2735                 DRM_ERROR("FDI train 1 fail!\n");
2736
2737         /* Train 2 */
2738         reg = FDI_TX_CTL(pipe);
2739         temp = I915_READ(reg);
2740         temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2741         temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
2742         temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2743         temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2744         I915_WRITE(reg, temp);
2745
2746         reg = FDI_RX_CTL(pipe);
2747         temp = I915_READ(reg);
2748         temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2749         temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2750         I915_WRITE(reg, temp);
2751
2752         POSTING_READ(reg);
2753         udelay(150);
2754
2755         for (i = 0; i < 4; i++) {
2756                 reg = FDI_TX_CTL(pipe);
2757                 temp = I915_READ(reg);
2758                 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2759                 temp |= snb_b_fdi_train_param[i];
2760                 I915_WRITE(reg, temp);
2761
2762                 POSTING_READ(reg);
2763                 udelay(500);
2764
2765                 reg = FDI_RX_IIR(pipe);
2766                 temp = I915_READ(reg);
2767                 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2768
2769                 if (temp & FDI_RX_SYMBOL_LOCK) {
2770                         I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2771                         DRM_DEBUG_KMS("FDI train 2 done.\n");
2772                         break;
2773                 }
2774         }
2775         if (i == 4)
2776                 DRM_ERROR("FDI train 2 fail!\n");
2777
2778         DRM_DEBUG_KMS("FDI train done.\n");
2779 }
2780
2781 static void ironlake_fdi_pll_enable(struct drm_crtc *crtc)
2782 {
2783         struct drm_device *dev = crtc->dev;
2784         struct drm_i915_private *dev_priv = dev->dev_private;
2785         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2786         int pipe = intel_crtc->pipe;
2787         u32 reg, temp;
2788
2789         /* Write the TU size bits so error detection works */
2790         I915_WRITE(FDI_RX_TUSIZE1(pipe),
2791                    I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
2792
2793         /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
2794         reg = FDI_RX_CTL(pipe);
2795         temp = I915_READ(reg);
2796         temp &= ~((0x7 << 19) | (0x7 << 16));
2797         temp |= (intel_crtc->fdi_lanes - 1) << 19;
2798         temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
2799         I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
2800
2801         POSTING_READ(reg);
2802         udelay(200);
2803
2804         /* Switch from Rawclk to PCDclk */
2805         temp = I915_READ(reg);
2806         I915_WRITE(reg, temp | FDI_PCDCLK);
2807
2808         POSTING_READ(reg);
2809         udelay(200);
2810
2811         /* Enable CPU FDI TX PLL, always on for Ironlake */
2812         reg = FDI_TX_CTL(pipe);
2813         temp = I915_READ(reg);
2814         if ((temp & FDI_TX_PLL_ENABLE) == 0) {
2815                 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
2816
2817                 POSTING_READ(reg);
2818                 udelay(100);
2819         }
2820 }
2821
2822 static void ironlake_fdi_disable(struct drm_crtc *crtc)
2823 {
2824         struct drm_device *dev = crtc->dev;
2825         struct drm_i915_private *dev_priv = dev->dev_private;
2826         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2827         int pipe = intel_crtc->pipe;
2828         u32 reg, temp;
2829
2830         /* disable CPU FDI tx and PCH FDI rx */
2831         reg = FDI_TX_CTL(pipe);
2832         temp = I915_READ(reg);
2833         I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
2834         POSTING_READ(reg);
2835
2836         reg = FDI_RX_CTL(pipe);
2837         temp = I915_READ(reg);
2838         temp &= ~(0x7 << 16);
2839         temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
2840         I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
2841
2842         POSTING_READ(reg);
2843         udelay(100);
2844
2845         /* Ironlake workaround, disable clock pointer after downing FDI */
2846         if (HAS_PCH_IBX(dev)) {
2847                 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2848                 I915_WRITE(FDI_RX_CHICKEN(pipe),
2849                            I915_READ(FDI_RX_CHICKEN(pipe) &
2850                                      ~FDI_RX_PHASE_SYNC_POINTER_EN));
2851         }
2852
2853         /* still set train pattern 1 */
2854         reg = FDI_TX_CTL(pipe);
2855         temp = I915_READ(reg);
2856         temp &= ~FDI_LINK_TRAIN_NONE;
2857         temp |= FDI_LINK_TRAIN_PATTERN_1;
2858         I915_WRITE(reg, temp);
2859
2860         reg = FDI_RX_CTL(pipe);
2861         temp = I915_READ(reg);
2862         if (HAS_PCH_CPT(dev)) {
2863                 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2864                 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2865         } else {
2866                 temp &= ~FDI_LINK_TRAIN_NONE;
2867                 temp |= FDI_LINK_TRAIN_PATTERN_1;
2868         }
2869         /* BPC in FDI rx is consistent with that in PIPECONF */
2870         temp &= ~(0x07 << 16);
2871         temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
2872         I915_WRITE(reg, temp);
2873
2874         POSTING_READ(reg);
2875         udelay(100);
2876 }
2877
2878 /*
2879  * When we disable a pipe, we need to clear any pending scanline wait events
2880  * to avoid hanging the ring, which we assume we are waiting on.
2881  */
2882 static void intel_clear_scanline_wait(struct drm_device *dev)
2883 {
2884         struct drm_i915_private *dev_priv = dev->dev_private;
2885         struct intel_ring_buffer *ring;
2886         u32 tmp;
2887
2888         if (IS_GEN2(dev))
2889                 /* Can't break the hang on i8xx */
2890                 return;
2891
2892         ring = LP_RING(dev_priv);
2893         tmp = I915_READ_CTL(ring);
2894         if (tmp & RING_WAIT)
2895                 I915_WRITE_CTL(ring, tmp);
2896 }
2897
2898 static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
2899 {
2900         struct drm_device *dev = crtc->dev;
2901
2902         if (crtc->fb == NULL)
2903                 return;
2904
2905         mutex_lock(&dev->struct_mutex);
2906         intel_finish_fb(crtc->fb);
2907         mutex_unlock(&dev->struct_mutex);
2908 }
2909
2910 static bool intel_crtc_driving_pch(struct drm_crtc *crtc)
2911 {
2912         struct drm_device *dev = crtc->dev;
2913         struct drm_mode_config *mode_config = &dev->mode_config;
2914         struct intel_encoder *encoder;
2915
2916         /*
2917          * If there's a non-PCH eDP on this crtc, it must be DP_A, and that
2918          * must be driven by its own crtc; no sharing is possible.
2919          */
2920         list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
2921                 if (encoder->base.crtc != crtc)
2922                         continue;
2923
2924                 switch (encoder->type) {
2925                 case INTEL_OUTPUT_EDP:
2926                         if (!intel_encoder_is_pch_edp(&encoder->base))
2927                                 return false;
2928                         continue;
2929                 }
2930         }
2931
2932         return true;
2933 }
2934
2935 /*
2936  * Enable PCH resources required for PCH ports:
2937  *   - PCH PLLs
2938  *   - FDI training & RX/TX
2939  *   - update transcoder timings
2940  *   - DP transcoding bits
2941  *   - transcoder
2942  */
2943 static void ironlake_pch_enable(struct drm_crtc *crtc)
2944 {
2945         struct drm_device *dev = crtc->dev;
2946         struct drm_i915_private *dev_priv = dev->dev_private;
2947         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2948         int pipe = intel_crtc->pipe;
2949         u32 reg, temp, transc_sel;
2950
2951         /* For PCH output, training FDI link */
2952         dev_priv->display.fdi_link_train(crtc);
2953
2954         intel_enable_pch_pll(dev_priv, pipe);
2955
2956         if (HAS_PCH_CPT(dev)) {
2957                 transc_sel = intel_crtc->use_pll_a ? TRANSC_DPLLA_SEL :
2958                         TRANSC_DPLLB_SEL;
2959
2960                 /* Be sure PCH DPLL SEL is set */
2961                 temp = I915_READ(PCH_DPLL_SEL);
2962                 if (pipe == 0) {
2963                         temp &= ~(TRANSA_DPLLB_SEL);
2964                         temp |= (TRANSA_DPLL_ENABLE | TRANSA_DPLLA_SEL);
2965                 } else if (pipe == 1) {
2966                         temp &= ~(TRANSB_DPLLB_SEL);
2967                         temp |= (TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
2968                 } else if (pipe == 2) {
2969                         temp &= ~(TRANSC_DPLLB_SEL);
2970                         temp |= (TRANSC_DPLL_ENABLE | transc_sel);
2971                 }
2972                 I915_WRITE(PCH_DPLL_SEL, temp);
2973         }
2974
2975         /* set transcoder timing, panel must allow it */
2976         assert_panel_unlocked(dev_priv, pipe);
2977         I915_WRITE(TRANS_HTOTAL(pipe), I915_READ(HTOTAL(pipe)));
2978         I915_WRITE(TRANS_HBLANK(pipe), I915_READ(HBLANK(pipe)));
2979         I915_WRITE(TRANS_HSYNC(pipe),  I915_READ(HSYNC(pipe)));
2980
2981         I915_WRITE(TRANS_VTOTAL(pipe), I915_READ(VTOTAL(pipe)));
2982         I915_WRITE(TRANS_VBLANK(pipe), I915_READ(VBLANK(pipe)));
2983         I915_WRITE(TRANS_VSYNC(pipe),  I915_READ(VSYNC(pipe)));
2984         I915_WRITE(TRANS_VSYNCSHIFT(pipe),  I915_READ(VSYNCSHIFT(pipe)));
2985
2986         intel_fdi_normal_train(crtc);
2987
2988         /* For PCH DP, enable TRANS_DP_CTL */
2989         if (HAS_PCH_CPT(dev) &&
2990             (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
2991              intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
2992                 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) >> 5;
2993                 reg = TRANS_DP_CTL(pipe);
2994                 temp = I915_READ(reg);
2995                 temp &= ~(TRANS_DP_PORT_SEL_MASK |
2996                           TRANS_DP_SYNC_MASK |
2997                           TRANS_DP_BPC_MASK);
2998                 temp |= (TRANS_DP_OUTPUT_ENABLE |
2999                          TRANS_DP_ENH_FRAMING);
3000                 temp |= bpc << 9; /* same format but at 11:9 */
3001
3002                 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
3003                         temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
3004                 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
3005                         temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
3006
3007                 switch (intel_trans_dp_port_sel(crtc)) {
3008                 case PCH_DP_B:
3009                         temp |= TRANS_DP_PORT_SEL_B;
3010                         break;
3011                 case PCH_DP_C:
3012                         temp |= TRANS_DP_PORT_SEL_C;
3013                         break;
3014                 case PCH_DP_D:
3015                         temp |= TRANS_DP_PORT_SEL_D;
3016                         break;
3017                 default:
3018                         DRM_DEBUG_KMS("Wrong PCH DP port return. Guess port B\n");
3019                         temp |= TRANS_DP_PORT_SEL_B;
3020                         break;
3021                 }
3022
3023                 I915_WRITE(reg, temp);
3024         }
3025
3026         intel_enable_transcoder(dev_priv, pipe);
3027 }
3028
3029 void intel_cpt_verify_modeset(struct drm_device *dev, int pipe)
3030 {
3031         struct drm_i915_private *dev_priv = dev->dev_private;
3032         int dslreg = PIPEDSL(pipe), tc2reg = TRANS_CHICKEN2(pipe);
3033         u32 temp;
3034
3035         temp = I915_READ(dslreg);
3036         udelay(500);
3037         if (wait_for(I915_READ(dslreg) != temp, 5)) {
3038                 /* Without this, mode sets may fail silently on FDI */
3039                 I915_WRITE(tc2reg, TRANS_AUTOTRAIN_GEN_STALL_DIS);
3040                 udelay(250);
3041                 I915_WRITE(tc2reg, 0);
3042                 if (wait_for(I915_READ(dslreg) != temp, 5))
3043                         DRM_ERROR("mode set failed: pipe %d stuck\n", pipe);
3044         }
3045 }
3046
3047 static void ironlake_crtc_enable(struct drm_crtc *crtc)
3048 {
3049         struct drm_device *dev = crtc->dev;
3050         struct drm_i915_private *dev_priv = dev->dev_private;
3051         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3052         int pipe = intel_crtc->pipe;
3053         int plane = intel_crtc->plane;
3054         u32 temp;
3055         bool is_pch_port;
3056
3057         if (intel_crtc->active)
3058                 return;
3059
3060         intel_crtc->active = true;
3061         intel_update_watermarks(dev);
3062
3063         if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
3064                 temp = I915_READ(PCH_LVDS);
3065                 if ((temp & LVDS_PORT_EN) == 0)
3066                         I915_WRITE(PCH_LVDS, temp | LVDS_PORT_EN);
3067         }
3068
3069         is_pch_port = intel_crtc_driving_pch(crtc);
3070
3071         if (is_pch_port)
3072                 ironlake_fdi_pll_enable(crtc);
3073         else
3074                 ironlake_fdi_disable(crtc);
3075
3076         /* Enable panel fitting for LVDS */
3077         if (dev_priv->pch_pf_size &&
3078             (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) || HAS_eDP)) {
3079                 /* Force use of hard-coded filter coefficients
3080                  * as some pre-programmed values are broken,
3081                  * e.g. x201.
3082                  */
3083                 if (IS_IVYBRIDGE(dev))
3084                         I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3085                                                  PF_PIPE_SEL_IVB(pipe));
3086                 else
3087                         I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
3088                 I915_WRITE(PF_WIN_POS(pipe), dev_priv->pch_pf_pos);
3089                 I915_WRITE(PF_WIN_SZ(pipe), dev_priv->pch_pf_size);
3090         }
3091
3092         /*
3093          * On ILK+ LUT must be loaded before the pipe is running but with
3094          * clocks enabled
3095          */
3096         intel_crtc_load_lut(crtc);
3097
3098         intel_enable_pipe(dev_priv, pipe, is_pch_port);
3099         intel_enable_plane(dev_priv, plane, pipe);
3100
3101         if (is_pch_port)
3102                 ironlake_pch_enable(crtc);
3103
3104         mutex_lock(&dev->struct_mutex);
3105         intel_update_fbc(dev);
3106         mutex_unlock(&dev->struct_mutex);
3107
3108         intel_crtc_update_cursor(crtc, true);
3109 }
3110
3111 static void ironlake_crtc_disable(struct drm_crtc *crtc)
3112 {
3113         struct drm_device *dev = crtc->dev;
3114         struct drm_i915_private *dev_priv = dev->dev_private;
3115         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3116         int pipe = intel_crtc->pipe;
3117         int plane = intel_crtc->plane;
3118         u32 reg, temp;
3119
3120         if (!intel_crtc->active)
3121                 return;
3122
3123         intel_crtc_wait_for_pending_flips(crtc);
3124         drm_vblank_off(dev, pipe);
3125         intel_crtc_update_cursor(crtc, false);
3126
3127         intel_disable_plane(dev_priv, plane, pipe);
3128
3129         if (dev_priv->cfb_plane == plane)
3130                 intel_disable_fbc(dev);
3131
3132         intel_disable_pipe(dev_priv, pipe);
3133
3134         /* Disable PF */
3135         I915_WRITE(PF_CTL(pipe), 0);
3136         I915_WRITE(PF_WIN_SZ(pipe), 0);
3137
3138         ironlake_fdi_disable(crtc);
3139
3140         /* This is a horrible layering violation; we should be doing this in
3141          * the connector/encoder ->prepare instead, but we don't always have
3142          * enough information there about the config to know whether it will
3143          * actually be necessary or just cause undesired flicker.
3144          */
3145         intel_disable_pch_ports(dev_priv, pipe);
3146
3147         intel_disable_transcoder(dev_priv, pipe);
3148
3149         if (HAS_PCH_CPT(dev)) {
3150                 /* disable TRANS_DP_CTL */
3151                 reg = TRANS_DP_CTL(pipe);
3152                 temp = I915_READ(reg);
3153                 temp &= ~(TRANS_DP_OUTPUT_ENABLE | TRANS_DP_PORT_SEL_MASK);
3154                 temp |= TRANS_DP_PORT_SEL_NONE;
3155                 I915_WRITE(reg, temp);
3156
3157                 /* disable DPLL_SEL */
3158                 temp = I915_READ(PCH_DPLL_SEL);
3159                 switch (pipe) {
3160                 case 0:
3161                         temp &= ~(TRANSA_DPLL_ENABLE | TRANSA_DPLLB_SEL);
3162                         break;
3163                 case 1:
3164                         temp &= ~(TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
3165                         break;
3166                 case 2:
3167                         /* C shares PLL A or B */
3168                         temp &= ~(TRANSC_DPLL_ENABLE | TRANSC_DPLLB_SEL);
3169                         break;
3170                 default:
3171                         BUG(); /* wtf */
3172                 }
3173                 I915_WRITE(PCH_DPLL_SEL, temp);
3174         }
3175
3176         /* disable PCH DPLL */
3177         if (!intel_crtc->no_pll)
3178                 intel_disable_pch_pll(dev_priv, pipe);
3179
3180         /* Switch from PCDclk to Rawclk */
3181         reg = FDI_RX_CTL(pipe);
3182         temp = I915_READ(reg);
3183         I915_WRITE(reg, temp & ~FDI_PCDCLK);
3184
3185         /* Disable CPU FDI TX PLL */
3186         reg = FDI_TX_CTL(pipe);
3187         temp = I915_READ(reg);
3188         I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3189
3190         POSTING_READ(reg);
3191         udelay(100);
3192
3193         reg = FDI_RX_CTL(pipe);
3194         temp = I915_READ(reg);
3195         I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3196
3197         /* Wait for the clocks to turn off. */
3198         POSTING_READ(reg);
3199         udelay(100);
3200
3201         intel_crtc->active = false;
3202         intel_update_watermarks(dev);
3203
3204         mutex_lock(&dev->struct_mutex);
3205         intel_update_fbc(dev);
3206         intel_clear_scanline_wait(dev);
3207         mutex_unlock(&dev->struct_mutex);
3208 }
3209
3210 static void ironlake_crtc_dpms(struct drm_crtc *crtc, int mode)
3211 {
3212         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3213         int pipe = intel_crtc->pipe;
3214         int plane = intel_crtc->plane;
3215
3216         /* XXX: When our outputs are all unaware of DPMS modes other than off
3217          * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
3218          */
3219         switch (mode) {
3220         case DRM_MODE_DPMS_ON:
3221         case DRM_MODE_DPMS_STANDBY:
3222         case DRM_MODE_DPMS_SUSPEND:
3223                 DRM_DEBUG_KMS("crtc %d/%d dpms on\n", pipe, plane);
3224                 ironlake_crtc_enable(crtc);
3225                 break;
3226
3227         case DRM_MODE_DPMS_OFF:
3228                 DRM_DEBUG_KMS("crtc %d/%d dpms off\n", pipe, plane);
3229                 ironlake_crtc_disable(crtc);
3230                 break;
3231         }
3232 }
3233
3234 static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
3235 {
3236         if (!enable && intel_crtc->overlay) {
3237                 struct drm_device *dev = intel_crtc->base.dev;
3238                 struct drm_i915_private *dev_priv = dev->dev_private;
3239
3240                 mutex_lock(&dev->struct_mutex);
3241                 dev_priv->mm.interruptible = false;
3242                 (void) intel_overlay_switch_off(intel_crtc->overlay);
3243                 dev_priv->mm.interruptible = true;
3244                 mutex_unlock(&dev->struct_mutex);
3245         }
3246
3247         /* Let userspace switch the overlay on again. In most cases userspace
3248          * has to recompute where to put it anyway.
3249          */
3250 }
3251
3252 static void i9xx_crtc_enable(struct drm_crtc *crtc)
3253 {
3254         struct drm_device *dev = crtc->dev;
3255         struct drm_i915_private *dev_priv = dev->dev_private;
3256         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3257         int pipe = intel_crtc->pipe;
3258         int plane = intel_crtc->plane;
3259
3260         if (intel_crtc->active)
3261                 return;
3262
3263         intel_crtc->active = true;
3264         intel_update_watermarks(dev);
3265
3266         intel_enable_pll(dev_priv, pipe);
3267         intel_enable_pipe(dev_priv, pipe, false);
3268         intel_enable_plane(dev_priv, plane, pipe);
3269
3270         intel_crtc_load_lut(crtc);
3271         intel_update_fbc(dev);
3272
3273         /* Give the overlay scaler a chance to enable if it's on this pipe */
3274         intel_crtc_dpms_overlay(intel_crtc, true);
3275         intel_crtc_update_cursor(crtc, true);
3276 }
3277
3278 static void i9xx_crtc_disable(struct drm_crtc *crtc)
3279 {
3280         struct drm_device *dev = crtc->dev;
3281         struct drm_i915_private *dev_priv = dev->dev_private;
3282         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3283         int pipe = intel_crtc->pipe;
3284         int plane = intel_crtc->plane;
3285
3286         if (!intel_crtc->active)
3287                 return;
3288
3289         /* Give the overlay scaler a chance to disable if it's on this pipe */
3290         intel_crtc_wait_for_pending_flips(crtc);
3291         drm_vblank_off(dev, pipe);
3292         intel_crtc_dpms_overlay(intel_crtc, false);
3293         intel_crtc_update_cursor(crtc, false);
3294
3295         if (dev_priv->cfb_plane == plane)
3296                 intel_disable_fbc(dev);
3297
3298         intel_disable_plane(dev_priv, plane, pipe);
3299         intel_disable_pipe(dev_priv, pipe);
3300         intel_disable_pll(dev_priv, pipe);
3301
3302         intel_crtc->active = false;
3303         intel_update_fbc(dev);
3304         intel_update_watermarks(dev);
3305         intel_clear_scanline_wait(dev);
3306 }
3307
3308 static void i9xx_crtc_dpms(struct drm_crtc *crtc, int mode)
3309 {
3310         /* XXX: When our outputs are all unaware of DPMS modes other than off
3311          * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
3312          */
3313         switch (mode) {
3314         case DRM_MODE_DPMS_ON:
3315         case DRM_MODE_DPMS_STANDBY:
3316         case DRM_MODE_DPMS_SUSPEND:
3317                 i9xx_crtc_enable(crtc);
3318                 break;
3319         case DRM_MODE_DPMS_OFF:
3320                 i9xx_crtc_disable(crtc);
3321                 break;
3322         }
3323 }
3324
3325 /**
3326  * Sets the power management mode of the pipe and plane.
3327  */
3328 static void intel_crtc_dpms(struct drm_crtc *crtc, int mode)
3329 {
3330         struct drm_device *dev = crtc->dev;
3331         struct drm_i915_private *dev_priv = dev->dev_private;
3332         struct drm_i915_master_private *master_priv;
3333         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3334         int pipe = intel_crtc->pipe;
3335         bool enabled;
3336
3337         if (intel_crtc->dpms_mode == mode)
3338                 return;
3339
3340         intel_crtc->dpms_mode = mode;
3341
3342         dev_priv->display.dpms(crtc, mode);
3343
3344         if (!dev->primary->master)
3345                 return;
3346
3347         master_priv = dev->primary->master->driver_priv;
3348         if (!master_priv->sarea_priv)
3349                 return;
3350
3351         enabled = crtc->enabled && mode != DRM_MODE_DPMS_OFF;
3352
3353         switch (pipe) {
3354         case 0:
3355                 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
3356                 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
3357                 break;
3358         case 1:
3359                 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
3360                 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
3361                 break;
3362         default:
3363                 DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
3364                 break;
3365         }
3366 }
3367
3368 static void intel_crtc_disable(struct drm_crtc *crtc)
3369 {
3370         struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
3371         struct drm_device *dev = crtc->dev;
3372
3373         crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
3374         assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
3375         assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
3376
3377         if (crtc->fb) {
3378                 mutex_lock(&dev->struct_mutex);
3379                 intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
3380                 mutex_unlock(&dev->struct_mutex);
3381         }
3382 }
3383
3384 /* Prepare for a mode set.
3385  *
3386  * Note we could be a lot smarter here.  We need to figure out which outputs
3387  * will be enabled, which disabled (in short, how the config will changes)
3388  * and perform the minimum necessary steps to accomplish that, e.g. updating
3389  * watermarks, FBC configuration, making sure PLLs are programmed correctly,
3390  * panel fitting is in the proper state, etc.
3391  */
3392 static void i9xx_crtc_prepare(struct drm_crtc *crtc)
3393 {
3394         i9xx_crtc_disable(crtc);
3395 }
3396
3397 static void i9xx_crtc_commit(struct drm_crtc *crtc)
3398 {
3399         i9xx_crtc_enable(crtc);
3400 }
3401
3402 static void ironlake_crtc_prepare(struct drm_crtc *crtc)
3403 {
3404         ironlake_crtc_disable(crtc);
3405 }
3406
3407 static void ironlake_crtc_commit(struct drm_crtc *crtc)
3408 {
3409         ironlake_crtc_enable(crtc);
3410 }
3411
3412 void intel_encoder_prepare(struct drm_encoder *encoder)
3413 {
3414         struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
3415         /* lvds has its own version of prepare see intel_lvds_prepare */
3416         encoder_funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
3417 }
3418
3419 void intel_encoder_commit(struct drm_encoder *encoder)
3420 {
3421         struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
3422         struct drm_device *dev = encoder->dev;
3423         struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
3424         struct intel_crtc *intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
3425
3426         /* lvds has its own version of commit see intel_lvds_commit */
3427         encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
3428
3429         if (HAS_PCH_CPT(dev))
3430                 intel_cpt_verify_modeset(dev, intel_crtc->pipe);
3431 }
3432
3433 void intel_encoder_destroy(struct drm_encoder *encoder)
3434 {
3435         struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
3436
3437         drm_encoder_cleanup(encoder);
3438         kfree(intel_encoder);
3439 }
3440
3441 static bool intel_crtc_mode_fixup(struct drm_crtc *crtc,
3442                                   struct drm_display_mode *mode,
3443                                   struct drm_display_mode *adjusted_mode)
3444 {
3445         struct drm_device *dev = crtc->dev;
3446
3447         if (HAS_PCH_SPLIT(dev)) {
3448                 /* FDI link clock is fixed at 2.7G */
3449                 if (mode->clock * 3 > IRONLAKE_FDI_FREQ * 4)
3450                         return false;
3451         }
3452
3453         /* All interlaced capable intel hw wants timings in frames. Note though
3454          * that intel_lvds_mode_fixup does some funny tricks with the crtc
3455          * timings, so we need to be careful not to clobber these.*/
3456         if (!(adjusted_mode->private_flags & INTEL_MODE_CRTC_TIMINGS_SET))
3457                 drm_mode_set_crtcinfo(adjusted_mode, 0);
3458
3459         return true;
3460 }
3461
3462 static int i945_get_display_clock_speed(struct drm_device *dev)
3463 {
3464         return 400000;
3465 }
3466
3467 static int i915_get_display_clock_speed(struct drm_device *dev)
3468 {
3469         return 333000;
3470 }
3471
3472 static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
3473 {
3474         return 200000;
3475 }
3476
3477 static int i915gm_get_display_clock_speed(struct drm_device *dev)
3478 {
3479         u16 gcfgc = 0;
3480
3481         pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
3482
3483         if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
3484                 return 133000;
3485         else {
3486                 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
3487                 case GC_DISPLAY_CLOCK_333_MHZ:
3488                         return 333000;
3489                 default:
3490                 case GC_DISPLAY_CLOCK_190_200_MHZ:
3491                         return 190000;
3492                 }
3493         }
3494 }
3495
3496 static int i865_get_display_clock_speed(struct drm_device *dev)
3497 {
3498         return 266000;
3499 }
3500
3501 static int i855_get_display_clock_speed(struct drm_device *dev)
3502 {
3503         u16 hpllcc = 0;
3504         /* Assume that the hardware is in the high speed state.  This
3505          * should be the default.
3506          */
3507         switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
3508         case GC_CLOCK_133_200:
3509         case GC_CLOCK_100_200:
3510                 return 200000;
3511         case GC_CLOCK_166_250:
3512                 return 250000;
3513         case GC_CLOCK_100_133:
3514                 return 133000;
3515         }
3516
3517         /* Shouldn't happen */
3518         return 0;
3519 }
3520
3521 static int i830_get_display_clock_speed(struct drm_device *dev)
3522 {
3523         return 133000;
3524 }
3525
3526 struct fdi_m_n {
3527         u32        tu;
3528         u32        gmch_m;
3529         u32        gmch_n;
3530         u32        link_m;
3531         u32        link_n;
3532 };
3533
3534 static void
3535 fdi_reduce_ratio(u32 *num, u32 *den)
3536 {
3537         while (*num > 0xffffff || *den > 0xffffff) {
3538                 *num >>= 1;
3539                 *den >>= 1;
3540         }
3541 }
3542
3543 static void
3544 ironlake_compute_m_n(int bits_per_pixel, int nlanes, int pixel_clock,
3545                      int link_clock, struct fdi_m_n *m_n)
3546 {
3547         m_n->tu = 64; /* default size */
3548
3549         /* BUG_ON(pixel_clock > INT_MAX / 36); */
3550         m_n->gmch_m = bits_per_pixel * pixel_clock;
3551         m_n->gmch_n = link_clock * nlanes * 8;
3552         fdi_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
3553
3554         m_n->link_m = pixel_clock;
3555         m_n->link_n = link_clock;
3556         fdi_reduce_ratio(&m_n->link_m, &m_n->link_n);
3557 }
3558
3559
3560 struct intel_watermark_params {
3561         unsigned long fifo_size;
3562         unsigned long max_wm;
3563         unsigned long default_wm;
3564         unsigned long guard_size;
3565         unsigned long cacheline_size;
3566 };
3567
3568 /* Pineview has different values for various configs */
3569 static const struct intel_watermark_params pineview_display_wm = {
3570         PINEVIEW_DISPLAY_FIFO,
3571         PINEVIEW_MAX_WM,
3572         PINEVIEW_DFT_WM,
3573         PINEVIEW_GUARD_WM,
3574         PINEVIEW_FIFO_LINE_SIZE
3575 };
3576 static const struct intel_watermark_params pineview_display_hplloff_wm = {
3577         PINEVIEW_DISPLAY_FIFO,
3578         PINEVIEW_MAX_WM,
3579         PINEVIEW_DFT_HPLLOFF_WM,
3580         PINEVIEW_GUARD_WM,
3581         PINEVIEW_FIFO_LINE_SIZE
3582 };
3583 static const struct intel_watermark_params pineview_cursor_wm = {
3584         PINEVIEW_CURSOR_FIFO,
3585         PINEVIEW_CURSOR_MAX_WM,
3586         PINEVIEW_CURSOR_DFT_WM,
3587         PINEVIEW_CURSOR_GUARD_WM,
3588         PINEVIEW_FIFO_LINE_SIZE,
3589 };
3590 static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
3591         PINEVIEW_CURSOR_FIFO,
3592         PINEVIEW_CURSOR_MAX_WM,
3593         PINEVIEW_CURSOR_DFT_WM,
3594         PINEVIEW_CURSOR_GUARD_WM,
3595         PINEVIEW_FIFO_LINE_SIZE
3596 };
3597 static const struct intel_watermark_params g4x_wm_info = {
3598         G4X_FIFO_SIZE,
3599         G4X_MAX_WM,
3600         G4X_MAX_WM,
3601         2,
3602         G4X_FIFO_LINE_SIZE,
3603 };
3604 static const struct intel_watermark_params g4x_cursor_wm_info = {
3605         I965_CURSOR_FIFO,
3606         I965_CURSOR_MAX_WM,
3607         I965_CURSOR_DFT_WM,
3608         2,
3609         G4X_FIFO_LINE_SIZE,
3610 };
3611 static const struct intel_watermark_params i965_cursor_wm_info = {
3612         I965_CURSOR_FIFO,
3613         I965_CURSOR_MAX_WM,
3614         I965_CURSOR_DFT_WM,
3615         2,
3616         I915_FIFO_LINE_SIZE,
3617 };
3618 static const struct intel_watermark_params i945_wm_info = {
3619         I945_FIFO_SIZE,
3620         I915_MAX_WM,
3621         1,
3622         2,
3623         I915_FIFO_LINE_SIZE
3624 };
3625 static const struct intel_watermark_params i915_wm_info = {
3626         I915_FIFO_SIZE,
3627         I915_MAX_WM,
3628         1,
3629         2,
3630         I915_FIFO_LINE_SIZE
3631 };
3632 static const struct intel_watermark_params i855_wm_info = {
3633         I855GM_FIFO_SIZE,
3634         I915_MAX_WM,
3635         1,
3636         2,
3637         I830_FIFO_LINE_SIZE
3638 };
3639 static const struct intel_watermark_params i830_wm_info = {
3640         I830_FIFO_SIZE,
3641         I915_MAX_WM,
3642         1,
3643         2,
3644         I830_FIFO_LINE_SIZE
3645 };
3646
3647 static const struct intel_watermark_params ironlake_display_wm_info = {
3648         ILK_DISPLAY_FIFO,
3649         ILK_DISPLAY_MAXWM,
3650         ILK_DISPLAY_DFTWM,
3651         2,
3652         ILK_FIFO_LINE_SIZE
3653 };
3654 static const struct intel_watermark_params ironlake_cursor_wm_info = {
3655         ILK_CURSOR_FIFO,
3656         ILK_CURSOR_MAXWM,
3657         ILK_CURSOR_DFTWM,
3658         2,
3659         ILK_FIFO_LINE_SIZE
3660 };
3661 static const struct intel_watermark_params ironlake_display_srwm_info = {
3662         ILK_DISPLAY_SR_FIFO,
3663         ILK_DISPLAY_MAX_SRWM,
3664         ILK_DISPLAY_DFT_SRWM,
3665         2,
3666         ILK_FIFO_LINE_SIZE
3667 };
3668 static const struct intel_watermark_params ironlake_cursor_srwm_info = {
3669         ILK_CURSOR_SR_FIFO,
3670         ILK_CURSOR_MAX_SRWM,
3671         ILK_CURSOR_DFT_SRWM,
3672         2,
3673         ILK_FIFO_LINE_SIZE
3674 };
3675
3676 static const struct intel_watermark_params sandybridge_display_wm_info = {
3677         SNB_DISPLAY_FIFO,
3678         SNB_DISPLAY_MAXWM,
3679         SNB_DISPLAY_DFTWM,
3680         2,
3681         SNB_FIFO_LINE_SIZE
3682 };
3683 static const struct intel_watermark_params sandybridge_cursor_wm_info = {
3684         SNB_CURSOR_FIFO,
3685         SNB_CURSOR_MAXWM,
3686         SNB_CURSOR_DFTWM,
3687         2,
3688         SNB_FIFO_LINE_SIZE
3689 };
3690 static const struct intel_watermark_params sandybridge_display_srwm_info = {
3691         SNB_DISPLAY_SR_FIFO,
3692         SNB_DISPLAY_MAX_SRWM,
3693         SNB_DISPLAY_DFT_SRWM,
3694         2,
3695         SNB_FIFO_LINE_SIZE
3696 };
3697 static const struct intel_watermark_params sandybridge_cursor_srwm_info = {
3698         SNB_CURSOR_SR_FIFO,
3699         SNB_CURSOR_MAX_SRWM,
3700         SNB_CURSOR_DFT_SRWM,
3701         2,
3702         SNB_FIFO_LINE_SIZE
3703 };
3704
3705
3706 /**
3707  * intel_calculate_wm - calculate watermark level
3708  * @clock_in_khz: pixel clock
3709  * @wm: chip FIFO params
3710  * @pixel_size: display pixel size
3711  * @latency_ns: memory latency for the platform
3712  *
3713  * Calculate the watermark level (the level at which the display plane will
3714  * start fetching from memory again).  Each chip has a different display
3715  * FIFO size and allocation, so the caller needs to figure that out and pass
3716  * in the correct intel_watermark_params structure.
3717  *
3718  * As the pixel clock runs, the FIFO will be drained at a rate that depends
3719  * on the pixel size.  When it reaches the watermark level, it'll start
3720  * fetching FIFO line sized based chunks from memory until the FIFO fills
3721  * past the watermark point.  If the FIFO drains completely, a FIFO underrun
3722  * will occur, and a display engine hang could result.
3723  */
3724 static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
3725                                         const struct intel_watermark_params *wm,
3726                                         int fifo_size,
3727                                         int pixel_size,
3728                                         unsigned long latency_ns)
3729 {
3730         long entries_required, wm_size;
3731
3732         /*
3733          * Note: we need to make sure we don't overflow for various clock &
3734          * latency values.
3735          * clocks go from a few thousand to several hundred thousand.
3736          * latency is usually a few thousand
3737          */
3738         entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
3739                 1000;
3740         entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
3741
3742         DRM_DEBUG_KMS("FIFO entries required for mode: %ld\n", entries_required);
3743
3744         wm_size = fifo_size - (entries_required + wm->guard_size);
3745
3746         DRM_DEBUG_KMS("FIFO watermark level: %ld\n", wm_size);
3747
3748         /* Don't promote wm_size to unsigned... */
3749         if (wm_size > (long)wm->max_wm)
3750                 wm_size = wm->max_wm;
3751         if (wm_size <= 0)
3752                 wm_size = wm->default_wm;
3753         return wm_size;
3754 }
3755
3756 struct cxsr_latency {
3757         int is_desktop;
3758         int is_ddr3;
3759         unsigned long fsb_freq;
3760         unsigned long mem_freq;
3761         unsigned long display_sr;
3762         unsigned long display_hpll_disable;
3763         unsigned long cursor_sr;
3764         unsigned long cursor_hpll_disable;
3765 };
3766
3767 static const struct cxsr_latency cxsr_latency_table[] = {
3768         {1, 0, 800, 400, 3382, 33382, 3983, 33983},    /* DDR2-400 SC */
3769         {1, 0, 800, 667, 3354, 33354, 3807, 33807},    /* DDR2-667 SC */
3770         {1, 0, 800, 800, 3347, 33347, 3763, 33763},    /* DDR2-800 SC */
3771         {1, 1, 800, 667, 6420, 36420, 6873, 36873},    /* DDR3-667 SC */
3772         {1, 1, 800, 800, 5902, 35902, 6318, 36318},    /* DDR3-800 SC */
3773
3774         {1, 0, 667, 400, 3400, 33400, 4021, 34021},    /* DDR2-400 SC */
3775         {1, 0, 667, 667, 3372, 33372, 3845, 33845},    /* DDR2-667 SC */
3776         {1, 0, 667, 800, 3386, 33386, 3822, 33822},    /* DDR2-800 SC */
3777         {1, 1, 667, 667, 6438, 36438, 6911, 36911},    /* DDR3-667 SC */
3778         {1, 1, 667, 800, 5941, 35941, 6377, 36377},    /* DDR3-800 SC */
3779
3780         {1, 0, 400, 400, 3472, 33472, 4173, 34173},    /* DDR2-400 SC */
3781         {1, 0, 400, 667, 3443, 33443, 3996, 33996},    /* DDR2-667 SC */
3782         {1, 0, 400, 800, 3430, 33430, 3946, 33946},    /* DDR2-800 SC */
3783         {1, 1, 400, 667, 6509, 36509, 7062, 37062},    /* DDR3-667 SC */
3784         {1, 1, 400, 800, 5985, 35985, 6501, 36501},    /* DDR3-800 SC */
3785
3786         {0, 0, 800, 400, 3438, 33438, 4065, 34065},    /* DDR2-400 SC */
3787         {0, 0, 800, 667, 3410, 33410, 3889, 33889},    /* DDR2-667 SC */
3788         {0, 0, 800, 800, 3403, 33403, 3845, 33845},    /* DDR2-800 SC */
3789         {0, 1, 800, 667, 6476, 36476, 6955, 36955},    /* DDR3-667 SC */
3790         {0, 1, 800, 800, 5958, 35958, 6400, 36400},    /* DDR3-800 SC */
3791
3792         {0, 0, 667, 400, 3456, 33456, 4103, 34106},    /* DDR2-400 SC */
3793         {0, 0, 667, 667, 3428, 33428, 3927, 33927},    /* DDR2-667 SC */
3794         {0, 0, 667, 800, 3443, 33443, 3905, 33905},    /* DDR2-800 SC */
3795         {0, 1, 667, 667, 6494, 36494, 6993, 36993},    /* DDR3-667 SC */
3796         {0, 1, 667, 800, 5998, 35998, 6460, 36460},    /* DDR3-800 SC */
3797
3798         {0, 0, 400, 400, 3528, 33528, 4255, 34255},    /* DDR2-400 SC */
3799         {0, 0, 400, 667, 3500, 33500, 4079, 34079},    /* DDR2-667 SC */
3800         {0, 0, 400, 800, 3487, 33487, 4029, 34029},    /* DDR2-800 SC */
3801         {0, 1, 400, 667, 6566, 36566, 7145, 37145},    /* DDR3-667 SC */
3802         {0, 1, 400, 800, 6042, 36042, 6584, 36584},    /* DDR3-800 SC */
3803 };
3804
3805 static const struct cxsr_latency *intel_get_cxsr_latency(int is_desktop,
3806                                                          int is_ddr3,
3807                                                          int fsb,
3808                                                          int mem)
3809 {
3810         const struct cxsr_latency *latency;
3811         int i;
3812
3813         if (fsb == 0 || mem == 0)
3814                 return NULL;
3815
3816         for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
3817                 latency = &cxsr_latency_table[i];
3818                 if (is_desktop == latency->is_desktop &&
3819                     is_ddr3 == latency->is_ddr3 &&
3820                     fsb == latency->fsb_freq && mem == latency->mem_freq)
3821                         return latency;
3822         }
3823
3824         DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
3825
3826         return NULL;
3827 }
3828
3829 static void pineview_disable_cxsr(struct drm_device *dev)
3830 {
3831         struct drm_i915_private *dev_priv = dev->dev_private;
3832
3833         /* deactivate cxsr */
3834         I915_WRITE(DSPFW3, I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN);
3835 }
3836
3837 /*
3838  * Latency for FIFO fetches is dependent on several factors:
3839  *   - memory configuration (speed, channels)
3840  *   - chipset
3841  *   - current MCH state
3842  * It can be fairly high in some situations, so here we assume a fairly
3843  * pessimal value.  It's a tradeoff between extra memory fetches (if we
3844  * set this value too high, the FIFO will fetch frequently to stay full)
3845  * and power consumption (set it too low to save power and we might see
3846  * FIFO underruns and display "flicker").
3847  *
3848  * A value of 5us seems to be a good balance; safe for very low end
3849  * platforms but not overly aggressive on lower latency configs.
3850  */
3851 static const int latency_ns = 5000;
3852
3853 static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
3854 {
3855         struct drm_i915_private *dev_priv = dev->dev_private;
3856         uint32_t dsparb = I915_READ(DSPARB);
3857         int size;
3858
3859         size = dsparb & 0x7f;
3860         if (plane)
3861                 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
3862
3863         DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
3864                       plane ? "B" : "A", size);
3865
3866         return size;
3867 }
3868
3869 static int i85x_get_fifo_size(struct drm_device *dev, int plane)
3870 {
3871         struct drm_i915_private *dev_priv = dev->dev_private;
3872         uint32_t dsparb = I915_READ(DSPARB);
3873         int size;
3874
3875         size = dsparb & 0x1ff;
3876         if (plane)
3877                 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
3878         size >>= 1; /* Convert to cachelines */
3879
3880         DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
3881                       plane ? "B" : "A", size);
3882
3883         return size;
3884 }
3885
3886 static int i845_get_fifo_size(struct drm_device *dev, int plane)
3887 {
3888         struct drm_i915_private *dev_priv = dev->dev_private;
3889         uint32_t dsparb = I915_READ(DSPARB);
3890         int size;
3891
3892         size = dsparb & 0x7f;
3893         size >>= 2; /* Convert to cachelines */
3894
3895         DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
3896                       plane ? "B" : "A",
3897                       size);
3898
3899         return size;
3900 }
3901
3902 static int i830_get_fifo_size(struct drm_device *dev, int plane)
3903 {
3904         struct drm_i915_private *dev_priv = dev->dev_private;
3905         uint32_t dsparb = I915_READ(DSPARB);
3906         int size;
3907
3908         size = dsparb & 0x7f;
3909         size >>= 1; /* Convert to cachelines */
3910
3911         DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
3912                       plane ? "B" : "A", size);
3913
3914         return size;
3915 }
3916
3917 static struct drm_crtc *single_enabled_crtc(struct drm_device *dev)
3918 {
3919         struct drm_crtc *crtc, *enabled = NULL;
3920
3921         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
3922                 if (crtc->enabled && crtc->fb) {
3923                         if (enabled)
3924                                 return NULL;
3925                         enabled = crtc;
3926                 }
3927         }
3928
3929         return enabled;
3930 }
3931
3932 static void pineview_update_wm(struct drm_device *dev)
3933 {
3934         struct drm_i915_private *dev_priv = dev->dev_private;
3935         struct drm_crtc *crtc;
3936         const struct cxsr_latency *latency;
3937         u32 reg;
3938         unsigned long wm;
3939
3940         latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
3941                                          dev_priv->fsb_freq, dev_priv->mem_freq);
3942         if (!latency) {
3943                 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
3944                 pineview_disable_cxsr(dev);
3945                 return;
3946         }
3947
3948         crtc = single_enabled_crtc(dev);
3949         if (crtc) {
3950                 int clock = crtc->mode.clock;
3951                 int pixel_size = crtc->fb->bits_per_pixel / 8;
3952
3953                 /* Display SR */
3954                 wm = intel_calculate_wm(clock, &pineview_display_wm,
3955                                         pineview_display_wm.fifo_size,
3956                                         pixel_size, latency->display_sr);
3957                 reg = I915_READ(DSPFW1);
3958                 reg &= ~DSPFW_SR_MASK;
3959                 reg |= wm << DSPFW_SR_SHIFT;
3960                 I915_WRITE(DSPFW1, reg);
3961                 DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
3962
3963                 /* cursor SR */
3964                 wm = intel_calculate_wm(clock, &pineview_cursor_wm,
3965                                         pineview_display_wm.fifo_size,
3966                                         pixel_size, latency->cursor_sr);
3967                 reg = I915_READ(DSPFW3);
3968                 reg &= ~DSPFW_CURSOR_SR_MASK;
3969                 reg |= (wm & 0x3f) << DSPFW_CURSOR_SR_SHIFT;
3970                 I915_WRITE(DSPFW3, reg);
3971
3972                 /* Display HPLL off SR */
3973                 wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
3974                                         pineview_display_hplloff_wm.fifo_size,
3975                                         pixel_size, latency->display_hpll_disable);
3976                 reg = I915_READ(DSPFW3);
3977                 reg &= ~DSPFW_HPLL_SR_MASK;
3978                 reg |= wm & DSPFW_HPLL_SR_MASK;
3979                 I915_WRITE(DSPFW3, reg);
3980
3981                 /* cursor HPLL off SR */
3982                 wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
3983                                         pineview_display_hplloff_wm.fifo_size,
3984                                         pixel_size, latency->cursor_hpll_disable);
3985                 reg = I915_READ(DSPFW3);
3986                 reg &= ~DSPFW_HPLL_CURSOR_MASK;
3987                 reg |= (wm & 0x3f) << DSPFW_HPLL_CURSOR_SHIFT;
3988                 I915_WRITE(DSPFW3, reg);
3989                 DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
3990
3991                 /* activate cxsr */
3992                 I915_WRITE(DSPFW3,
3993                            I915_READ(DSPFW3) | PINEVIEW_SELF_REFRESH_EN);
3994                 DRM_DEBUG_KMS("Self-refresh is enabled\n");
3995         } else {
3996                 pineview_disable_cxsr(dev);
3997                 DRM_DEBUG_KMS("Self-refresh is disabled\n");
3998         }
3999 }
4000
4001 static bool g4x_compute_wm0(struct drm_device *dev,
4002                             int plane,
4003                             const struct intel_watermark_params *display,
4004                             int display_latency_ns,
4005                             const struct intel_watermark_params *cursor,
4006                             int cursor_latency_ns,
4007                             int *plane_wm,
4008                             int *cursor_wm)
4009 {
4010         struct drm_crtc *crtc;
4011         int htotal, hdisplay, clock, pixel_size;
4012         int line_time_us, line_count;
4013         int entries, tlb_miss;
4014
4015         crtc = intel_get_crtc_for_plane(dev, plane);
4016         if (crtc->fb == NULL || !crtc->enabled) {
4017                 *cursor_wm = cursor->guard_size;
4018                 *plane_wm = display->guard_size;
4019                 return false;
4020         }
4021
4022         htotal = crtc->mode.htotal;
4023         hdisplay = crtc->mode.hdisplay;
4024         clock = crtc->mode.clock;
4025         pixel_size = crtc->fb->bits_per_pixel / 8;
4026
4027         /* Use the small buffer method to calculate plane watermark */
4028         entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
4029         tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
4030         if (tlb_miss > 0)
4031                 entries += tlb_miss;
4032         entries = DIV_ROUND_UP(entries, display->cacheline_size);
4033         *plane_wm = entries + display->guard_size;
4034         if (*plane_wm > (int)display->max_wm)
4035                 *plane_wm = display->max_wm;
4036
4037         /* Use the large buffer method to calculate cursor watermark */
4038         line_time_us = ((htotal * 1000) / clock);
4039         line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
4040         entries = line_count * 64 * pixel_size;
4041         tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
4042         if (tlb_miss > 0)
4043                 entries += tlb_miss;
4044         entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
4045         *cursor_wm = entries + cursor->guard_size;
4046         if (*cursor_wm > (int)cursor->max_wm)
4047                 *cursor_wm = (int)cursor->max_wm;
4048
4049         return true;
4050 }
4051
4052 /*
4053  * Check the wm result.
4054  *
4055  * If any calculated watermark values is larger than the maximum value that
4056  * can be programmed into the associated watermark register, that watermark
4057  * must be disabled.
4058  */
4059 static bool g4x_check_srwm(struct drm_device *dev,
4060                            int display_wm, int cursor_wm,
4061                            const struct intel_watermark_params *display,
4062                            const struct intel_watermark_params *cursor)
4063 {
4064         DRM_DEBUG_KMS("SR watermark: display plane %d, cursor %d\n",
4065                       display_wm, cursor_wm);
4066
4067         if (display_wm > display->max_wm) {
4068                 DRM_DEBUG_KMS("display watermark is too large(%d/%ld), disabling\n",
4069                               display_wm, display->max_wm);
4070                 return false;
4071         }
4072
4073         if (cursor_wm > cursor->max_wm) {
4074                 DRM_DEBUG_KMS("cursor watermark is too large(%d/%ld), disabling\n",
4075                               cursor_wm, cursor->max_wm);
4076                 return false;
4077         }
4078
4079         if (!(display_wm || cursor_wm)) {
4080                 DRM_DEBUG_KMS("SR latency is 0, disabling\n");
4081                 return false;
4082         }
4083
4084         return true;
4085 }
4086
4087 static bool g4x_compute_srwm(struct drm_device *dev,
4088                              int plane,
4089                              int latency_ns,
4090                              const struct intel_watermark_params *display,
4091                              const struct intel_watermark_params *cursor,
4092                              int *display_wm, int *cursor_wm)
4093 {
4094         struct drm_crtc *crtc;
4095         int hdisplay, htotal, pixel_size, clock;
4096         unsigned long line_time_us;
4097         int line_count, line_size;
4098         int small, large;
4099         int entries;
4100
4101         if (!latency_ns) {
4102                 *display_wm = *cursor_wm = 0;
4103                 return false;
4104         }
4105
4106         crtc = intel_get_crtc_for_plane(dev, plane);
4107         hdisplay = crtc->mode.hdisplay;
4108         htotal = crtc->mode.htotal;
4109         clock = crtc->mode.clock;
4110         pixel_size = crtc->fb->bits_per_pixel / 8;
4111
4112         line_time_us = (htotal * 1000) / clock;
4113         line_count = (latency_ns / line_time_us + 1000) / 1000;
4114         line_size = hdisplay * pixel_size;
4115
4116         /* Use the minimum of the small and large buffer method for primary */
4117         small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
4118         large = line_count * line_size;
4119
4120         entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
4121         *display_wm = entries + display->guard_size;
4122
4123         /* calculate the self-refresh watermark for display cursor */
4124         entries = line_count * pixel_size * 64;
4125         entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
4126         *cursor_wm = entries + cursor->guard_size;
4127
4128         return g4x_check_srwm(dev,
4129                               *display_wm, *cursor_wm,
4130                               display, cursor);
4131 }
4132
4133 #define single_plane_enabled(mask) is_power_of_2(mask)
4134
4135 static void g4x_update_wm(struct drm_device *dev)
4136 {
4137         static const int sr_latency_ns = 12000;
4138         struct drm_i915_private *dev_priv = dev->dev_private;
4139         int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
4140         int plane_sr, cursor_sr;
4141         unsigned int enabled = 0;
4142
4143         if (g4x_compute_wm0(dev, 0,
4144                             &g4x_wm_info, latency_ns,
4145                             &g4x_cursor_wm_info, latency_ns,
4146                             &planea_wm, &cursora_wm))
4147                 enabled |= 1;
4148
4149         if (g4x_compute_wm0(dev, 1,
4150                             &g4x_wm_info, latency_ns,
4151                             &g4x_cursor_wm_info, latency_ns,
4152                             &planeb_wm, &cursorb_wm))
4153                 enabled |= 2;
4154
4155         plane_sr = cursor_sr = 0;
4156         if (single_plane_enabled(enabled) &&
4157             g4x_compute_srwm(dev, ffs(enabled) - 1,
4158                              sr_latency_ns,
4159                              &g4x_wm_info,
4160                              &g4x_cursor_wm_info,
4161                              &plane_sr, &cursor_sr))
4162                 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
4163         else
4164                 I915_WRITE(FW_BLC_SELF,
4165                            I915_READ(FW_BLC_SELF) & ~FW_BLC_SELF_EN);
4166
4167         DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
4168                       planea_wm, cursora_wm,
4169                       planeb_wm, cursorb_wm,
4170                       plane_sr, cursor_sr);
4171
4172         I915_WRITE(DSPFW1,
4173                    (plane_sr << DSPFW_SR_SHIFT) |
4174                    (cursorb_wm << DSPFW_CURSORB_SHIFT) |
4175                    (planeb_wm << DSPFW_PLANEB_SHIFT) |
4176                    planea_wm);
4177         I915_WRITE(DSPFW2,
4178                    (I915_READ(DSPFW2) & DSPFW_CURSORA_MASK) |
4179                    (cursora_wm << DSPFW_CURSORA_SHIFT));
4180         /* HPLL off in SR has some issues on G4x... disable it */
4181         I915_WRITE(DSPFW3,
4182                    (I915_READ(DSPFW3) & ~DSPFW_HPLL_SR_EN) |
4183                    (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
4184 }
4185
4186 static void i965_update_wm(struct drm_device *dev)
4187 {
4188         struct drm_i915_private *dev_priv = dev->dev_private;
4189         struct drm_crtc *crtc;
4190         int srwm = 1;
4191         int cursor_sr = 16;
4192
4193         /* Calc sr entries for one plane configs */
4194         crtc = single_enabled_crtc(dev);
4195         if (crtc) {
4196                 /* self-refresh has much higher latency */
4197                 static const int sr_latency_ns = 12000;
4198                 int clock = crtc->mode.clock;
4199                 int htotal = crtc->mode.htotal;
4200                 int hdisplay = crtc->mode.hdisplay;
4201                 int pixel_size = crtc->fb->bits_per_pixel / 8;
4202                 unsigned long line_time_us;
4203                 int entries;
4204
4205                 line_time_us = ((htotal * 1000) / clock);
4206
4207                 /* Use ns/us then divide to preserve precision */
4208                 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
4209                         pixel_size * hdisplay;
4210                 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
4211                 srwm = I965_FIFO_SIZE - entries;
4212                 if (srwm < 0)
4213                         srwm = 1;
4214                 srwm &= 0x1ff;
4215                 DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
4216                               entries, srwm);
4217
4218                 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
4219                         pixel_size * 64;
4220                 entries = DIV_ROUND_UP(entries,
4221                                           i965_cursor_wm_info.cacheline_size);
4222                 cursor_sr = i965_cursor_wm_info.fifo_size -
4223                         (entries + i965_cursor_wm_info.guard_size);
4224
4225                 if (cursor_sr > i965_cursor_wm_info.max_wm)
4226                         cursor_sr = i965_cursor_wm_info.max_wm;
4227
4228                 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
4229                               "cursor %d\n", srwm, cursor_sr);
4230
4231                 if (IS_CRESTLINE(dev))
4232                         I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
4233         } else {
4234                 /* Turn off self refresh if both pipes are enabled */
4235                 if (IS_CRESTLINE(dev))
4236                         I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
4237                                    & ~FW_BLC_SELF_EN);
4238         }
4239
4240         DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
4241                       srwm);
4242
4243         /* 965 has limitations... */
4244         I915_WRITE(DSPFW1, (srwm << DSPFW_SR_SHIFT) |
4245                    (8 << 16) | (8 << 8) | (8 << 0));
4246         I915_WRITE(DSPFW2, (8 << 8) | (8 << 0));
4247         /* update cursor SR watermark */
4248         I915_WRITE(DSPFW3, (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
4249 }
4250
4251 static void i9xx_update_wm(struct drm_device *dev)
4252 {
4253         struct drm_i915_private *dev_priv = dev->dev_private;
4254         const struct intel_watermark_params *wm_info;
4255         uint32_t fwater_lo;
4256         uint32_t fwater_hi;
4257         int cwm, srwm = 1;
4258         int fifo_size;
4259         int planea_wm, planeb_wm;
4260         struct drm_crtc *crtc, *enabled = NULL;
4261
4262         if (IS_I945GM(dev))
4263                 wm_info = &i945_wm_info;
4264         else if (!IS_GEN2(dev))
4265                 wm_info = &i915_wm_info;
4266         else
4267                 wm_info = &i855_wm_info;
4268
4269         fifo_size = dev_priv->display.get_fifo_size(dev, 0);
4270         crtc = intel_get_crtc_for_plane(dev, 0);
4271         if (crtc->enabled && crtc->fb) {
4272                 planea_wm = intel_calculate_wm(crtc->mode.clock,
4273                                                wm_info, fifo_size,
4274                                                crtc->fb->bits_per_pixel / 8,
4275                                                latency_ns);
4276                 enabled = crtc;
4277         } else
4278                 planea_wm = fifo_size - wm_info->guard_size;
4279
4280         fifo_size = dev_priv->display.get_fifo_size(dev, 1);
4281         crtc = intel_get_crtc_for_plane(dev, 1);
4282         if (crtc->enabled && crtc->fb) {
4283                 planeb_wm = intel_calculate_wm(crtc->mode.clock,
4284                                                wm_info, fifo_size,
4285                                                crtc->fb->bits_per_pixel / 8,
4286                                                latency_ns);
4287                 if (enabled == NULL)
4288                         enabled = crtc;
4289                 else
4290                         enabled = NULL;
4291         } else
4292                 planeb_wm = fifo_size - wm_info->guard_size;
4293
4294         DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
4295
4296         /*
4297          * Overlay gets an aggressive default since video jitter is bad.
4298          */
4299         cwm = 2;
4300
4301         /* Play safe and disable self-refresh before adjusting watermarks. */
4302         if (IS_I945G(dev) || IS_I945GM(dev))
4303                 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN_MASK | 0);
4304         else if (IS_I915GM(dev))
4305                 I915_WRITE(INSTPM, I915_READ(INSTPM) & ~INSTPM_SELF_EN);
4306
4307         /* Calc sr entries for one plane configs */
4308         if (HAS_FW_BLC(dev) && enabled) {
4309                 /* self-refresh has much higher latency */
4310                 static const int sr_latency_ns = 6000;
4311                 int clock = enabled->mode.clock;
4312                 int htotal = enabled->mode.htotal;
4313                 int hdisplay = enabled->mode.hdisplay;
4314                 int pixel_size = enabled->fb->bits_per_pixel / 8;
4315                 unsigned long line_time_us;
4316                 int entries;
4317
4318                 line_time_us = (htotal * 1000) / clock;
4319
4320                 /* Use ns/us then divide to preserve precision */
4321                 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
4322                         pixel_size * hdisplay;
4323                 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
4324                 DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
4325                 srwm = wm_info->fifo_size - entries;
4326                 if (srwm < 0)
4327                         srwm = 1;
4328
4329                 if (IS_I945G(dev) || IS_I945GM(dev))
4330                         I915_WRITE(FW_BLC_SELF,
4331                                    FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
4332                 else if (IS_I915GM(dev))
4333                         I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
4334         }
4335
4336         DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
4337                       planea_wm, planeb_wm, cwm, srwm);
4338
4339         fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
4340         fwater_hi = (cwm & 0x1f);
4341
4342         /* Set request length to 8 cachelines per fetch */
4343         fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
4344         fwater_hi = fwater_hi | (1 << 8);
4345
4346         I915_WRITE(FW_BLC, fwater_lo);
4347         I915_WRITE(FW_BLC2, fwater_hi);
4348
4349         if (HAS_FW_BLC(dev)) {
4350                 if (enabled) {
4351                         if (IS_I945G(dev) || IS_I945GM(dev))
4352                                 I915_WRITE(FW_BLC_SELF,
4353                                            FW_BLC_SELF_EN_MASK | FW_BLC_SELF_EN);
4354                         else if (IS_I915GM(dev))
4355                                 I915_WRITE(INSTPM, I915_READ(INSTPM) | INSTPM_SELF_EN);
4356                         DRM_DEBUG_KMS("memory self refresh enabled\n");
4357                 } else
4358                         DRM_DEBUG_KMS("memory self refresh disabled\n");
4359         }
4360 }
4361
4362 static void i830_update_wm(struct drm_device *dev)
4363 {
4364         struct drm_i915_private *dev_priv = dev->dev_private;
4365         struct drm_crtc *crtc;
4366         uint32_t fwater_lo;
4367         int planea_wm;
4368
4369         crtc = single_enabled_crtc(dev);
4370         if (crtc == NULL)
4371                 return;
4372
4373         planea_wm = intel_calculate_wm(crtc->mode.clock, &i830_wm_info,
4374                                        dev_priv->display.get_fifo_size(dev, 0),
4375                                        crtc->fb->bits_per_pixel / 8,
4376                                        latency_ns);
4377         fwater_lo = I915_READ(FW_BLC) & ~0xfff;
4378         fwater_lo |= (3<<8) | planea_wm;
4379
4380         DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
4381
4382         I915_WRITE(FW_BLC, fwater_lo);
4383 }
4384
4385 #define ILK_LP0_PLANE_LATENCY           700
4386 #define ILK_LP0_CURSOR_LATENCY          1300
4387
4388 /*
4389  * Check the wm result.
4390  *
4391  * If any calculated watermark values is larger than the maximum value that
4392  * can be programmed into the associated watermark register, that watermark
4393  * must be disabled.
4394  */
4395 static bool ironlake_check_srwm(struct drm_device *dev, int level,
4396                                 int fbc_wm, int display_wm, int cursor_wm,
4397                                 const struct intel_watermark_params *display,
4398                                 const struct intel_watermark_params *cursor)
4399 {
4400         struct drm_i915_private *dev_priv = dev->dev_private;
4401
4402         DRM_DEBUG_KMS("watermark %d: display plane %d, fbc lines %d,"
4403                       " cursor %d\n", level, display_wm, fbc_wm, cursor_wm);
4404
4405         if (fbc_wm > SNB_FBC_MAX_SRWM) {
4406                 DRM_DEBUG_KMS("fbc watermark(%d) is too large(%d), disabling wm%d+\n",
4407                               fbc_wm, SNB_FBC_MAX_SRWM, level);
4408
4409                 /* fbc has it's own way to disable FBC WM */
4410                 I915_WRITE(DISP_ARB_CTL,
4411                            I915_READ(DISP_ARB_CTL) | DISP_FBC_WM_DIS);
4412                 return false;
4413         }
4414
4415         if (display_wm > display->max_wm) {
4416                 DRM_DEBUG_KMS("display watermark(%d) is too large(%d), disabling wm%d+\n",
4417                               display_wm, SNB_DISPLAY_MAX_SRWM, level);
4418                 return false;
4419         }
4420
4421         if (cursor_wm > cursor->max_wm) {
4422                 DRM_DEBUG_KMS("cursor watermark(%d) is too large(%d), disabling wm%d+\n",
4423                               cursor_wm, SNB_CURSOR_MAX_SRWM, level);
4424                 return false;
4425         }
4426
4427         if (!(fbc_wm || display_wm || cursor_wm)) {
4428                 DRM_DEBUG_KMS("latency %d is 0, disabling wm%d+\n", level, level);
4429                 return false;
4430         }
4431
4432         return true;
4433 }
4434
4435 /*
4436  * Compute watermark values of WM[1-3],
4437  */
4438 static bool ironlake_compute_srwm(struct drm_device *dev, int level, int plane,
4439                                   int latency_ns,
4440                                   const struct intel_watermark_params *display,
4441                                   const struct intel_watermark_params *cursor,
4442                                   int *fbc_wm, int *display_wm, int *cursor_wm)
4443 {
4444         struct drm_crtc *crtc;
4445         unsigned long line_time_us;
4446         int hdisplay, htotal, pixel_size, clock;
4447         int line_count, line_size;
4448         int small, large;
4449         int entries;
4450
4451         if (!latency_ns) {
4452                 *fbc_wm = *display_wm = *cursor_wm = 0;
4453                 return false;
4454         }
4455
4456         crtc = intel_get_crtc_for_plane(dev, plane);
4457         hdisplay = crtc->mode.hdisplay;
4458         htotal = crtc->mode.htotal;
4459         clock = crtc->mode.clock;
4460         pixel_size = crtc->fb->bits_per_pixel / 8;
4461
4462         line_time_us = (htotal * 1000) / clock;
4463         line_count = (latency_ns / line_time_us + 1000) / 1000;
4464         line_size = hdisplay * pixel_size;
4465
4466         /* Use the minimum of the small and large buffer method for primary */
4467         small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
4468         large = line_count * line_size;
4469
4470         entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
4471         *display_wm = entries + display->guard_size;
4472
4473         /*
4474          * Spec says:
4475          * FBC WM = ((Final Primary WM * 64) / number of bytes per line) + 2
4476          */
4477         *fbc_wm = DIV_ROUND_UP(*display_wm * 64, line_size) + 2;
4478
4479         /* calculate the self-refresh watermark for display cursor */
4480         entries = line_count * pixel_size * 64;
4481         entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
4482         *cursor_wm = entries + cursor->guard_size;
4483
4484         return ironlake_check_srwm(dev, level,
4485                                    *fbc_wm, *display_wm, *cursor_wm,
4486                                    display, cursor);
4487 }
4488
4489 static void ironlake_update_wm(struct drm_device *dev)
4490 {
4491         struct drm_i915_private *dev_priv = dev->dev_private;
4492         int fbc_wm, plane_wm, cursor_wm;
4493         unsigned int enabled;
4494
4495         enabled = 0;
4496         if (g4x_compute_wm0(dev, 0,
4497                             &ironlake_display_wm_info,
4498                             ILK_LP0_PLANE_LATENCY,
4499                             &ironlake_cursor_wm_info,
4500                             ILK_LP0_CURSOR_LATENCY,
4501                             &plane_wm, &cursor_wm)) {
4502                 I915_WRITE(WM0_PIPEA_ILK,
4503                            (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
4504                 DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
4505                               " plane %d, " "cursor: %d\n",
4506                               plane_wm, cursor_wm);
4507                 enabled |= 1;
4508         }
4509
4510         if (g4x_compute_wm0(dev, 1,
4511                             &ironlake_display_wm_info,
4512                             ILK_LP0_PLANE_LATENCY,
4513                             &ironlake_cursor_wm_info,
4514                             ILK_LP0_CURSOR_LATENCY,
4515                             &plane_wm, &cursor_wm)) {
4516                 I915_WRITE(WM0_PIPEB_ILK,
4517                            (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
4518                 DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
4519                               " plane %d, cursor: %d\n",
4520                               plane_wm, cursor_wm);
4521                 enabled |= 2;
4522         }
4523
4524         /*
4525          * Calculate and update the self-refresh watermark only when one
4526          * display plane is used.
4527          */
4528         I915_WRITE(WM3_LP_ILK, 0);
4529         I915_WRITE(WM2_LP_ILK, 0);
4530         I915_WRITE(WM1_LP_ILK, 0);
4531
4532         if (!single_plane_enabled(enabled))
4533                 return;
4534         enabled = ffs(enabled) - 1;
4535
4536         /* WM1 */
4537         if (!ironlake_compute_srwm(dev, 1, enabled,
4538                                    ILK_READ_WM1_LATENCY() * 500,
4539                                    &ironlake_display_srwm_info,
4540                                    &ironlake_cursor_srwm_info,
4541                                    &fbc_wm, &plane_wm, &cursor_wm))
4542                 return;
4543
4544         I915_WRITE(WM1_LP_ILK,
4545                    WM1_LP_SR_EN |
4546                    (ILK_READ_WM1_LATENCY() << WM1_LP_LATENCY_SHIFT) |
4547                    (fbc_wm << WM1_LP_FBC_SHIFT) |
4548                    (plane_wm << WM1_LP_SR_SHIFT) |
4549                    cursor_wm);
4550
4551         /* WM2 */
4552         if (!ironlake_compute_srwm(dev, 2, enabled,
4553                                    ILK_READ_WM2_LATENCY() * 500,
4554                                    &ironlake_display_srwm_info,
4555                                    &ironlake_cursor_srwm_info,
4556                                    &fbc_wm, &plane_wm, &cursor_wm))
4557                 return;
4558
4559         I915_WRITE(WM2_LP_ILK,
4560                    WM2_LP_EN |
4561                    (ILK_READ_WM2_LATENCY() << WM1_LP_LATENCY_SHIFT) |
4562                    (fbc_wm << WM1_LP_FBC_SHIFT) |
4563                    (plane_wm << WM1_LP_SR_SHIFT) |
4564                    cursor_wm);
4565
4566         /*
4567          * WM3 is unsupported on ILK, probably because we don't have latency
4568          * data for that power state
4569          */
4570 }
4571
4572 void sandybridge_update_wm(struct drm_device *dev)
4573 {
4574         struct drm_i915_private *dev_priv = dev->dev_private;
4575         int latency = SNB_READ_WM0_LATENCY() * 100;     /* In unit 0.1us */
4576         u32 val;
4577         int fbc_wm, plane_wm, cursor_wm;
4578         unsigned int enabled;
4579
4580         enabled = 0;
4581         if (g4x_compute_wm0(dev, 0,
4582                             &sandybridge_display_wm_info, latency,
4583                             &sandybridge_cursor_wm_info, latency,
4584                             &plane_wm, &cursor_wm)) {
4585                 val = I915_READ(WM0_PIPEA_ILK);
4586                 val &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
4587                 I915_WRITE(WM0_PIPEA_ILK, val |
4588                            ((plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm));
4589                 DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
4590                               " plane %d, " "cursor: %d\n",
4591                               plane_wm, cursor_wm);
4592                 enabled |= 1;
4593         }
4594
4595         if (g4x_compute_wm0(dev, 1,
4596                             &sandybridge_display_wm_info, latency,
4597                             &sandybridge_cursor_wm_info, latency,
4598                             &plane_wm, &cursor_wm)) {
4599                 val = I915_READ(WM0_PIPEB_ILK);
4600                 val &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
4601                 I915_WRITE(WM0_PIPEB_ILK, val |
4602                            ((plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm));
4603                 DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
4604                               " plane %d, cursor: %d\n",
4605                               plane_wm, cursor_wm);
4606                 enabled |= 2;
4607         }
4608
4609         /* IVB has 3 pipes */
4610         if (IS_IVYBRIDGE(dev) &&
4611             g4x_compute_wm0(dev, 2,
4612                             &sandybridge_display_wm_info, latency,
4613                             &sandybridge_cursor_wm_info, latency,
4614                             &plane_wm, &cursor_wm)) {
4615                 val = I915_READ(WM0_PIPEC_IVB);
4616                 val &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
4617                 I915_WRITE(WM0_PIPEC_IVB, val |
4618                            ((plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm));
4619                 DRM_DEBUG_KMS("FIFO watermarks For pipe C -"
4620                               " plane %d, cursor: %d\n",
4621                               plane_wm, cursor_wm);
4622                 enabled |= 3;
4623         }
4624
4625         /*
4626          * Calculate and update the self-refresh watermark only when one
4627          * display plane is used.
4628          *
4629          * SNB support 3 levels of watermark.
4630          *
4631          * WM1/WM2/WM2 watermarks have to be enabled in the ascending order,
4632          * and disabled in the descending order
4633          *
4634          */
4635         I915_WRITE(WM3_LP_ILK, 0);
4636         I915_WRITE(WM2_LP_ILK, 0);
4637         I915_WRITE(WM1_LP_ILK, 0);
4638
4639         if (!single_plane_enabled(enabled) ||
4640             dev_priv->sprite_scaling_enabled)
4641                 return;
4642         enabled = ffs(enabled) - 1;
4643
4644         /* WM1 */
4645         if (!ironlake_compute_srwm(dev, 1, enabled,
4646                                    SNB_READ_WM1_LATENCY() * 500,
4647                                    &sandybridge_display_srwm_info,
4648                                    &sandybridge_cursor_srwm_info,
4649                                    &fbc_wm, &plane_wm, &cursor_wm))
4650                 return;
4651
4652         I915_WRITE(WM1_LP_ILK,
4653                    WM1_LP_SR_EN |
4654                    (SNB_READ_WM1_LATENCY() << WM1_LP_LATENCY_SHIFT) |
4655                    (fbc_wm << WM1_LP_FBC_SHIFT) |
4656                    (plane_wm << WM1_LP_SR_SHIFT) |
4657                    cursor_wm);
4658
4659         /* WM2 */
4660         if (!ironlake_compute_srwm(dev, 2, enabled,
4661                                    SNB_READ_WM2_LATENCY() * 500,
4662                                    &sandybridge_display_srwm_info,
4663                                    &sandybridge_cursor_srwm_info,
4664                                    &fbc_wm, &plane_wm, &cursor_wm))
4665                 return;
4666
4667         I915_WRITE(WM2_LP_ILK,
4668                    WM2_LP_EN |
4669                    (SNB_READ_WM2_LATENCY() << WM1_LP_LATENCY_SHIFT) |
4670                    (fbc_wm << WM1_LP_FBC_SHIFT) |
4671                    (plane_wm << WM1_LP_SR_SHIFT) |
4672                    cursor_wm);
4673
4674         /* WM3 */
4675         if (!ironlake_compute_srwm(dev, 3, enabled,
4676                                    SNB_READ_WM3_LATENCY() * 500,
4677                                    &sandybridge_display_srwm_info,
4678                                    &sandybridge_cursor_srwm_info,
4679                                    &fbc_wm, &plane_wm, &cursor_wm))
4680                 return;
4681
4682         I915_WRITE(WM3_LP_ILK,
4683                    WM3_LP_EN |
4684                    (SNB_READ_WM3_LATENCY() << WM1_LP_LATENCY_SHIFT) |
4685                    (fbc_wm << WM1_LP_FBC_SHIFT) |
4686                    (plane_wm << WM1_LP_SR_SHIFT) |
4687                    cursor_wm);
4688 }
4689
4690 static bool
4691 sandybridge_compute_sprite_wm(struct drm_device *dev, int plane,
4692                               uint32_t sprite_width, int pixel_size,
4693                               const struct intel_watermark_params *display,
4694                               int display_latency_ns, int *sprite_wm)
4695 {
4696         struct drm_crtc *crtc;
4697         int clock;
4698         int entries, tlb_miss;
4699
4700         crtc = intel_get_crtc_for_plane(dev, plane);
4701         if (crtc->fb == NULL || !crtc->enabled) {
4702                 *sprite_wm = display->guard_size;
4703                 return false;
4704         }
4705
4706         clock = crtc->mode.clock;
4707
4708         /* Use the small buffer method to calculate the sprite watermark */
4709         entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
4710         tlb_miss = display->fifo_size*display->cacheline_size -
4711                 sprite_width * 8;
4712         if (tlb_miss > 0)
4713                 entries += tlb_miss;
4714         entries = DIV_ROUND_UP(entries, display->cacheline_size);
4715         *sprite_wm = entries + display->guard_size;
4716         if (*sprite_wm > (int)display->max_wm)
4717                 *sprite_wm = display->max_wm;
4718
4719         return true;
4720 }
4721
4722 static bool
4723 sandybridge_compute_sprite_srwm(struct drm_device *dev, int plane,
4724                                 uint32_t sprite_width, int pixel_size,
4725                                 const struct intel_watermark_params *display,
4726                                 int latency_ns, int *sprite_wm)
4727 {
4728         struct drm_crtc *crtc;
4729         unsigned long line_time_us;
4730         int clock;
4731         int line_count, line_size;
4732         int small, large;
4733         int entries;
4734
4735         if (!latency_ns) {
4736                 *sprite_wm = 0;
4737                 return false;
4738         }
4739
4740         crtc = intel_get_crtc_for_plane(dev, plane);
4741         clock = crtc->mode.clock;
4742         if (!clock) {
4743                 *sprite_wm = 0;
4744                 return false;
4745         }
4746
4747         line_time_us = (sprite_width * 1000) / clock;
4748         if (!line_time_us) {
4749                 *sprite_wm = 0;
4750                 return false;
4751         }
4752
4753         line_count = (latency_ns / line_time_us + 1000) / 1000;
4754         line_size = sprite_width * pixel_size;
4755
4756         /* Use the minimum of the small and large buffer method for primary */
4757         small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
4758         large = line_count * line_size;
4759
4760         entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
4761         *sprite_wm = entries + display->guard_size;
4762
4763         return *sprite_wm > 0x3ff ? false : true;
4764 }
4765
4766 static void sandybridge_update_sprite_wm(struct drm_device *dev, int pipe,
4767                                          uint32_t sprite_width, int pixel_size)
4768 {
4769         struct drm_i915_private *dev_priv = dev->dev_private;
4770         int latency = SNB_READ_WM0_LATENCY() * 100;     /* In unit 0.1us */
4771         u32 val;
4772         int sprite_wm, reg;
4773         int ret;
4774
4775         switch (pipe) {
4776         case 0:
4777                 reg = WM0_PIPEA_ILK;
4778                 break;
4779         case 1:
4780                 reg = WM0_PIPEB_ILK;
4781                 break;
4782         case 2:
4783                 reg = WM0_PIPEC_IVB;
4784                 break;
4785         default:
4786                 return; /* bad pipe */
4787         }
4788
4789         ret = sandybridge_compute_sprite_wm(dev, pipe, sprite_width, pixel_size,
4790                                             &sandybridge_display_wm_info,
4791                                             latency, &sprite_wm);
4792         if (!ret) {
4793                 DRM_DEBUG_KMS("failed to compute sprite wm for pipe %d\n",
4794                               pipe);
4795                 return;
4796         }
4797
4798         val = I915_READ(reg);
4799         val &= ~WM0_PIPE_SPRITE_MASK;
4800         I915_WRITE(reg, val | (sprite_wm << WM0_PIPE_SPRITE_SHIFT));
4801         DRM_DEBUG_KMS("sprite watermarks For pipe %d - %d\n", pipe, sprite_wm);
4802
4803
4804         ret = sandybridge_compute_sprite_srwm(dev, pipe, sprite_width,
4805                                               pixel_size,
4806                                               &sandybridge_display_srwm_info,
4807                                               SNB_READ_WM1_LATENCY() * 500,
4808                                               &sprite_wm);
4809         if (!ret) {
4810                 DRM_DEBUG_KMS("failed to compute sprite lp1 wm on pipe %d\n",
4811                               pipe);
4812                 return;
4813         }
4814         I915_WRITE(WM1S_LP_ILK, sprite_wm);
4815
4816         /* Only IVB has two more LP watermarks for sprite */
4817         if (!IS_IVYBRIDGE(dev))
4818                 return;
4819
4820         ret = sandybridge_compute_sprite_srwm(dev, pipe, sprite_width,
4821                                               pixel_size,
4822                                               &sandybridge_display_srwm_info,
4823                                               SNB_READ_WM2_LATENCY() * 500,
4824                                               &sprite_wm);
4825         if (!ret) {
4826                 DRM_DEBUG_KMS("failed to compute sprite lp2 wm on pipe %d\n",
4827                               pipe);
4828                 return;
4829         }
4830         I915_WRITE(WM2S_LP_IVB, sprite_wm);
4831
4832         ret = sandybridge_compute_sprite_srwm(dev, pipe, sprite_width,
4833                                               pixel_size,
4834                                               &sandybridge_display_srwm_info,
4835                                               SNB_READ_WM3_LATENCY() * 500,
4836                                               &sprite_wm);
4837         if (!ret) {
4838                 DRM_DEBUG_KMS("failed to compute sprite lp3 wm on pipe %d\n",
4839                               pipe);
4840                 return;
4841         }
4842         I915_WRITE(WM3S_LP_IVB, sprite_wm);
4843 }
4844
4845 /**
4846  * intel_update_watermarks - update FIFO watermark values based on current modes
4847  *
4848  * Calculate watermark values for the various WM regs based on current mode
4849  * and plane configuration.
4850  *
4851  * There are several cases to deal with here:
4852  *   - normal (i.e. non-self-refresh)
4853  *   - self-refresh (SR) mode
4854  *   - lines are large relative to FIFO size (buffer can hold up to 2)
4855  *   - lines are small relative to FIFO size (buffer can hold more than 2
4856  *     lines), so need to account for TLB latency
4857  *
4858  *   The normal calculation is:
4859  *     watermark = dotclock * bytes per pixel * latency
4860  *   where latency is platform & configuration dependent (we assume pessimal
4861  *   values here).
4862  *
4863  *   The SR calculation is:
4864  *     watermark = (trunc(latency/line time)+1) * surface width *
4865  *       bytes per pixel
4866  *   where
4867  *     line time = htotal / dotclock
4868  *     surface width = hdisplay for normal plane and 64 for cursor
4869  *   and latency is assumed to be high, as above.
4870  *
4871  * The final value programmed to the register should always be rounded up,
4872  * and include an extra 2 entries to account for clock crossings.
4873  *
4874  * We don't use the sprite, so we can ignore that.  And on Crestline we have
4875  * to set the non-SR watermarks to 8.
4876  */
4877 static void intel_update_watermarks(struct drm_device *dev)
4878 {
4879         struct drm_i915_private *dev_priv = dev->dev_private;
4880
4881         if (dev_priv->display.update_wm)
4882                 dev_priv->display.update_wm(dev);
4883 }
4884
4885 void intel_update_sprite_watermarks(struct drm_device *dev, int pipe,
4886                                     uint32_t sprite_width, int pixel_size)
4887 {
4888         struct drm_i915_private *dev_priv = dev->dev_private;
4889
4890         if (dev_priv->display.update_sprite_wm)
4891                 dev_priv->display.update_sprite_wm(dev, pipe, sprite_width,
4892                                                    pixel_size);
4893 }
4894
4895 static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
4896 {
4897         if (i915_panel_use_ssc >= 0)
4898                 return i915_panel_use_ssc != 0;
4899         return dev_priv->lvds_use_ssc
4900                 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
4901 }
4902
4903 /**
4904  * intel_choose_pipe_bpp_dither - figure out what color depth the pipe should send
4905  * @crtc: CRTC structure
4906  * @mode: requested mode
4907  *
4908  * A pipe may be connected to one or more outputs.  Based on the depth of the
4909  * attached framebuffer, choose a good color depth to use on the pipe.
4910  *
4911  * If possible, match the pipe depth to the fb depth.  In some cases, this
4912  * isn't ideal, because the connected output supports a lesser or restricted
4913  * set of depths.  Resolve that here:
4914  *    LVDS typically supports only 6bpc, so clamp down in that case
4915  *    HDMI supports only 8bpc or 12bpc, so clamp to 8bpc with dither for 10bpc
4916  *    Displays may support a restricted set as well, check EDID and clamp as
4917  *      appropriate.
4918  *    DP may want to dither down to 6bpc to fit larger modes
4919  *
4920  * RETURNS:
4921  * Dithering requirement (i.e. false if display bpc and pipe bpc match,
4922  * true if they don't match).
4923  */
4924 static bool intel_choose_pipe_bpp_dither(struct drm_crtc *crtc,
4925                                          unsigned int *pipe_bpp,
4926                                          struct drm_display_mode *mode)
4927 {
4928         struct drm_device *dev = crtc->dev;
4929         struct drm_i915_private *dev_priv = dev->dev_private;
4930         struct drm_encoder *encoder;
4931         struct drm_connector *connector;
4932         unsigned int display_bpc = UINT_MAX, bpc;
4933
4934         /* Walk the encoders & connectors on this crtc, get min bpc */
4935         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
4936                 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
4937
4938                 if (encoder->crtc != crtc)
4939                         continue;
4940
4941                 if (intel_encoder->type == INTEL_OUTPUT_LVDS) {
4942                         unsigned int lvds_bpc;
4943
4944                         if ((I915_READ(PCH_LVDS) & LVDS_A3_POWER_MASK) ==
4945                             LVDS_A3_POWER_UP)
4946                                 lvds_bpc = 8;
4947                         else
4948                                 lvds_bpc = 6;
4949
4950                         if (lvds_bpc < display_bpc) {
4951                                 DRM_DEBUG_KMS("clamping display bpc (was %d) to LVDS (%d)\n", display_bpc, lvds_bpc);
4952                                 display_bpc = lvds_bpc;
4953                         }
4954                         continue;
4955                 }
4956
4957                 /* Not one of the known troublemakers, check the EDID */
4958                 list_for_each_entry(connector, &dev->mode_config.connector_list,
4959                                     head) {
4960                         if (connector->encoder != encoder)
4961                                 continue;
4962
4963                         /* Don't use an invalid EDID bpc value */
4964                         if (connector->display_info.bpc &&
4965                             connector->display_info.bpc < display_bpc) {
4966                                 DRM_DEBUG_KMS("clamping display bpc (was %d) to EDID reported max of %d\n", display_bpc, connector->display_info.bpc);
4967                                 display_bpc = connector->display_info.bpc;
4968                         }
4969                 }
4970
4971                 if (intel_encoder->type == INTEL_OUTPUT_EDP) {
4972                         /* Use VBT settings if we have an eDP panel */
4973                         unsigned int edp_bpc = dev_priv->edp.bpp / 3;
4974
4975                         if (edp_bpc && edp_bpc < display_bpc) {
4976                                 DRM_DEBUG_KMS("clamping display bpc (was %d) to eDP (%d)\n", display_bpc, edp_bpc);
4977                                 display_bpc = edp_bpc;
4978                         }
4979                         continue;
4980                 }
4981
4982                 /*
4983                  * HDMI is either 12 or 8, so if the display lets 10bpc sneak
4984                  * through, clamp it down.  (Note: >12bpc will be caught below.)
4985                  */
4986                 if (intel_encoder->type == INTEL_OUTPUT_HDMI) {
4987                         if (display_bpc > 8 && display_bpc < 12) {
4988                                 DRM_DEBUG_KMS("forcing bpc to 12 for HDMI\n");
4989                                 display_bpc = 12;
4990                         } else {
4991                                 DRM_DEBUG_KMS("forcing bpc to 8 for HDMI\n");
4992                                 display_bpc = 8;
4993                         }
4994                 }
4995         }
4996
4997         if (mode->private_flags & INTEL_MODE_DP_FORCE_6BPC) {
4998                 DRM_DEBUG_KMS("Dithering DP to 6bpc\n");
4999                 display_bpc = 6;
5000         }
5001
5002         /*
5003          * We could just drive the pipe at the highest bpc all the time and
5004          * enable dithering as needed, but that costs bandwidth.  So choose
5005          * the minimum value that expresses the full color range of the fb but
5006          * also stays within the max display bpc discovered above.
5007          */
5008
5009         switch (crtc->fb->depth) {
5010         case 8:
5011                 bpc = 8; /* since we go through a colormap */
5012                 break;
5013         case 15:
5014         case 16:
5015                 bpc = 6; /* min is 18bpp */
5016                 break;
5017         case 24:
5018                 bpc = 8;
5019                 break;
5020         case 30:
5021                 bpc = 10;
5022                 break;
5023         case 48:
5024                 bpc = 12;
5025                 break;
5026         default:
5027                 DRM_DEBUG("unsupported depth, assuming 24 bits\n");
5028                 bpc = min((unsigned int)8, display_bpc);
5029                 break;
5030         }
5031
5032         display_bpc = min(display_bpc, bpc);
5033
5034         DRM_DEBUG_KMS("setting pipe bpc to %d (max display bpc %d)\n",
5035                       bpc, display_bpc);
5036
5037         *pipe_bpp = display_bpc * 3;
5038
5039         return display_bpc != bpc;
5040 }
5041
5042 static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
5043 {
5044         struct drm_device *dev = crtc->dev;
5045         struct drm_i915_private *dev_priv = dev->dev_private;
5046         int refclk;
5047
5048         if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
5049             intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
5050                 refclk = dev_priv->lvds_ssc_freq * 1000;
5051                 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
5052                               refclk / 1000);
5053         } else if (!IS_GEN2(dev)) {
5054                 refclk = 96000;
5055         } else {
5056                 refclk = 48000;
5057         }
5058
5059         return refclk;
5060 }
5061
5062 static void i9xx_adjust_sdvo_tv_clock(struct drm_display_mode *adjusted_mode,
5063                                       intel_clock_t *clock)
5064 {
5065         /* SDVO TV has fixed PLL values depend on its clock range,
5066            this mirrors vbios setting. */
5067         if (adjusted_mode->clock >= 100000
5068             && adjusted_mode->clock < 140500) {
5069                 clock->p1 = 2;
5070                 clock->p2 = 10;
5071                 clock->n = 3;
5072                 clock->m1 = 16;
5073                 clock->m2 = 8;
5074         } else if (adjusted_mode->clock >= 140500
5075                    && adjusted_mode->clock <= 200000) {
5076                 clock->p1 = 1;
5077                 clock->p2 = 10;
5078                 clock->n = 6;
5079                 clock->m1 = 12;
5080                 clock->m2 = 8;
5081         }
5082 }
5083
5084 static void i9xx_update_pll_dividers(struct drm_crtc *crtc,
5085                                      intel_clock_t *clock,
5086                                      intel_clock_t *reduced_clock)
5087 {
5088         struct drm_device *dev = crtc->dev;
5089         struct drm_i915_private *dev_priv = dev->dev_private;
5090         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5091         int pipe = intel_crtc->pipe;
5092         u32 fp, fp2 = 0;
5093
5094         if (IS_PINEVIEW(dev)) {
5095                 fp = (1 << clock->n) << 16 | clock->m1 << 8 | clock->m2;
5096                 if (reduced_clock)
5097                         fp2 = (1 << reduced_clock->n) << 16 |
5098                                 reduced_clock->m1 << 8 | reduced_clock->m2;
5099         } else {
5100                 fp = clock->n << 16 | clock->m1 << 8 | clock->m2;
5101                 if (reduced_clock)
5102                         fp2 = reduced_clock->n << 16 | reduced_clock->m1 << 8 |
5103                                 reduced_clock->m2;
5104         }
5105
5106         I915_WRITE(FP0(pipe), fp);
5107
5108         intel_crtc->lowfreq_avail = false;
5109         if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
5110             reduced_clock && i915_powersave) {
5111                 I915_WRITE(FP1(pipe), fp2);
5112                 intel_crtc->lowfreq_avail = true;
5113         } else {
5114                 I915_WRITE(FP1(pipe), fp);
5115         }
5116 }
5117
5118 static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
5119                               struct drm_display_mode *mode,
5120                               struct drm_display_mode *adjusted_mode,
5121                               int x, int y,
5122                               struct drm_framebuffer *old_fb)
5123 {
5124         struct drm_device *dev = crtc->dev;
5125         struct drm_i915_private *dev_priv = dev->dev_private;
5126         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5127         int pipe = intel_crtc->pipe;
5128         int plane = intel_crtc->plane;
5129         int refclk, num_connectors = 0;
5130         intel_clock_t clock, reduced_clock;
5131         u32 dpll, dspcntr, pipeconf, vsyncshift;
5132         bool ok, has_reduced_clock = false, is_sdvo = false, is_dvo = false;
5133         bool is_crt = false, is_lvds = false, is_tv = false, is_dp = false;
5134         struct drm_mode_config *mode_config = &dev->mode_config;
5135         struct intel_encoder *encoder;
5136         const intel_limit_t *limit;
5137         int ret;
5138         u32 temp;
5139         u32 lvds_sync = 0;
5140
5141         list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
5142                 if (encoder->base.crtc != crtc)
5143                         continue;
5144
5145                 switch (encoder->type) {
5146                 case INTEL_OUTPUT_LVDS:
5147                         is_lvds = true;
5148                         break;
5149                 case INTEL_OUTPUT_SDVO:
5150                 case INTEL_OUTPUT_HDMI:
5151                         is_sdvo = true;
5152                         if (encoder->needs_tv_clock)
5153                                 is_tv = true;
5154                         break;
5155                 case INTEL_OUTPUT_DVO:
5156                         is_dvo = true;
5157                         break;
5158                 case INTEL_OUTPUT_TVOUT:
5159                         is_tv = true;
5160                         break;
5161                 case INTEL_OUTPUT_ANALOG:
5162                         is_crt = true;
5163                         break;
5164                 case INTEL_OUTPUT_DISPLAYPORT:
5165                         is_dp = true;
5166                         break;
5167                 }
5168
5169                 num_connectors++;
5170         }
5171
5172         refclk = i9xx_get_refclk(crtc, num_connectors);
5173
5174         /*
5175          * Returns a set of divisors for the desired target clock with the given
5176          * refclk, or FALSE.  The returned values represent the clock equation:
5177          * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
5178          */
5179         limit = intel_limit(crtc, refclk);
5180         ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, NULL,
5181                              &clock);
5182         if (!ok) {
5183                 DRM_ERROR("Couldn't find PLL settings for mode!\n");
5184                 return -EINVAL;
5185         }
5186
5187         /* Ensure that the cursor is valid for the new mode before changing... */
5188         intel_crtc_update_cursor(crtc, true);
5189
5190         if (is_lvds && dev_priv->lvds_downclock_avail) {
5191                 /*
5192                  * Ensure we match the reduced clock's P to the target clock.
5193                  * If the clocks don't match, we can't switch the display clock
5194                  * by using the FP0/FP1. In such case we will disable the LVDS
5195                  * downclock feature.
5196                 */
5197                 has_reduced_clock = limit->find_pll(limit, crtc,
5198                                                     dev_priv->lvds_downclock,
5199                                                     refclk,
5200                                                     &clock,
5201                                                     &reduced_clock);
5202         }
5203
5204         if (is_sdvo && is_tv)
5205                 i9xx_adjust_sdvo_tv_clock(adjusted_mode, &clock);
5206
5207         i9xx_update_pll_dividers(crtc, &clock, has_reduced_clock ?
5208                                  &reduced_clock : NULL);
5209
5210         dpll = DPLL_VGA_MODE_DIS;
5211
5212         if (!IS_GEN2(dev)) {
5213                 if (is_lvds)
5214                         dpll |= DPLLB_MODE_LVDS;
5215                 else
5216                         dpll |= DPLLB_MODE_DAC_SERIAL;
5217                 if (is_sdvo) {
5218                         int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
5219                         if (pixel_multiplier > 1) {
5220                                 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
5221                                         dpll |= (pixel_multiplier - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
5222                         }
5223                         dpll |= DPLL_DVO_HIGH_SPEED;
5224                 }
5225                 if (is_dp)
5226                         dpll |= DPLL_DVO_HIGH_SPEED;
5227
5228                 /* compute bitmask from p1 value */
5229                 if (IS_PINEVIEW(dev))
5230                         dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
5231                 else {
5232                         dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5233                         if (IS_G4X(dev) && has_reduced_clock)
5234                                 dpll |= (1 << (reduced_clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
5235                 }
5236                 switch (clock.p2) {
5237                 case 5:
5238                         dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
5239                         break;
5240                 case 7:
5241                         dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
5242                         break;
5243                 case 10:
5244                         dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
5245                         break;
5246                 case 14:
5247                         dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
5248                         break;
5249                 }
5250                 if (INTEL_INFO(dev)->gen >= 4)
5251                         dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
5252         } else {
5253                 if (is_lvds) {
5254                         dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5255                 } else {
5256                         if (clock.p1 == 2)
5257                                 dpll |= PLL_P1_DIVIDE_BY_TWO;
5258                         else
5259                                 dpll |= (clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5260                         if (clock.p2 == 4)
5261                                 dpll |= PLL_P2_DIVIDE_BY_4;
5262                 }
5263         }
5264
5265         if (is_sdvo && is_tv)
5266                 dpll |= PLL_REF_INPUT_TVCLKINBC;
5267         else if (is_tv)
5268                 /* XXX: just matching BIOS for now */
5269                 /*      dpll |= PLL_REF_INPUT_TVCLKINBC; */
5270                 dpll |= 3;
5271         else if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5272                 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5273         else
5274                 dpll |= PLL_REF_INPUT_DREFCLK;
5275
5276         /* setup pipeconf */
5277         pipeconf = I915_READ(PIPECONF(pipe));
5278
5279         /* Set up the display plane register */
5280         dspcntr = DISPPLANE_GAMMA_ENABLE;
5281
5282         if (pipe == 0)
5283                 dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
5284         else
5285                 dspcntr |= DISPPLANE_SEL_PIPE_B;
5286
5287         if (pipe == 0 && INTEL_INFO(dev)->gen < 4) {
5288                 /* Enable pixel doubling when the dot clock is > 90% of the (display)
5289                  * core speed.
5290                  *
5291                  * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
5292                  * pipe == 0 check?
5293                  */
5294                 if (mode->clock >
5295                     dev_priv->display.get_display_clock_speed(dev) * 9 / 10)
5296                         pipeconf |= PIPECONF_DOUBLE_WIDE;
5297                 else
5298                         pipeconf &= ~PIPECONF_DOUBLE_WIDE;
5299         }
5300
5301         /* default to 8bpc */
5302         pipeconf &= ~(PIPECONF_BPP_MASK | PIPECONF_DITHER_EN);
5303         if (is_dp) {
5304                 if (adjusted_mode->private_flags & INTEL_MODE_DP_FORCE_6BPC) {
5305                         pipeconf |= PIPECONF_BPP_6 |
5306                                     PIPECONF_DITHER_EN |
5307                                     PIPECONF_DITHER_TYPE_SP;
5308                 }
5309         }
5310
5311         dpll |= DPLL_VCO_ENABLE;
5312
5313         DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
5314         drm_mode_debug_printmodeline(mode);
5315
5316         I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
5317
5318         POSTING_READ(DPLL(pipe));
5319         udelay(150);
5320
5321         /* The LVDS pin pair needs to be on before the DPLLs are enabled.
5322          * This is an exception to the general rule that mode_set doesn't turn
5323          * things on.
5324          */
5325         if (is_lvds) {
5326                 temp = I915_READ(LVDS);
5327                 temp |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
5328                 if (pipe == 1) {
5329                         temp |= LVDS_PIPEB_SELECT;
5330                 } else {
5331                         temp &= ~LVDS_PIPEB_SELECT;
5332                 }
5333                 /* set the corresponsding LVDS_BORDER bit */
5334                 temp |= dev_priv->lvds_border_bits;
5335                 /* Set the B0-B3 data pairs corresponding to whether we're going to
5336                  * set the DPLLs for dual-channel mode or not.
5337                  */
5338                 if (clock.p2 == 7)
5339                         temp |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
5340                 else
5341                         temp &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
5342
5343                 /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
5344                  * appropriately here, but we need to look more thoroughly into how
5345                  * panels behave in the two modes.
5346                  */
5347                 /* set the dithering flag on LVDS as needed */
5348                 if (INTEL_INFO(dev)->gen >= 4) {
5349                         if (dev_priv->lvds_dither)
5350                                 temp |= LVDS_ENABLE_DITHER;
5351                         else
5352                                 temp &= ~LVDS_ENABLE_DITHER;
5353                 }
5354                 if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
5355                         lvds_sync |= LVDS_HSYNC_POLARITY;
5356                 if (adjusted_mode->flags & DRM_MODE_FLAG_NVSYNC)
5357                         lvds_sync |= LVDS_VSYNC_POLARITY;
5358                 if ((temp & (LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY))
5359                     != lvds_sync) {
5360                         char flags[2] = "-+";
5361                         DRM_INFO("Changing LVDS panel from "
5362                                  "(%chsync, %cvsync) to (%chsync, %cvsync)\n",
5363                                  flags[!(temp & LVDS_HSYNC_POLARITY)],
5364                                  flags[!(temp & LVDS_VSYNC_POLARITY)],
5365                                  flags[!(lvds_sync & LVDS_HSYNC_POLARITY)],
5366                                  flags[!(lvds_sync & LVDS_VSYNC_POLARITY)]);
5367                         temp &= ~(LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY);
5368                         temp |= lvds_sync;
5369                 }
5370                 I915_WRITE(LVDS, temp);
5371         }
5372
5373         if (is_dp) {
5374                 intel_dp_set_m_n(crtc, mode, adjusted_mode);
5375         }
5376
5377         I915_WRITE(DPLL(pipe), dpll);
5378
5379         /* Wait for the clocks to stabilize. */
5380         POSTING_READ(DPLL(pipe));
5381         udelay(150);
5382
5383         if (INTEL_INFO(dev)->gen >= 4) {
5384                 temp = 0;
5385                 if (is_sdvo) {
5386                         temp = intel_mode_get_pixel_multiplier(adjusted_mode);
5387                         if (temp > 1)
5388                                 temp = (temp - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
5389                         else
5390                                 temp = 0;
5391                 }
5392                 I915_WRITE(DPLL_MD(pipe), temp);
5393         } else {
5394                 /* The pixel multiplier can only be updated once the
5395                  * DPLL is enabled and the clocks are stable.
5396                  *
5397                  * So write it again.
5398                  */
5399                 I915_WRITE(DPLL(pipe), dpll);
5400         }
5401
5402         if (HAS_PIPE_CXSR(dev)) {
5403                 if (intel_crtc->lowfreq_avail) {
5404                         DRM_DEBUG_KMS("enabling CxSR downclocking\n");
5405                         pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
5406                 } else {
5407                         DRM_DEBUG_KMS("disabling CxSR downclocking\n");
5408                         pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
5409                 }
5410         }
5411
5412         pipeconf &= ~PIPECONF_INTERLACE_MASK;
5413         if (!IS_GEN2(dev) &&
5414             adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
5415                 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
5416                 /* the chip adds 2 halflines automatically */
5417                 adjusted_mode->crtc_vtotal -= 1;
5418                 adjusted_mode->crtc_vblank_end -= 1;
5419                 vsyncshift = adjusted_mode->crtc_hsync_start
5420                              - adjusted_mode->crtc_htotal/2;
5421         } else {
5422                 pipeconf |= PIPECONF_PROGRESSIVE;
5423                 vsyncshift = 0;
5424         }
5425
5426         if (!IS_GEN3(dev))
5427                 I915_WRITE(VSYNCSHIFT(pipe), vsyncshift);
5428
5429         I915_WRITE(HTOTAL(pipe),
5430                    (adjusted_mode->crtc_hdisplay - 1) |
5431                    ((adjusted_mode->crtc_htotal - 1) << 16));
5432         I915_WRITE(HBLANK(pipe),
5433                    (adjusted_mode->crtc_hblank_start - 1) |
5434                    ((adjusted_mode->crtc_hblank_end - 1) << 16));
5435         I915_WRITE(HSYNC(pipe),
5436                    (adjusted_mode->crtc_hsync_start - 1) |
5437                    ((adjusted_mode->crtc_hsync_end - 1) << 16));
5438
5439         I915_WRITE(VTOTAL(pipe),
5440                    (adjusted_mode->crtc_vdisplay - 1) |
5441                    ((adjusted_mode->crtc_vtotal - 1) << 16));
5442         I915_WRITE(VBLANK(pipe),
5443                    (adjusted_mode->crtc_vblank_start - 1) |
5444                    ((adjusted_mode->crtc_vblank_end - 1) << 16));
5445         I915_WRITE(VSYNC(pipe),
5446                    (adjusted_mode->crtc_vsync_start - 1) |
5447                    ((adjusted_mode->crtc_vsync_end - 1) << 16));
5448
5449         /* pipesrc and dspsize control the size that is scaled from,
5450          * which should always be the user's requested size.
5451          */
5452         I915_WRITE(DSPSIZE(plane),
5453                    ((mode->vdisplay - 1) << 16) |
5454                    (mode->hdisplay - 1));
5455         I915_WRITE(DSPPOS(plane), 0);
5456         I915_WRITE(PIPESRC(pipe),
5457                    ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
5458
5459         I915_WRITE(PIPECONF(pipe), pipeconf);
5460         POSTING_READ(PIPECONF(pipe));
5461         intel_enable_pipe(dev_priv, pipe, false);
5462
5463         intel_wait_for_vblank(dev, pipe);
5464
5465         I915_WRITE(DSPCNTR(plane), dspcntr);
5466         POSTING_READ(DSPCNTR(plane));
5467         intel_enable_plane(dev_priv, plane, pipe);
5468
5469         ret = intel_pipe_set_base(crtc, x, y, old_fb);
5470
5471         intel_update_watermarks(dev);
5472
5473         return ret;
5474 }
5475
5476 /*
5477  * Initialize reference clocks when the driver loads
5478  */
5479 void ironlake_init_pch_refclk(struct drm_device *dev)
5480 {
5481         struct drm_i915_private *dev_priv = dev->dev_private;
5482         struct drm_mode_config *mode_config = &dev->mode_config;
5483         struct intel_encoder *encoder;
5484         u32 temp;
5485         bool has_lvds = false;
5486         bool has_cpu_edp = false;
5487         bool has_pch_edp = false;
5488         bool has_panel = false;
5489         bool has_ck505 = false;
5490         bool can_ssc = false;
5491
5492         /* We need to take the global config into account */
5493         list_for_each_entry(encoder, &mode_config->encoder_list,
5494                             base.head) {
5495                 switch (encoder->type) {
5496                 case INTEL_OUTPUT_LVDS:
5497                         has_panel = true;
5498                         has_lvds = true;
5499                         break;
5500                 case INTEL_OUTPUT_EDP:
5501                         has_panel = true;
5502                         if (intel_encoder_is_pch_edp(&encoder->base))
5503                                 has_pch_edp = true;
5504                         else
5505                                 has_cpu_edp = true;
5506                         break;
5507                 }
5508         }
5509
5510         if (HAS_PCH_IBX(dev)) {
5511                 has_ck505 = dev_priv->display_clock_mode;
5512                 can_ssc = has_ck505;
5513         } else {
5514                 has_ck505 = false;
5515                 can_ssc = true;
5516         }
5517
5518         DRM_DEBUG_KMS("has_panel %d has_lvds %d has_pch_edp %d has_cpu_edp %d has_ck505 %d\n",
5519                       has_panel, has_lvds, has_pch_edp, has_cpu_edp,
5520                       has_ck505);
5521
5522         /* Ironlake: try to setup display ref clock before DPLL
5523          * enabling. This is only under driver's control after
5524          * PCH B stepping, previous chipset stepping should be
5525          * ignoring this setting.
5526          */
5527         temp = I915_READ(PCH_DREF_CONTROL);
5528         /* Always enable nonspread source */
5529         temp &= ~DREF_NONSPREAD_SOURCE_MASK;
5530
5531         if (has_ck505)
5532                 temp |= DREF_NONSPREAD_CK505_ENABLE;
5533         else
5534                 temp |= DREF_NONSPREAD_SOURCE_ENABLE;
5535
5536         if (has_panel) {
5537                 temp &= ~DREF_SSC_SOURCE_MASK;
5538                 temp |= DREF_SSC_SOURCE_ENABLE;
5539
5540                 /* SSC must be turned on before enabling the CPU output  */
5541                 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
5542                         DRM_DEBUG_KMS("Using SSC on panel\n");
5543                         temp |= DREF_SSC1_ENABLE;
5544                 } else
5545                         temp &= ~DREF_SSC1_ENABLE;
5546
5547                 /* Get SSC going before enabling the outputs */
5548                 I915_WRITE(PCH_DREF_CONTROL, temp);
5549                 POSTING_READ(PCH_DREF_CONTROL);
5550                 udelay(200);
5551
5552                 temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
5553
5554                 /* Enable CPU source on CPU attached eDP */
5555                 if (has_cpu_edp) {
5556                         if (intel_panel_use_ssc(dev_priv) && can_ssc) {
5557                                 DRM_DEBUG_KMS("Using SSC on eDP\n");
5558                                 temp |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
5559                         }
5560                         else
5561                                 temp |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
5562                 } else
5563                         temp |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5564
5565                 I915_WRITE(PCH_DREF_CONTROL, temp);
5566                 POSTING_READ(PCH_DREF_CONTROL);
5567                 udelay(200);
5568         } else {
5569                 DRM_DEBUG_KMS("Disabling SSC entirely\n");
5570
5571                 temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
5572
5573                 /* Turn off CPU output */
5574                 temp |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5575
5576                 I915_WRITE(PCH_DREF_CONTROL, temp);
5577                 POSTING_READ(PCH_DREF_CONTROL);
5578                 udelay(200);
5579
5580                 /* Turn off the SSC source */
5581                 temp &= ~DREF_SSC_SOURCE_MASK;
5582                 temp |= DREF_SSC_SOURCE_DISABLE;
5583
5584                 /* Turn off SSC1 */
5585                 temp &= ~ DREF_SSC1_ENABLE;
5586
5587                 I915_WRITE(PCH_DREF_CONTROL, temp);
5588                 POSTING_READ(PCH_DREF_CONTROL);
5589                 udelay(200);
5590         }
5591 }
5592
5593 static int ironlake_get_refclk(struct drm_crtc *crtc)
5594 {
5595         struct drm_device *dev = crtc->dev;
5596         struct drm_i915_private *dev_priv = dev->dev_private;
5597         struct intel_encoder *encoder;
5598         struct drm_mode_config *mode_config = &dev->mode_config;
5599         struct intel_encoder *edp_encoder = NULL;
5600         int num_connectors = 0;
5601         bool is_lvds = false;
5602
5603         list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
5604                 if (encoder->base.crtc != crtc)
5605                         continue;
5606
5607                 switch (encoder->type) {
5608                 case INTEL_OUTPUT_LVDS:
5609                         is_lvds = true;
5610                         break;
5611                 case INTEL_OUTPUT_EDP:
5612                         edp_encoder = encoder;
5613                         break;
5614                 }
5615                 num_connectors++;
5616         }
5617
5618         if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
5619                 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
5620                               dev_priv->lvds_ssc_freq);
5621                 return dev_priv->lvds_ssc_freq * 1000;
5622         }
5623
5624         return 120000;
5625 }
5626
5627 static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
5628                                   struct drm_display_mode *mode,
5629                                   struct drm_display_mode *adjusted_mode,
5630                                   int x, int y,
5631                                   struct drm_framebuffer *old_fb)
5632 {
5633         struct drm_device *dev = crtc->dev;
5634         struct drm_i915_private *dev_priv = dev->dev_private;
5635         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5636         int pipe = intel_crtc->pipe;
5637         int plane = intel_crtc->plane;
5638         int refclk, num_connectors = 0;
5639         intel_clock_t clock, reduced_clock;
5640         u32 dpll, fp = 0, fp2 = 0, dspcntr, pipeconf;
5641         bool ok, has_reduced_clock = false, is_sdvo = false;
5642         bool is_crt = false, is_lvds = false, is_tv = false, is_dp = false;
5643         struct intel_encoder *has_edp_encoder = NULL;
5644         struct drm_mode_config *mode_config = &dev->mode_config;
5645         struct intel_encoder *encoder;
5646         const intel_limit_t *limit;
5647         int ret;
5648         struct fdi_m_n m_n = {0};
5649         u32 temp;
5650         u32 lvds_sync = 0;
5651         int target_clock, pixel_multiplier, lane, link_bw, factor;
5652         unsigned int pipe_bpp;
5653         bool dither;
5654
5655         list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
5656                 if (encoder->base.crtc != crtc)
5657                         continue;
5658
5659                 switch (encoder->type) {
5660                 case INTEL_OUTPUT_LVDS:
5661                         is_lvds = true;
5662                         break;
5663                 case INTEL_OUTPUT_SDVO:
5664                 case INTEL_OUTPUT_HDMI:
5665                         is_sdvo = true;
5666                         if (encoder->needs_tv_clock)
5667                                 is_tv = true;
5668                         break;
5669                 case INTEL_OUTPUT_TVOUT:
5670                         is_tv = true;
5671                         break;
5672                 case INTEL_OUTPUT_ANALOG:
5673                         is_crt = true;
5674                         break;
5675                 case INTEL_OUTPUT_DISPLAYPORT:
5676                         is_dp = true;
5677                         break;
5678                 case INTEL_OUTPUT_EDP:
5679                         has_edp_encoder = encoder;
5680                         break;
5681                 }
5682
5683                 num_connectors++;
5684         }
5685
5686         refclk = ironlake_get_refclk(crtc);
5687
5688         /*
5689          * Returns a set of divisors for the desired target clock with the given
5690          * refclk, or FALSE.  The returned values represent the clock equation:
5691          * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
5692          */
5693         limit = intel_limit(crtc, refclk);
5694         ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, NULL,
5695                              &clock);
5696         if (!ok) {
5697                 DRM_ERROR("Couldn't find PLL settings for mode!\n");
5698                 return -EINVAL;
5699         }
5700
5701         /* Ensure that the cursor is valid for the new mode before changing... */
5702         intel_crtc_update_cursor(crtc, true);
5703
5704         if (is_lvds && dev_priv->lvds_downclock_avail) {
5705                 /*
5706                  * Ensure we match the reduced clock's P to the target clock.
5707                  * If the clocks don't match, we can't switch the display clock
5708                  * by using the FP0/FP1. In such case we will disable the LVDS
5709                  * downclock feature.
5710                 */
5711                 has_reduced_clock = limit->find_pll(limit, crtc,
5712                                                     dev_priv->lvds_downclock,
5713                                                     refclk,
5714                                                     &clock,
5715                                                     &reduced_clock);
5716         }
5717         /* SDVO TV has fixed PLL values depend on its clock range,
5718            this mirrors vbios setting. */
5719         if (is_sdvo && is_tv) {
5720                 if (adjusted_mode->clock >= 100000
5721                     && adjusted_mode->clock < 140500) {
5722                         clock.p1 = 2;
5723                         clock.p2 = 10;
5724                         clock.n = 3;
5725                         clock.m1 = 16;
5726                         clock.m2 = 8;
5727                 } else if (adjusted_mode->clock >= 140500
5728                            && adjusted_mode->clock <= 200000) {
5729                         clock.p1 = 1;
5730                         clock.p2 = 10;
5731                         clock.n = 6;
5732                         clock.m1 = 12;
5733                         clock.m2 = 8;
5734                 }
5735         }
5736
5737         /* FDI link */
5738         pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
5739         lane = 0;
5740         /* CPU eDP doesn't require FDI link, so just set DP M/N
5741            according to current link config */
5742         if (has_edp_encoder &&
5743             !intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
5744                 target_clock = mode->clock;
5745                 intel_edp_link_config(has_edp_encoder,
5746                                       &lane, &link_bw);
5747         } else {
5748                 /* [e]DP over FDI requires target mode clock
5749                    instead of link clock */
5750                 if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base))
5751                         target_clock = mode->clock;
5752                 else
5753                         target_clock = adjusted_mode->clock;
5754
5755                 /* FDI is a binary signal running at ~2.7GHz, encoding
5756                  * each output octet as 10 bits. The actual frequency
5757                  * is stored as a divider into a 100MHz clock, and the
5758                  * mode pixel clock is stored in units of 1KHz.
5759                  * Hence the bw of each lane in terms of the mode signal
5760                  * is:
5761                  */
5762                 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
5763         }
5764
5765         /* determine panel color depth */
5766         temp = I915_READ(PIPECONF(pipe));
5767         temp &= ~PIPE_BPC_MASK;
5768         dither = intel_choose_pipe_bpp_dither(crtc, &pipe_bpp, adjusted_mode);
5769         switch (pipe_bpp) {
5770         case 18:
5771                 temp |= PIPE_6BPC;
5772                 break;
5773         case 24:
5774                 temp |= PIPE_8BPC;
5775                 break;
5776         case 30:
5777                 temp |= PIPE_10BPC;
5778                 break;
5779         case 36:
5780                 temp |= PIPE_12BPC;
5781                 break;
5782         default:
5783                 WARN(1, "intel_choose_pipe_bpp returned invalid value %d\n",
5784                         pipe_bpp);
5785                 temp |= PIPE_8BPC;
5786                 pipe_bpp = 24;
5787                 break;
5788         }
5789
5790         intel_crtc->bpp = pipe_bpp;
5791         I915_WRITE(PIPECONF(pipe), temp);
5792
5793         if (!lane) {
5794                 /*
5795                  * Account for spread spectrum to avoid
5796                  * oversubscribing the link. Max center spread
5797                  * is 2.5%; use 5% for safety's sake.
5798                  */
5799                 u32 bps = target_clock * intel_crtc->bpp * 21 / 20;
5800                 lane = bps / (link_bw * 8) + 1;
5801         }
5802
5803         intel_crtc->fdi_lanes = lane;
5804
5805         if (pixel_multiplier > 1)
5806                 link_bw *= pixel_multiplier;
5807         ironlake_compute_m_n(intel_crtc->bpp, lane, target_clock, link_bw,
5808                              &m_n);
5809
5810         fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
5811         if (has_reduced_clock)
5812                 fp2 = reduced_clock.n << 16 | reduced_clock.m1 << 8 |
5813                         reduced_clock.m2;
5814
5815         /* Enable autotuning of the PLL clock (if permissible) */
5816         factor = 21;
5817         if (is_lvds) {
5818                 if ((intel_panel_use_ssc(dev_priv) &&
5819                      dev_priv->lvds_ssc_freq == 100) ||
5820                     (I915_READ(PCH_LVDS) & LVDS_CLKB_POWER_MASK) == LVDS_CLKB_POWER_UP)
5821                         factor = 25;
5822         } else if (is_sdvo && is_tv)
5823                 factor = 20;
5824
5825         if (clock.m < factor * clock.n)
5826                 fp |= FP_CB_TUNE;
5827
5828         dpll = 0;
5829
5830         if (is_lvds)
5831                 dpll |= DPLLB_MODE_LVDS;
5832         else
5833                 dpll |= DPLLB_MODE_DAC_SERIAL;
5834         if (is_sdvo) {
5835                 int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
5836                 if (pixel_multiplier > 1) {
5837                         dpll |= (pixel_multiplier - 1) << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
5838                 }
5839                 dpll |= DPLL_DVO_HIGH_SPEED;
5840         }
5841         if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base))
5842                 dpll |= DPLL_DVO_HIGH_SPEED;
5843
5844         /* compute bitmask from p1 value */
5845         dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5846         /* also FPA1 */
5847         dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
5848
5849         switch (clock.p2) {
5850         case 5:
5851                 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
5852                 break;
5853         case 7:
5854                 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
5855                 break;
5856         case 10:
5857                 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
5858                 break;
5859         case 14:
5860                 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
5861                 break;
5862         }
5863
5864         if (is_sdvo && is_tv)
5865                 dpll |= PLL_REF_INPUT_TVCLKINBC;
5866         else if (is_tv)
5867                 /* XXX: just matching BIOS for now */
5868                 /*      dpll |= PLL_REF_INPUT_TVCLKINBC; */
5869                 dpll |= 3;
5870         else if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5871                 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5872         else
5873                 dpll |= PLL_REF_INPUT_DREFCLK;
5874
5875         /* setup pipeconf */
5876         pipeconf = I915_READ(PIPECONF(pipe));
5877
5878         /* Set up the display plane register */
5879         dspcntr = DISPPLANE_GAMMA_ENABLE;
5880
5881         DRM_DEBUG_KMS("Mode for pipe %d:\n", pipe);
5882         drm_mode_debug_printmodeline(mode);
5883
5884         /* PCH eDP needs FDI, but CPU eDP does not */
5885         if (!intel_crtc->no_pll) {
5886                 if (!has_edp_encoder ||
5887                     intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
5888                         I915_WRITE(PCH_FP0(pipe), fp);
5889                         I915_WRITE(PCH_DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
5890
5891                         POSTING_READ(PCH_DPLL(pipe));
5892                         udelay(150);
5893                 }
5894         } else {
5895                 if (dpll == (I915_READ(PCH_DPLL(0)) & 0x7fffffff) &&
5896                     fp == I915_READ(PCH_FP0(0))) {
5897                         intel_crtc->use_pll_a = true;
5898                         DRM_DEBUG_KMS("using pipe a dpll\n");
5899                 } else if (dpll == (I915_READ(PCH_DPLL(1)) & 0x7fffffff) &&
5900                            fp == I915_READ(PCH_FP0(1))) {
5901                         intel_crtc->use_pll_a = false;
5902                         DRM_DEBUG_KMS("using pipe b dpll\n");
5903                 } else {
5904                         DRM_DEBUG_KMS("no matching PLL configuration for pipe 2\n");
5905                         return -EINVAL;
5906                 }
5907         }
5908
5909         /* The LVDS pin pair needs to be on before the DPLLs are enabled.
5910          * This is an exception to the general rule that mode_set doesn't turn
5911          * things on.
5912          */
5913         if (is_lvds) {
5914                 temp = I915_READ(PCH_LVDS);
5915                 temp |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
5916                 if (HAS_PCH_CPT(dev)) {
5917                         temp &= ~PORT_TRANS_SEL_MASK;
5918                         temp |= PORT_TRANS_SEL_CPT(pipe);
5919                 } else {
5920                         if (pipe == 1)
5921                                 temp |= LVDS_PIPEB_SELECT;
5922                         else
5923                                 temp &= ~LVDS_PIPEB_SELECT;
5924                 }
5925
5926                 /* set the corresponsding LVDS_BORDER bit */
5927                 temp |= dev_priv->lvds_border_bits;
5928                 /* Set the B0-B3 data pairs corresponding to whether we're going to
5929                  * set the DPLLs for dual-channel mode or not.
5930                  */
5931                 if (clock.p2 == 7)
5932                         temp |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
5933                 else
5934                         temp &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
5935
5936                 /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
5937                  * appropriately here, but we need to look more thoroughly into how
5938                  * panels behave in the two modes.
5939                  */
5940                 if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
5941                         lvds_sync |= LVDS_HSYNC_POLARITY;
5942                 if (adjusted_mode->flags & DRM_MODE_FLAG_NVSYNC)
5943                         lvds_sync |= LVDS_VSYNC_POLARITY;
5944                 if ((temp & (LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY))
5945                     != lvds_sync) {
5946                         char flags[2] = "-+";
5947                         DRM_INFO("Changing LVDS panel from "
5948                                  "(%chsync, %cvsync) to (%chsync, %cvsync)\n",
5949                                  flags[!(temp & LVDS_HSYNC_POLARITY)],
5950                                  flags[!(temp & LVDS_VSYNC_POLARITY)],
5951                                  flags[!(lvds_sync & LVDS_HSYNC_POLARITY)],
5952                                  flags[!(lvds_sync & LVDS_VSYNC_POLARITY)]);
5953                         temp &= ~(LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY);
5954                         temp |= lvds_sync;
5955                 }
5956                 I915_WRITE(PCH_LVDS, temp);
5957         }
5958
5959         pipeconf &= ~PIPECONF_DITHER_EN;
5960         pipeconf &= ~PIPECONF_DITHER_TYPE_MASK;
5961         if ((is_lvds && dev_priv->lvds_dither) || dither) {
5962                 pipeconf |= PIPECONF_DITHER_EN;
5963                 pipeconf |= PIPECONF_DITHER_TYPE_SP;
5964         }
5965         if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
5966                 intel_dp_set_m_n(crtc, mode, adjusted_mode);
5967         } else {
5968                 /* For non-DP output, clear any trans DP clock recovery setting.*/
5969                 I915_WRITE(TRANSDATA_M1(pipe), 0);
5970                 I915_WRITE(TRANSDATA_N1(pipe), 0);
5971                 I915_WRITE(TRANSDPLINK_M1(pipe), 0);
5972                 I915_WRITE(TRANSDPLINK_N1(pipe), 0);
5973         }
5974
5975         if (!intel_crtc->no_pll &&
5976             (!has_edp_encoder ||
5977              intel_encoder_is_pch_edp(&has_edp_encoder->base))) {
5978                 I915_WRITE(PCH_DPLL(pipe), dpll);
5979
5980                 /* Wait for the clocks to stabilize. */
5981                 POSTING_READ(PCH_DPLL(pipe));
5982                 udelay(150);
5983
5984                 /* The pixel multiplier can only be updated once the
5985                  * DPLL is enabled and the clocks are stable.
5986                  *
5987                  * So write it again.
5988                  */
5989   &nb