drm/i915: Select the correct BPC for LVDS on Ironlake
[safe/jmp/linux-2.6] / 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/module.h>
28 #include <linux/input.h>
29 #include <linux/i2c.h>
30 #include <linux/kernel.h>
31 #include "drmP.h"
32 #include "intel_drv.h"
33 #include "i915_drm.h"
34 #include "i915_drv.h"
35 #include "drm_dp_helper.h"
36
37 #include "drm_crtc_helper.h"
38
39 #define HAS_eDP (intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
40
41 bool intel_pipe_has_type (struct drm_crtc *crtc, int type);
42 static void intel_update_watermarks(struct drm_device *dev);
43 static void intel_increase_pllclock(struct drm_crtc *crtc, bool schedule);
44
45 typedef struct {
46     /* given values */
47     int n;
48     int m1, m2;
49     int p1, p2;
50     /* derived values */
51     int dot;
52     int vco;
53     int m;
54     int p;
55 } intel_clock_t;
56
57 typedef struct {
58     int min, max;
59 } intel_range_t;
60
61 typedef struct {
62     int dot_limit;
63     int p2_slow, p2_fast;
64 } intel_p2_t;
65
66 #define INTEL_P2_NUM                  2
67 typedef struct intel_limit intel_limit_t;
68 struct intel_limit {
69     intel_range_t   dot, vco, n, m, m1, m2, p, p1;
70     intel_p2_t      p2;
71     bool (* find_pll)(const intel_limit_t *, struct drm_crtc *,
72                       int, int, intel_clock_t *);
73     bool (* find_reduced_pll)(const intel_limit_t *, struct drm_crtc *,
74                               int, int, intel_clock_t *);
75 };
76
77 #define I8XX_DOT_MIN              25000
78 #define I8XX_DOT_MAX             350000
79 #define I8XX_VCO_MIN             930000
80 #define I8XX_VCO_MAX            1400000
81 #define I8XX_N_MIN                    3
82 #define I8XX_N_MAX                   16
83 #define I8XX_M_MIN                   96
84 #define I8XX_M_MAX                  140
85 #define I8XX_M1_MIN                  18
86 #define I8XX_M1_MAX                  26
87 #define I8XX_M2_MIN                   6
88 #define I8XX_M2_MAX                  16
89 #define I8XX_P_MIN                    4
90 #define I8XX_P_MAX                  128
91 #define I8XX_P1_MIN                   2
92 #define I8XX_P1_MAX                  33
93 #define I8XX_P1_LVDS_MIN              1
94 #define I8XX_P1_LVDS_MAX              6
95 #define I8XX_P2_SLOW                  4
96 #define I8XX_P2_FAST                  2
97 #define I8XX_P2_LVDS_SLOW             14
98 #define I8XX_P2_LVDS_FAST             7
99 #define I8XX_P2_SLOW_LIMIT       165000
100
101 #define I9XX_DOT_MIN              20000
102 #define I9XX_DOT_MAX             400000
103 #define I9XX_VCO_MIN            1400000
104 #define I9XX_VCO_MAX            2800000
105 #define PINEVIEW_VCO_MIN                1700000
106 #define PINEVIEW_VCO_MAX                3500000
107 #define I9XX_N_MIN                    1
108 #define I9XX_N_MAX                    6
109 /* Pineview's Ncounter is a ring counter */
110 #define PINEVIEW_N_MIN                3
111 #define PINEVIEW_N_MAX                6
112 #define I9XX_M_MIN                   70
113 #define I9XX_M_MAX                  120
114 #define PINEVIEW_M_MIN                2
115 #define PINEVIEW_M_MAX              256
116 #define I9XX_M1_MIN                  10
117 #define I9XX_M1_MAX                  22
118 #define I9XX_M2_MIN                   5
119 #define I9XX_M2_MAX                   9
120 /* Pineview M1 is reserved, and must be 0 */
121 #define PINEVIEW_M1_MIN               0
122 #define PINEVIEW_M1_MAX               0
123 #define PINEVIEW_M2_MIN               0
124 #define PINEVIEW_M2_MAX               254
125 #define I9XX_P_SDVO_DAC_MIN           5
126 #define I9XX_P_SDVO_DAC_MAX          80
127 #define I9XX_P_LVDS_MIN               7
128 #define I9XX_P_LVDS_MAX              98
129 #define PINEVIEW_P_LVDS_MIN                   7
130 #define PINEVIEW_P_LVDS_MAX                  112
131 #define I9XX_P1_MIN                   1
132 #define I9XX_P1_MAX                   8
133 #define I9XX_P2_SDVO_DAC_SLOW                10
134 #define I9XX_P2_SDVO_DAC_FAST                 5
135 #define I9XX_P2_SDVO_DAC_SLOW_LIMIT      200000
136 #define I9XX_P2_LVDS_SLOW                    14
137 #define I9XX_P2_LVDS_FAST                     7
138 #define I9XX_P2_LVDS_SLOW_LIMIT          112000
139
140 /*The parameter is for SDVO on G4x platform*/
141 #define G4X_DOT_SDVO_MIN           25000
142 #define G4X_DOT_SDVO_MAX           270000
143 #define G4X_VCO_MIN                1750000
144 #define G4X_VCO_MAX                3500000
145 #define G4X_N_SDVO_MIN             1
146 #define G4X_N_SDVO_MAX             4
147 #define G4X_M_SDVO_MIN             104
148 #define G4X_M_SDVO_MAX             138
149 #define G4X_M1_SDVO_MIN            17
150 #define G4X_M1_SDVO_MAX            23
151 #define G4X_M2_SDVO_MIN            5
152 #define G4X_M2_SDVO_MAX            11
153 #define G4X_P_SDVO_MIN             10
154 #define G4X_P_SDVO_MAX             30
155 #define G4X_P1_SDVO_MIN            1
156 #define G4X_P1_SDVO_MAX            3
157 #define G4X_P2_SDVO_SLOW           10
158 #define G4X_P2_SDVO_FAST           10
159 #define G4X_P2_SDVO_LIMIT          270000
160
161 /*The parameter is for HDMI_DAC on G4x platform*/
162 #define G4X_DOT_HDMI_DAC_MIN           22000
163 #define G4X_DOT_HDMI_DAC_MAX           400000
164 #define G4X_N_HDMI_DAC_MIN             1
165 #define G4X_N_HDMI_DAC_MAX             4
166 #define G4X_M_HDMI_DAC_MIN             104
167 #define G4X_M_HDMI_DAC_MAX             138
168 #define G4X_M1_HDMI_DAC_MIN            16
169 #define G4X_M1_HDMI_DAC_MAX            23
170 #define G4X_M2_HDMI_DAC_MIN            5
171 #define G4X_M2_HDMI_DAC_MAX            11
172 #define G4X_P_HDMI_DAC_MIN             5
173 #define G4X_P_HDMI_DAC_MAX             80
174 #define G4X_P1_HDMI_DAC_MIN            1
175 #define G4X_P1_HDMI_DAC_MAX            8
176 #define G4X_P2_HDMI_DAC_SLOW           10
177 #define G4X_P2_HDMI_DAC_FAST           5
178 #define G4X_P2_HDMI_DAC_LIMIT          165000
179
180 /*The parameter is for SINGLE_CHANNEL_LVDS on G4x platform*/
181 #define G4X_DOT_SINGLE_CHANNEL_LVDS_MIN           20000
182 #define G4X_DOT_SINGLE_CHANNEL_LVDS_MAX           115000
183 #define G4X_N_SINGLE_CHANNEL_LVDS_MIN             1
184 #define G4X_N_SINGLE_CHANNEL_LVDS_MAX             3
185 #define G4X_M_SINGLE_CHANNEL_LVDS_MIN             104
186 #define G4X_M_SINGLE_CHANNEL_LVDS_MAX             138
187 #define G4X_M1_SINGLE_CHANNEL_LVDS_MIN            17
188 #define G4X_M1_SINGLE_CHANNEL_LVDS_MAX            23
189 #define G4X_M2_SINGLE_CHANNEL_LVDS_MIN            5
190 #define G4X_M2_SINGLE_CHANNEL_LVDS_MAX            11
191 #define G4X_P_SINGLE_CHANNEL_LVDS_MIN             28
192 #define G4X_P_SINGLE_CHANNEL_LVDS_MAX             112
193 #define G4X_P1_SINGLE_CHANNEL_LVDS_MIN            2
194 #define G4X_P1_SINGLE_CHANNEL_LVDS_MAX            8
195 #define G4X_P2_SINGLE_CHANNEL_LVDS_SLOW           14
196 #define G4X_P2_SINGLE_CHANNEL_LVDS_FAST           14
197 #define G4X_P2_SINGLE_CHANNEL_LVDS_LIMIT          0
198
199 /*The parameter is for DUAL_CHANNEL_LVDS on G4x platform*/
200 #define G4X_DOT_DUAL_CHANNEL_LVDS_MIN           80000
201 #define G4X_DOT_DUAL_CHANNEL_LVDS_MAX           224000
202 #define G4X_N_DUAL_CHANNEL_LVDS_MIN             1
203 #define G4X_N_DUAL_CHANNEL_LVDS_MAX             3
204 #define G4X_M_DUAL_CHANNEL_LVDS_MIN             104
205 #define G4X_M_DUAL_CHANNEL_LVDS_MAX             138
206 #define G4X_M1_DUAL_CHANNEL_LVDS_MIN            17
207 #define G4X_M1_DUAL_CHANNEL_LVDS_MAX            23
208 #define G4X_M2_DUAL_CHANNEL_LVDS_MIN            5
209 #define G4X_M2_DUAL_CHANNEL_LVDS_MAX            11
210 #define G4X_P_DUAL_CHANNEL_LVDS_MIN             14
211 #define G4X_P_DUAL_CHANNEL_LVDS_MAX             42
212 #define G4X_P1_DUAL_CHANNEL_LVDS_MIN            2
213 #define G4X_P1_DUAL_CHANNEL_LVDS_MAX            6
214 #define G4X_P2_DUAL_CHANNEL_LVDS_SLOW           7
215 #define G4X_P2_DUAL_CHANNEL_LVDS_FAST           7
216 #define G4X_P2_DUAL_CHANNEL_LVDS_LIMIT          0
217
218 /*The parameter is for DISPLAY PORT on G4x platform*/
219 #define G4X_DOT_DISPLAY_PORT_MIN           161670
220 #define G4X_DOT_DISPLAY_PORT_MAX           227000
221 #define G4X_N_DISPLAY_PORT_MIN             1
222 #define G4X_N_DISPLAY_PORT_MAX             2
223 #define G4X_M_DISPLAY_PORT_MIN             97
224 #define G4X_M_DISPLAY_PORT_MAX             108
225 #define G4X_M1_DISPLAY_PORT_MIN            0x10
226 #define G4X_M1_DISPLAY_PORT_MAX            0x12
227 #define G4X_M2_DISPLAY_PORT_MIN            0x05
228 #define G4X_M2_DISPLAY_PORT_MAX            0x06
229 #define G4X_P_DISPLAY_PORT_MIN             10
230 #define G4X_P_DISPLAY_PORT_MAX             20
231 #define G4X_P1_DISPLAY_PORT_MIN            1
232 #define G4X_P1_DISPLAY_PORT_MAX            2
233 #define G4X_P2_DISPLAY_PORT_SLOW           10
234 #define G4X_P2_DISPLAY_PORT_FAST           10
235 #define G4X_P2_DISPLAY_PORT_LIMIT          0
236
237 /* Ironlake */
238 /* as we calculate clock using (register_value + 2) for
239    N/M1/M2, so here the range value for them is (actual_value-2).
240  */
241 #define IRONLAKE_DOT_MIN         25000
242 #define IRONLAKE_DOT_MAX         350000
243 #define IRONLAKE_VCO_MIN         1760000
244 #define IRONLAKE_VCO_MAX         3510000
245 #define IRONLAKE_N_MIN           1
246 #define IRONLAKE_N_MAX           5
247 #define IRONLAKE_M_MIN           79
248 #define IRONLAKE_M_MAX           118
249 #define IRONLAKE_M1_MIN          12
250 #define IRONLAKE_M1_MAX          23
251 #define IRONLAKE_M2_MIN          5
252 #define IRONLAKE_M2_MAX          9
253 #define IRONLAKE_P_SDVO_DAC_MIN  5
254 #define IRONLAKE_P_SDVO_DAC_MAX  80
255 #define IRONLAKE_P_LVDS_MIN      28
256 #define IRONLAKE_P_LVDS_MAX      112
257 #define IRONLAKE_P1_MIN          1
258 #define IRONLAKE_P1_MAX          8
259 #define IRONLAKE_P2_SDVO_DAC_SLOW 10
260 #define IRONLAKE_P2_SDVO_DAC_FAST 5
261 #define IRONLAKE_P2_LVDS_SLOW    14 /* single channel */
262 #define IRONLAKE_P2_LVDS_FAST    7  /* double channel */
263 #define IRONLAKE_P2_DOT_LIMIT    225000 /* 225Mhz */
264
265 static bool
266 intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
267                     int target, int refclk, intel_clock_t *best_clock);
268 static bool
269 intel_find_best_reduced_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
270                             int target, int refclk, intel_clock_t *best_clock);
271 static bool
272 intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
273                         int target, int refclk, intel_clock_t *best_clock);
274 static bool
275 intel_ironlake_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
276                              int target, int refclk, intel_clock_t *best_clock);
277
278 static bool
279 intel_find_pll_g4x_dp(const intel_limit_t *, struct drm_crtc *crtc,
280                       int target, int refclk, intel_clock_t *best_clock);
281 static bool
282 intel_find_pll_ironlake_dp(const intel_limit_t *, struct drm_crtc *crtc,
283                            int target, int refclk, intel_clock_t *best_clock);
284
285 static const intel_limit_t intel_limits_i8xx_dvo = {
286         .dot = { .min = I8XX_DOT_MIN,           .max = I8XX_DOT_MAX },
287         .vco = { .min = I8XX_VCO_MIN,           .max = I8XX_VCO_MAX },
288         .n   = { .min = I8XX_N_MIN,             .max = I8XX_N_MAX },
289         .m   = { .min = I8XX_M_MIN,             .max = I8XX_M_MAX },
290         .m1  = { .min = I8XX_M1_MIN,            .max = I8XX_M1_MAX },
291         .m2  = { .min = I8XX_M2_MIN,            .max = I8XX_M2_MAX },
292         .p   = { .min = I8XX_P_MIN,             .max = I8XX_P_MAX },
293         .p1  = { .min = I8XX_P1_MIN,            .max = I8XX_P1_MAX },
294         .p2  = { .dot_limit = I8XX_P2_SLOW_LIMIT,
295                  .p2_slow = I8XX_P2_SLOW,       .p2_fast = I8XX_P2_FAST },
296         .find_pll = intel_find_best_PLL,
297         .find_reduced_pll = intel_find_best_reduced_PLL,
298 };
299
300 static const intel_limit_t intel_limits_i8xx_lvds = {
301         .dot = { .min = I8XX_DOT_MIN,           .max = I8XX_DOT_MAX },
302         .vco = { .min = I8XX_VCO_MIN,           .max = I8XX_VCO_MAX },
303         .n   = { .min = I8XX_N_MIN,             .max = I8XX_N_MAX },
304         .m   = { .min = I8XX_M_MIN,             .max = I8XX_M_MAX },
305         .m1  = { .min = I8XX_M1_MIN,            .max = I8XX_M1_MAX },
306         .m2  = { .min = I8XX_M2_MIN,            .max = I8XX_M2_MAX },
307         .p   = { .min = I8XX_P_MIN,             .max = I8XX_P_MAX },
308         .p1  = { .min = I8XX_P1_LVDS_MIN,       .max = I8XX_P1_LVDS_MAX },
309         .p2  = { .dot_limit = I8XX_P2_SLOW_LIMIT,
310                  .p2_slow = I8XX_P2_LVDS_SLOW,  .p2_fast = I8XX_P2_LVDS_FAST },
311         .find_pll = intel_find_best_PLL,
312         .find_reduced_pll = intel_find_best_reduced_PLL,
313 };
314         
315 static const intel_limit_t intel_limits_i9xx_sdvo = {
316         .dot = { .min = I9XX_DOT_MIN,           .max = I9XX_DOT_MAX },
317         .vco = { .min = I9XX_VCO_MIN,           .max = I9XX_VCO_MAX },
318         .n   = { .min = I9XX_N_MIN,             .max = I9XX_N_MAX },
319         .m   = { .min = I9XX_M_MIN,             .max = I9XX_M_MAX },
320         .m1  = { .min = I9XX_M1_MIN,            .max = I9XX_M1_MAX },
321         .m2  = { .min = I9XX_M2_MIN,            .max = I9XX_M2_MAX },
322         .p   = { .min = I9XX_P_SDVO_DAC_MIN,    .max = I9XX_P_SDVO_DAC_MAX },
323         .p1  = { .min = I9XX_P1_MIN,            .max = I9XX_P1_MAX },
324         .p2  = { .dot_limit = I9XX_P2_SDVO_DAC_SLOW_LIMIT,
325                  .p2_slow = I9XX_P2_SDVO_DAC_SLOW,      .p2_fast = I9XX_P2_SDVO_DAC_FAST },
326         .find_pll = intel_find_best_PLL,
327         .find_reduced_pll = intel_find_best_reduced_PLL,
328 };
329
330 static const intel_limit_t intel_limits_i9xx_lvds = {
331         .dot = { .min = I9XX_DOT_MIN,           .max = I9XX_DOT_MAX },
332         .vco = { .min = I9XX_VCO_MIN,           .max = I9XX_VCO_MAX },
333         .n   = { .min = I9XX_N_MIN,             .max = I9XX_N_MAX },
334         .m   = { .min = I9XX_M_MIN,             .max = I9XX_M_MAX },
335         .m1  = { .min = I9XX_M1_MIN,            .max = I9XX_M1_MAX },
336         .m2  = { .min = I9XX_M2_MIN,            .max = I9XX_M2_MAX },
337         .p   = { .min = I9XX_P_LVDS_MIN,        .max = I9XX_P_LVDS_MAX },
338         .p1  = { .min = I9XX_P1_MIN,            .max = I9XX_P1_MAX },
339         /* The single-channel range is 25-112Mhz, and dual-channel
340          * is 80-224Mhz.  Prefer single channel as much as possible.
341          */
342         .p2  = { .dot_limit = I9XX_P2_LVDS_SLOW_LIMIT,
343                  .p2_slow = I9XX_P2_LVDS_SLOW,  .p2_fast = I9XX_P2_LVDS_FAST },
344         .find_pll = intel_find_best_PLL,
345         .find_reduced_pll = intel_find_best_reduced_PLL,
346 };
347
348     /* below parameter and function is for G4X Chipset Family*/
349 static const intel_limit_t intel_limits_g4x_sdvo = {
350         .dot = { .min = G4X_DOT_SDVO_MIN,       .max = G4X_DOT_SDVO_MAX },
351         .vco = { .min = G4X_VCO_MIN,            .max = G4X_VCO_MAX},
352         .n   = { .min = G4X_N_SDVO_MIN,         .max = G4X_N_SDVO_MAX },
353         .m   = { .min = G4X_M_SDVO_MIN,         .max = G4X_M_SDVO_MAX },
354         .m1  = { .min = G4X_M1_SDVO_MIN,        .max = G4X_M1_SDVO_MAX },
355         .m2  = { .min = G4X_M2_SDVO_MIN,        .max = G4X_M2_SDVO_MAX },
356         .p   = { .min = G4X_P_SDVO_MIN,         .max = G4X_P_SDVO_MAX },
357         .p1  = { .min = G4X_P1_SDVO_MIN,        .max = G4X_P1_SDVO_MAX},
358         .p2  = { .dot_limit = G4X_P2_SDVO_LIMIT,
359                  .p2_slow = G4X_P2_SDVO_SLOW,
360                  .p2_fast = G4X_P2_SDVO_FAST
361         },
362         .find_pll = intel_g4x_find_best_PLL,
363         .find_reduced_pll = intel_g4x_find_best_PLL,
364 };
365
366 static const intel_limit_t intel_limits_g4x_hdmi = {
367         .dot = { .min = G4X_DOT_HDMI_DAC_MIN,   .max = G4X_DOT_HDMI_DAC_MAX },
368         .vco = { .min = G4X_VCO_MIN,            .max = G4X_VCO_MAX},
369         .n   = { .min = G4X_N_HDMI_DAC_MIN,     .max = G4X_N_HDMI_DAC_MAX },
370         .m   = { .min = G4X_M_HDMI_DAC_MIN,     .max = G4X_M_HDMI_DAC_MAX },
371         .m1  = { .min = G4X_M1_HDMI_DAC_MIN,    .max = G4X_M1_HDMI_DAC_MAX },
372         .m2  = { .min = G4X_M2_HDMI_DAC_MIN,    .max = G4X_M2_HDMI_DAC_MAX },
373         .p   = { .min = G4X_P_HDMI_DAC_MIN,     .max = G4X_P_HDMI_DAC_MAX },
374         .p1  = { .min = G4X_P1_HDMI_DAC_MIN,    .max = G4X_P1_HDMI_DAC_MAX},
375         .p2  = { .dot_limit = G4X_P2_HDMI_DAC_LIMIT,
376                  .p2_slow = G4X_P2_HDMI_DAC_SLOW,
377                  .p2_fast = G4X_P2_HDMI_DAC_FAST
378         },
379         .find_pll = intel_g4x_find_best_PLL,
380         .find_reduced_pll = intel_g4x_find_best_PLL,
381 };
382
383 static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
384         .dot = { .min = G4X_DOT_SINGLE_CHANNEL_LVDS_MIN,
385                  .max = G4X_DOT_SINGLE_CHANNEL_LVDS_MAX },
386         .vco = { .min = G4X_VCO_MIN,
387                  .max = G4X_VCO_MAX },
388         .n   = { .min = G4X_N_SINGLE_CHANNEL_LVDS_MIN,
389                  .max = G4X_N_SINGLE_CHANNEL_LVDS_MAX },
390         .m   = { .min = G4X_M_SINGLE_CHANNEL_LVDS_MIN,
391                  .max = G4X_M_SINGLE_CHANNEL_LVDS_MAX },
392         .m1  = { .min = G4X_M1_SINGLE_CHANNEL_LVDS_MIN,
393                  .max = G4X_M1_SINGLE_CHANNEL_LVDS_MAX },
394         .m2  = { .min = G4X_M2_SINGLE_CHANNEL_LVDS_MIN,
395                  .max = G4X_M2_SINGLE_CHANNEL_LVDS_MAX },
396         .p   = { .min = G4X_P_SINGLE_CHANNEL_LVDS_MIN,
397                  .max = G4X_P_SINGLE_CHANNEL_LVDS_MAX },
398         .p1  = { .min = G4X_P1_SINGLE_CHANNEL_LVDS_MIN,
399                  .max = G4X_P1_SINGLE_CHANNEL_LVDS_MAX },
400         .p2  = { .dot_limit = G4X_P2_SINGLE_CHANNEL_LVDS_LIMIT,
401                  .p2_slow = G4X_P2_SINGLE_CHANNEL_LVDS_SLOW,
402                  .p2_fast = G4X_P2_SINGLE_CHANNEL_LVDS_FAST
403         },
404         .find_pll = intel_g4x_find_best_PLL,
405         .find_reduced_pll = intel_g4x_find_best_PLL,
406 };
407
408 static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
409         .dot = { .min = G4X_DOT_DUAL_CHANNEL_LVDS_MIN,
410                  .max = G4X_DOT_DUAL_CHANNEL_LVDS_MAX },
411         .vco = { .min = G4X_VCO_MIN,
412                  .max = G4X_VCO_MAX },
413         .n   = { .min = G4X_N_DUAL_CHANNEL_LVDS_MIN,
414                  .max = G4X_N_DUAL_CHANNEL_LVDS_MAX },
415         .m   = { .min = G4X_M_DUAL_CHANNEL_LVDS_MIN,
416                  .max = G4X_M_DUAL_CHANNEL_LVDS_MAX },
417         .m1  = { .min = G4X_M1_DUAL_CHANNEL_LVDS_MIN,
418                  .max = G4X_M1_DUAL_CHANNEL_LVDS_MAX },
419         .m2  = { .min = G4X_M2_DUAL_CHANNEL_LVDS_MIN,
420                  .max = G4X_M2_DUAL_CHANNEL_LVDS_MAX },
421         .p   = { .min = G4X_P_DUAL_CHANNEL_LVDS_MIN,
422                  .max = G4X_P_DUAL_CHANNEL_LVDS_MAX },
423         .p1  = { .min = G4X_P1_DUAL_CHANNEL_LVDS_MIN,
424                  .max = G4X_P1_DUAL_CHANNEL_LVDS_MAX },
425         .p2  = { .dot_limit = G4X_P2_DUAL_CHANNEL_LVDS_LIMIT,
426                  .p2_slow = G4X_P2_DUAL_CHANNEL_LVDS_SLOW,
427                  .p2_fast = G4X_P2_DUAL_CHANNEL_LVDS_FAST
428         },
429         .find_pll = intel_g4x_find_best_PLL,
430         .find_reduced_pll = intel_g4x_find_best_PLL,
431 };
432
433 static const intel_limit_t intel_limits_g4x_display_port = {
434         .dot = { .min = G4X_DOT_DISPLAY_PORT_MIN,
435                  .max = G4X_DOT_DISPLAY_PORT_MAX },
436         .vco = { .min = G4X_VCO_MIN,
437                  .max = G4X_VCO_MAX},
438         .n   = { .min = G4X_N_DISPLAY_PORT_MIN,
439                  .max = G4X_N_DISPLAY_PORT_MAX },
440         .m   = { .min = G4X_M_DISPLAY_PORT_MIN,
441                  .max = G4X_M_DISPLAY_PORT_MAX },
442         .m1  = { .min = G4X_M1_DISPLAY_PORT_MIN,
443                  .max = G4X_M1_DISPLAY_PORT_MAX },
444         .m2  = { .min = G4X_M2_DISPLAY_PORT_MIN,
445                  .max = G4X_M2_DISPLAY_PORT_MAX },
446         .p   = { .min = G4X_P_DISPLAY_PORT_MIN,
447                  .max = G4X_P_DISPLAY_PORT_MAX },
448         .p1  = { .min = G4X_P1_DISPLAY_PORT_MIN,
449                  .max = G4X_P1_DISPLAY_PORT_MAX},
450         .p2  = { .dot_limit = G4X_P2_DISPLAY_PORT_LIMIT,
451                  .p2_slow = G4X_P2_DISPLAY_PORT_SLOW,
452                  .p2_fast = G4X_P2_DISPLAY_PORT_FAST },
453         .find_pll = intel_find_pll_g4x_dp,
454 };
455
456 static const intel_limit_t intel_limits_pineview_sdvo = {
457         .dot = { .min = I9XX_DOT_MIN,           .max = I9XX_DOT_MAX},
458         .vco = { .min = PINEVIEW_VCO_MIN,               .max = PINEVIEW_VCO_MAX },
459         .n   = { .min = PINEVIEW_N_MIN,         .max = PINEVIEW_N_MAX },
460         .m   = { .min = PINEVIEW_M_MIN,         .max = PINEVIEW_M_MAX },
461         .m1  = { .min = PINEVIEW_M1_MIN,                .max = PINEVIEW_M1_MAX },
462         .m2  = { .min = PINEVIEW_M2_MIN,                .max = PINEVIEW_M2_MAX },
463         .p   = { .min = I9XX_P_SDVO_DAC_MIN,    .max = I9XX_P_SDVO_DAC_MAX },
464         .p1  = { .min = I9XX_P1_MIN,            .max = I9XX_P1_MAX },
465         .p2  = { .dot_limit = I9XX_P2_SDVO_DAC_SLOW_LIMIT,
466                  .p2_slow = I9XX_P2_SDVO_DAC_SLOW,      .p2_fast = I9XX_P2_SDVO_DAC_FAST },
467         .find_pll = intel_find_best_PLL,
468         .find_reduced_pll = intel_find_best_reduced_PLL,
469 };
470
471 static const intel_limit_t intel_limits_pineview_lvds = {
472         .dot = { .min = I9XX_DOT_MIN,           .max = I9XX_DOT_MAX },
473         .vco = { .min = PINEVIEW_VCO_MIN,               .max = PINEVIEW_VCO_MAX },
474         .n   = { .min = PINEVIEW_N_MIN,         .max = PINEVIEW_N_MAX },
475         .m   = { .min = PINEVIEW_M_MIN,         .max = PINEVIEW_M_MAX },
476         .m1  = { .min = PINEVIEW_M1_MIN,                .max = PINEVIEW_M1_MAX },
477         .m2  = { .min = PINEVIEW_M2_MIN,                .max = PINEVIEW_M2_MAX },
478         .p   = { .min = PINEVIEW_P_LVDS_MIN,    .max = PINEVIEW_P_LVDS_MAX },
479         .p1  = { .min = I9XX_P1_MIN,            .max = I9XX_P1_MAX },
480         /* Pineview only supports single-channel mode. */
481         .p2  = { .dot_limit = I9XX_P2_LVDS_SLOW_LIMIT,
482                  .p2_slow = I9XX_P2_LVDS_SLOW,  .p2_fast = I9XX_P2_LVDS_SLOW },
483         .find_pll = intel_find_best_PLL,
484         .find_reduced_pll = intel_find_best_reduced_PLL,
485 };
486
487 static const intel_limit_t intel_limits_ironlake_sdvo = {
488         .dot = { .min = IRONLAKE_DOT_MIN,          .max = IRONLAKE_DOT_MAX },
489         .vco = { .min = IRONLAKE_VCO_MIN,          .max = IRONLAKE_VCO_MAX },
490         .n   = { .min = IRONLAKE_N_MIN,            .max = IRONLAKE_N_MAX },
491         .m   = { .min = IRONLAKE_M_MIN,            .max = IRONLAKE_M_MAX },
492         .m1  = { .min = IRONLAKE_M1_MIN,           .max = IRONLAKE_M1_MAX },
493         .m2  = { .min = IRONLAKE_M2_MIN,           .max = IRONLAKE_M2_MAX },
494         .p   = { .min = IRONLAKE_P_SDVO_DAC_MIN,   .max = IRONLAKE_P_SDVO_DAC_MAX },
495         .p1  = { .min = IRONLAKE_P1_MIN,           .max = IRONLAKE_P1_MAX },
496         .p2  = { .dot_limit = IRONLAKE_P2_DOT_LIMIT,
497                  .p2_slow = IRONLAKE_P2_SDVO_DAC_SLOW,
498                  .p2_fast = IRONLAKE_P2_SDVO_DAC_FAST },
499         .find_pll = intel_ironlake_find_best_PLL,
500 };
501
502 static const intel_limit_t intel_limits_ironlake_lvds = {
503         .dot = { .min = IRONLAKE_DOT_MIN,          .max = IRONLAKE_DOT_MAX },
504         .vco = { .min = IRONLAKE_VCO_MIN,          .max = IRONLAKE_VCO_MAX },
505         .n   = { .min = IRONLAKE_N_MIN,            .max = IRONLAKE_N_MAX },
506         .m   = { .min = IRONLAKE_M_MIN,            .max = IRONLAKE_M_MAX },
507         .m1  = { .min = IRONLAKE_M1_MIN,           .max = IRONLAKE_M1_MAX },
508         .m2  = { .min = IRONLAKE_M2_MIN,           .max = IRONLAKE_M2_MAX },
509         .p   = { .min = IRONLAKE_P_LVDS_MIN,       .max = IRONLAKE_P_LVDS_MAX },
510         .p1  = { .min = IRONLAKE_P1_MIN,           .max = IRONLAKE_P1_MAX },
511         .p2  = { .dot_limit = IRONLAKE_P2_DOT_LIMIT,
512                  .p2_slow = IRONLAKE_P2_LVDS_SLOW,
513                  .p2_fast = IRONLAKE_P2_LVDS_FAST },
514         .find_pll = intel_ironlake_find_best_PLL,
515 };
516
517 static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc)
518 {
519         const intel_limit_t *limit;
520         if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
521                 limit = &intel_limits_ironlake_lvds;
522         else
523                 limit = &intel_limits_ironlake_sdvo;
524
525         return limit;
526 }
527
528 static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
529 {
530         struct drm_device *dev = crtc->dev;
531         struct drm_i915_private *dev_priv = dev->dev_private;
532         const intel_limit_t *limit;
533
534         if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
535                 if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
536                     LVDS_CLKB_POWER_UP)
537                         /* LVDS with dual channel */
538                         limit = &intel_limits_g4x_dual_channel_lvds;
539                 else
540                         /* LVDS with dual channel */
541                         limit = &intel_limits_g4x_single_channel_lvds;
542         } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
543                    intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
544                 limit = &intel_limits_g4x_hdmi;
545         } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
546                 limit = &intel_limits_g4x_sdvo;
547         } else if (intel_pipe_has_type (crtc, INTEL_OUTPUT_DISPLAYPORT)) {
548                 limit = &intel_limits_g4x_display_port;
549         } else /* The option is for other outputs */
550                 limit = &intel_limits_i9xx_sdvo;
551
552         return limit;
553 }
554
555 static const intel_limit_t *intel_limit(struct drm_crtc *crtc)
556 {
557         struct drm_device *dev = crtc->dev;
558         const intel_limit_t *limit;
559
560         if (IS_IRONLAKE(dev))
561                 limit = intel_ironlake_limit(crtc);
562         else if (IS_G4X(dev)) {
563                 limit = intel_g4x_limit(crtc);
564         } else if (IS_I9XX(dev) && !IS_PINEVIEW(dev)) {
565                 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
566                         limit = &intel_limits_i9xx_lvds;
567                 else
568                         limit = &intel_limits_i9xx_sdvo;
569         } else if (IS_PINEVIEW(dev)) {
570                 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
571                         limit = &intel_limits_pineview_lvds;
572                 else
573                         limit = &intel_limits_pineview_sdvo;
574         } else {
575                 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
576                         limit = &intel_limits_i8xx_lvds;
577                 else
578                         limit = &intel_limits_i8xx_dvo;
579         }
580         return limit;
581 }
582
583 /* m1 is reserved as 0 in Pineview, n is a ring counter */
584 static void pineview_clock(int refclk, intel_clock_t *clock)
585 {
586         clock->m = clock->m2 + 2;
587         clock->p = clock->p1 * clock->p2;
588         clock->vco = refclk * clock->m / clock->n;
589         clock->dot = clock->vco / clock->p;
590 }
591
592 static void intel_clock(struct drm_device *dev, int refclk, intel_clock_t *clock)
593 {
594         if (IS_PINEVIEW(dev)) {
595                 pineview_clock(refclk, clock);
596                 return;
597         }
598         clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
599         clock->p = clock->p1 * clock->p2;
600         clock->vco = refclk * clock->m / (clock->n + 2);
601         clock->dot = clock->vco / clock->p;
602 }
603
604 /**
605  * Returns whether any output on the specified pipe is of the specified type
606  */
607 bool intel_pipe_has_type (struct drm_crtc *crtc, int type)
608 {
609     struct drm_device *dev = crtc->dev;
610     struct drm_mode_config *mode_config = &dev->mode_config;
611     struct drm_connector *l_entry;
612
613     list_for_each_entry(l_entry, &mode_config->connector_list, head) {
614             if (l_entry->encoder &&
615                 l_entry->encoder->crtc == crtc) {
616                     struct intel_output *intel_output = to_intel_output(l_entry);
617                     if (intel_output->type == type)
618                             return true;
619             }
620     }
621     return false;
622 }
623
624 struct drm_connector *
625 intel_pipe_get_output (struct drm_crtc *crtc)
626 {
627     struct drm_device *dev = crtc->dev;
628     struct drm_mode_config *mode_config = &dev->mode_config;
629     struct drm_connector *l_entry, *ret = NULL;
630
631     list_for_each_entry(l_entry, &mode_config->connector_list, head) {
632             if (l_entry->encoder &&
633                 l_entry->encoder->crtc == crtc) {
634                     ret = l_entry;
635                     break;
636             }
637     }
638     return ret;
639 }
640
641 #define INTELPllInvalid(s)   do { /* DRM_DEBUG(s); */ return false; } while (0)
642 /**
643  * Returns whether the given set of divisors are valid for a given refclk with
644  * the given connectors.
645  */
646
647 static bool intel_PLL_is_valid(struct drm_crtc *crtc, intel_clock_t *clock)
648 {
649         const intel_limit_t *limit = intel_limit (crtc);
650         struct drm_device *dev = crtc->dev;
651
652         if (clock->p1  < limit->p1.min  || limit->p1.max  < clock->p1)
653                 INTELPllInvalid ("p1 out of range\n");
654         if (clock->p   < limit->p.min   || limit->p.max   < clock->p)
655                 INTELPllInvalid ("p out of range\n");
656         if (clock->m2  < limit->m2.min  || limit->m2.max  < clock->m2)
657                 INTELPllInvalid ("m2 out of range\n");
658         if (clock->m1  < limit->m1.min  || limit->m1.max  < clock->m1)
659                 INTELPllInvalid ("m1 out of range\n");
660         if (clock->m1 <= clock->m2 && !IS_PINEVIEW(dev))
661                 INTELPllInvalid ("m1 <= m2\n");
662         if (clock->m   < limit->m.min   || limit->m.max   < clock->m)
663                 INTELPllInvalid ("m out of range\n");
664         if (clock->n   < limit->n.min   || limit->n.max   < clock->n)
665                 INTELPllInvalid ("n out of range\n");
666         if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
667                 INTELPllInvalid ("vco out of range\n");
668         /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
669          * connector, etc., rather than just a single range.
670          */
671         if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
672                 INTELPllInvalid ("dot out of range\n");
673
674         return true;
675 }
676
677 static bool
678 intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
679                     int target, int refclk, intel_clock_t *best_clock)
680
681 {
682         struct drm_device *dev = crtc->dev;
683         struct drm_i915_private *dev_priv = dev->dev_private;
684         intel_clock_t clock;
685         int err = target;
686
687         if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
688             (I915_READ(LVDS)) != 0) {
689                 /*
690                  * For LVDS, if the panel is on, just rely on its current
691                  * settings for dual-channel.  We haven't figured out how to
692                  * reliably set up different single/dual channel state, if we
693                  * even can.
694                  */
695                 if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
696                     LVDS_CLKB_POWER_UP)
697                         clock.p2 = limit->p2.p2_fast;
698                 else
699                         clock.p2 = limit->p2.p2_slow;
700         } else {
701                 if (target < limit->p2.dot_limit)
702                         clock.p2 = limit->p2.p2_slow;
703                 else
704                         clock.p2 = limit->p2.p2_fast;
705         }
706
707         memset (best_clock, 0, sizeof (*best_clock));
708
709         for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
710              clock.m1++) {
711                 for (clock.m2 = limit->m2.min;
712                      clock.m2 <= limit->m2.max; clock.m2++) {
713                         /* m1 is always 0 in Pineview */
714                         if (clock.m2 >= clock.m1 && !IS_PINEVIEW(dev))
715                                 break;
716                         for (clock.n = limit->n.min;
717                              clock.n <= limit->n.max; clock.n++) {
718                                 for (clock.p1 = limit->p1.min;
719                                         clock.p1 <= limit->p1.max; clock.p1++) {
720                                         int this_err;
721
722                                         intel_clock(dev, refclk, &clock);
723
724                                         if (!intel_PLL_is_valid(crtc, &clock))
725                                                 continue;
726
727                                         this_err = abs(clock.dot - target);
728                                         if (this_err < err) {
729                                                 *best_clock = clock;
730                                                 err = this_err;
731                                         }
732                                 }
733                         }
734                 }
735         }
736
737         return (err != target);
738 }
739
740
741 static bool
742 intel_find_best_reduced_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
743                             int target, int refclk, intel_clock_t *best_clock)
744
745 {
746         struct drm_device *dev = crtc->dev;
747         intel_clock_t clock;
748         int err = target;
749         bool found = false;
750
751         memcpy(&clock, best_clock, sizeof(intel_clock_t));
752
753         for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
754                 for (clock.m2 = limit->m2.min; clock.m2 <= limit->m2.max; clock.m2++) {
755                         /* m1 is always 0 in Pineview */
756                         if (clock.m2 >= clock.m1 && !IS_PINEVIEW(dev))
757                                 break;
758                         for (clock.n = limit->n.min; clock.n <= limit->n.max;
759                              clock.n++) {
760                                 int this_err;
761
762                                 intel_clock(dev, refclk, &clock);
763
764                                 if (!intel_PLL_is_valid(crtc, &clock))
765                                         continue;
766
767                                 this_err = abs(clock.dot - target);
768                                 if (this_err < err) {
769                                         *best_clock = clock;
770                                         err = this_err;
771                                         found = true;
772                                 }
773                         }
774                 }
775         }
776
777         return found;
778 }
779
780 static bool
781 intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
782                         int target, int refclk, intel_clock_t *best_clock)
783 {
784         struct drm_device *dev = crtc->dev;
785         struct drm_i915_private *dev_priv = dev->dev_private;
786         intel_clock_t clock;
787         int max_n;
788         bool found;
789         /* approximately equals target * 0.00488 */
790         int err_most = (target >> 8) + (target >> 10);
791         found = false;
792
793         if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
794                 if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
795                     LVDS_CLKB_POWER_UP)
796                         clock.p2 = limit->p2.p2_fast;
797                 else
798                         clock.p2 = limit->p2.p2_slow;
799         } else {
800                 if (target < limit->p2.dot_limit)
801                         clock.p2 = limit->p2.p2_slow;
802                 else
803                         clock.p2 = limit->p2.p2_fast;
804         }
805
806         memset(best_clock, 0, sizeof(*best_clock));
807         max_n = limit->n.max;
808         /* based on hardware requriment prefer smaller n to precision */
809         for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
810                 /* based on hardware requirment prefere larger m1,m2 */
811                 for (clock.m1 = limit->m1.max;
812                      clock.m1 >= limit->m1.min; clock.m1--) {
813                         for (clock.m2 = limit->m2.max;
814                              clock.m2 >= limit->m2.min; clock.m2--) {
815                                 for (clock.p1 = limit->p1.max;
816                                      clock.p1 >= limit->p1.min; clock.p1--) {
817                                         int this_err;
818
819                                         intel_clock(dev, refclk, &clock);
820                                         if (!intel_PLL_is_valid(crtc, &clock))
821                                                 continue;
822                                         this_err = abs(clock.dot - target) ;
823                                         if (this_err < err_most) {
824                                                 *best_clock = clock;
825                                                 err_most = this_err;
826                                                 max_n = clock.n;
827                                                 found = true;
828                                         }
829                                 }
830                         }
831                 }
832         }
833         return found;
834 }
835
836 static bool
837 intel_find_pll_ironlake_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
838                            int target, int refclk, intel_clock_t *best_clock)
839 {
840         struct drm_device *dev = crtc->dev;
841         intel_clock_t clock;
842         if (target < 200000) {
843                 clock.n = 1;
844                 clock.p1 = 2;
845                 clock.p2 = 10;
846                 clock.m1 = 12;
847                 clock.m2 = 9;
848         } else {
849                 clock.n = 2;
850                 clock.p1 = 1;
851                 clock.p2 = 10;
852                 clock.m1 = 14;
853                 clock.m2 = 8;
854         }
855         intel_clock(dev, refclk, &clock);
856         memcpy(best_clock, &clock, sizeof(intel_clock_t));
857         return true;
858 }
859
860 static bool
861 intel_ironlake_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
862                              int target, int refclk, intel_clock_t *best_clock)
863 {
864         struct drm_device *dev = crtc->dev;
865         struct drm_i915_private *dev_priv = dev->dev_private;
866         intel_clock_t clock;
867         int err_most = 47;
868         int err_min = 10000;
869
870         /* eDP has only 2 clock choice, no n/m/p setting */
871         if (HAS_eDP)
872                 return true;
873
874         if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT))
875                 return intel_find_pll_ironlake_dp(limit, crtc, target,
876                                                refclk, best_clock);
877
878         if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
879                 if ((I915_READ(PCH_LVDS) & LVDS_CLKB_POWER_MASK) ==
880                     LVDS_CLKB_POWER_UP)
881                         clock.p2 = limit->p2.p2_fast;
882                 else
883                         clock.p2 = limit->p2.p2_slow;
884         } else {
885                 if (target < limit->p2.dot_limit)
886                         clock.p2 = limit->p2.p2_slow;
887                 else
888                         clock.p2 = limit->p2.p2_fast;
889         }
890
891         memset(best_clock, 0, sizeof(*best_clock));
892         for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
893                 /* based on hardware requriment prefer smaller n to precision */
894                 for (clock.n = limit->n.min; clock.n <= limit->n.max; clock.n++) {
895                         /* based on hardware requirment prefere larger m1,m2 */
896                         for (clock.m1 = limit->m1.max;
897                              clock.m1 >= limit->m1.min; clock.m1--) {
898                                 for (clock.m2 = limit->m2.max;
899                                      clock.m2 >= limit->m2.min; clock.m2--) {
900                                         int this_err;
901
902                                         intel_clock(dev, refclk, &clock);
903                                         if (!intel_PLL_is_valid(crtc, &clock))
904                                                 continue;
905                                         this_err = abs((10000 - (target*10000/clock.dot)));
906                                         if (this_err < err_most) {
907                                                 *best_clock = clock;
908                                                 /* found on first matching */
909                                                 goto out;
910                                         } else if (this_err < err_min) {
911                                                 *best_clock = clock;
912                                                 err_min = this_err;
913                                         }
914                                 }
915                         }
916                 }
917         }
918 out:
919         return true;
920 }
921
922 /* DisplayPort has only two frequencies, 162MHz and 270MHz */
923 static bool
924 intel_find_pll_g4x_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
925                       int target, int refclk, intel_clock_t *best_clock)
926 {
927     intel_clock_t clock;
928     if (target < 200000) {
929         clock.p1 = 2;
930         clock.p2 = 10;
931         clock.n = 2;
932         clock.m1 = 23;
933         clock.m2 = 8;
934     } else {
935         clock.p1 = 1;
936         clock.p2 = 10;
937         clock.n = 1;
938         clock.m1 = 14;
939         clock.m2 = 2;
940     }
941     clock.m = 5 * (clock.m1 + 2) + (clock.m2 + 2);
942     clock.p = (clock.p1 * clock.p2);
943     clock.dot = 96000 * clock.m / (clock.n + 2) / clock.p;
944     clock.vco = 0;
945     memcpy(best_clock, &clock, sizeof(intel_clock_t));
946     return true;
947 }
948
949 void
950 intel_wait_for_vblank(struct drm_device *dev)
951 {
952         /* Wait for 20ms, i.e. one cycle at 50hz. */
953         msleep(20);
954 }
955
956 /* Parameters have changed, update FBC info */
957 static void i8xx_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
958 {
959         struct drm_device *dev = crtc->dev;
960         struct drm_i915_private *dev_priv = dev->dev_private;
961         struct drm_framebuffer *fb = crtc->fb;
962         struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
963         struct drm_i915_gem_object *obj_priv = intel_fb->obj->driver_private;
964         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
965         int plane, i;
966         u32 fbc_ctl, fbc_ctl2;
967
968         dev_priv->cfb_pitch = dev_priv->cfb_size / FBC_LL_SIZE;
969
970         if (fb->pitch < dev_priv->cfb_pitch)
971                 dev_priv->cfb_pitch = fb->pitch;
972
973         /* FBC_CTL wants 64B units */
974         dev_priv->cfb_pitch = (dev_priv->cfb_pitch / 64) - 1;
975         dev_priv->cfb_fence = obj_priv->fence_reg;
976         dev_priv->cfb_plane = intel_crtc->plane;
977         plane = dev_priv->cfb_plane == 0 ? FBC_CTL_PLANEA : FBC_CTL_PLANEB;
978
979         /* Clear old tags */
980         for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++)
981                 I915_WRITE(FBC_TAG + (i * 4), 0);
982
983         /* Set it up... */
984         fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM | plane;
985         if (obj_priv->tiling_mode != I915_TILING_NONE)
986                 fbc_ctl2 |= FBC_CTL_CPU_FENCE;
987         I915_WRITE(FBC_CONTROL2, fbc_ctl2);
988         I915_WRITE(FBC_FENCE_OFF, crtc->y);
989
990         /* enable it... */
991         fbc_ctl = FBC_CTL_EN | FBC_CTL_PERIODIC;
992         fbc_ctl |= (dev_priv->cfb_pitch & 0xff) << FBC_CTL_STRIDE_SHIFT;
993         fbc_ctl |= (interval & 0x2fff) << FBC_CTL_INTERVAL_SHIFT;
994         if (obj_priv->tiling_mode != I915_TILING_NONE)
995                 fbc_ctl |= dev_priv->cfb_fence;
996         I915_WRITE(FBC_CONTROL, fbc_ctl);
997
998         DRM_DEBUG_KMS("enabled FBC, pitch %ld, yoff %d, plane %d, ",
999                   dev_priv->cfb_pitch, crtc->y, dev_priv->cfb_plane);
1000 }
1001
1002 void i8xx_disable_fbc(struct drm_device *dev)
1003 {
1004         struct drm_i915_private *dev_priv = dev->dev_private;
1005         u32 fbc_ctl;
1006
1007         if (!I915_HAS_FBC(dev))
1008                 return;
1009
1010         /* Disable compression */
1011         fbc_ctl = I915_READ(FBC_CONTROL);
1012         fbc_ctl &= ~FBC_CTL_EN;
1013         I915_WRITE(FBC_CONTROL, fbc_ctl);
1014
1015         /* Wait for compressing bit to clear */
1016         while (I915_READ(FBC_STATUS) & FBC_STAT_COMPRESSING)
1017                 ; /* nothing */
1018
1019         intel_wait_for_vblank(dev);
1020
1021         DRM_DEBUG_KMS("disabled FBC\n");
1022 }
1023
1024 static bool i8xx_fbc_enabled(struct drm_crtc *crtc)
1025 {
1026         struct drm_device *dev = crtc->dev;
1027         struct drm_i915_private *dev_priv = dev->dev_private;
1028
1029         return I915_READ(FBC_CONTROL) & FBC_CTL_EN;
1030 }
1031
1032 static void g4x_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
1033 {
1034         struct drm_device *dev = crtc->dev;
1035         struct drm_i915_private *dev_priv = dev->dev_private;
1036         struct drm_framebuffer *fb = crtc->fb;
1037         struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
1038         struct drm_i915_gem_object *obj_priv = intel_fb->obj->driver_private;
1039         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1040         int plane = (intel_crtc->plane == 0 ? DPFC_CTL_PLANEA :
1041                      DPFC_CTL_PLANEB);
1042         unsigned long stall_watermark = 200;
1043         u32 dpfc_ctl;
1044
1045         dev_priv->cfb_pitch = (dev_priv->cfb_pitch / 64) - 1;
1046         dev_priv->cfb_fence = obj_priv->fence_reg;
1047         dev_priv->cfb_plane = intel_crtc->plane;
1048
1049         dpfc_ctl = plane | DPFC_SR_EN | DPFC_CTL_LIMIT_1X;
1050         if (obj_priv->tiling_mode != I915_TILING_NONE) {
1051                 dpfc_ctl |= DPFC_CTL_FENCE_EN | dev_priv->cfb_fence;
1052                 I915_WRITE(DPFC_CHICKEN, DPFC_HT_MODIFY);
1053         } else {
1054                 I915_WRITE(DPFC_CHICKEN, ~DPFC_HT_MODIFY);
1055         }
1056
1057         I915_WRITE(DPFC_CONTROL, dpfc_ctl);
1058         I915_WRITE(DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
1059                    (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
1060                    (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
1061         I915_WRITE(DPFC_FENCE_YOFF, crtc->y);
1062
1063         /* enable it... */
1064         I915_WRITE(DPFC_CONTROL, I915_READ(DPFC_CONTROL) | DPFC_CTL_EN);
1065
1066         DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
1067 }
1068
1069 void g4x_disable_fbc(struct drm_device *dev)
1070 {
1071         struct drm_i915_private *dev_priv = dev->dev_private;
1072         u32 dpfc_ctl;
1073
1074         /* Disable compression */
1075         dpfc_ctl = I915_READ(DPFC_CONTROL);
1076         dpfc_ctl &= ~DPFC_CTL_EN;
1077         I915_WRITE(DPFC_CONTROL, dpfc_ctl);
1078         intel_wait_for_vblank(dev);
1079
1080         DRM_DEBUG_KMS("disabled FBC\n");
1081 }
1082
1083 static bool g4x_fbc_enabled(struct drm_crtc *crtc)
1084 {
1085         struct drm_device *dev = crtc->dev;
1086         struct drm_i915_private *dev_priv = dev->dev_private;
1087
1088         return I915_READ(DPFC_CONTROL) & DPFC_CTL_EN;
1089 }
1090
1091 /**
1092  * intel_update_fbc - enable/disable FBC as needed
1093  * @crtc: CRTC to point the compressor at
1094  * @mode: mode in use
1095  *
1096  * Set up the framebuffer compression hardware at mode set time.  We
1097  * enable it if possible:
1098  *   - plane A only (on pre-965)
1099  *   - no pixel mulitply/line duplication
1100  *   - no alpha buffer discard
1101  *   - no dual wide
1102  *   - framebuffer <= 2048 in width, 1536 in height
1103  *
1104  * We can't assume that any compression will take place (worst case),
1105  * so the compressed buffer has to be the same size as the uncompressed
1106  * one.  It also must reside (along with the line length buffer) in
1107  * stolen memory.
1108  *
1109  * We need to enable/disable FBC on a global basis.
1110  */
1111 static void intel_update_fbc(struct drm_crtc *crtc,
1112                              struct drm_display_mode *mode)
1113 {
1114         struct drm_device *dev = crtc->dev;
1115         struct drm_i915_private *dev_priv = dev->dev_private;
1116         struct drm_framebuffer *fb = crtc->fb;
1117         struct intel_framebuffer *intel_fb;
1118         struct drm_i915_gem_object *obj_priv;
1119         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1120         int plane = intel_crtc->plane;
1121
1122         if (!i915_powersave)
1123                 return;
1124
1125         if (!dev_priv->display.fbc_enabled ||
1126             !dev_priv->display.enable_fbc ||
1127             !dev_priv->display.disable_fbc)
1128                 return;
1129
1130         if (!crtc->fb)
1131                 return;
1132
1133         intel_fb = to_intel_framebuffer(fb);
1134         obj_priv = intel_fb->obj->driver_private;
1135
1136         /*
1137          * If FBC is already on, we just have to verify that we can
1138          * keep it that way...
1139          * Need to disable if:
1140          *   - changing FBC params (stride, fence, mode)
1141          *   - new fb is too large to fit in compressed buffer
1142          *   - going to an unsupported config (interlace, pixel multiply, etc.)
1143          */
1144         if (intel_fb->obj->size > dev_priv->cfb_size) {
1145                 DRM_DEBUG_KMS("framebuffer too large, disabling "
1146                                 "compression\n");
1147                 goto out_disable;
1148         }
1149         if ((mode->flags & DRM_MODE_FLAG_INTERLACE) ||
1150             (mode->flags & DRM_MODE_FLAG_DBLSCAN)) {
1151                 DRM_DEBUG_KMS("mode incompatible with compression, "
1152                                 "disabling\n");
1153                 goto out_disable;
1154         }
1155         if ((mode->hdisplay > 2048) ||
1156             (mode->vdisplay > 1536)) {
1157                 DRM_DEBUG_KMS("mode too large for compression, disabling\n");
1158                 goto out_disable;
1159         }
1160         if ((IS_I915GM(dev) || IS_I945GM(dev)) && plane != 0) {
1161                 DRM_DEBUG_KMS("plane not 0, disabling compression\n");
1162                 goto out_disable;
1163         }
1164         if (obj_priv->tiling_mode != I915_TILING_X) {
1165                 DRM_DEBUG_KMS("framebuffer not tiled, disabling compression\n");
1166                 goto out_disable;
1167         }
1168
1169         if (dev_priv->display.fbc_enabled(crtc)) {
1170                 /* We can re-enable it in this case, but need to update pitch */
1171                 if (fb->pitch > dev_priv->cfb_pitch)
1172                         dev_priv->display.disable_fbc(dev);
1173                 if (obj_priv->fence_reg != dev_priv->cfb_fence)
1174                         dev_priv->display.disable_fbc(dev);
1175                 if (plane != dev_priv->cfb_plane)
1176                         dev_priv->display.disable_fbc(dev);
1177         }
1178
1179         if (!dev_priv->display.fbc_enabled(crtc)) {
1180                 /* Now try to turn it back on if possible */
1181                 dev_priv->display.enable_fbc(crtc, 500);
1182         }
1183
1184         return;
1185
1186 out_disable:
1187         DRM_DEBUG_KMS("unsupported config, disabling FBC\n");
1188         /* Multiple disables should be harmless */
1189         if (dev_priv->display.fbc_enabled(crtc))
1190                 dev_priv->display.disable_fbc(dev);
1191 }
1192
1193 static int
1194 intel_pin_and_fence_fb_obj(struct drm_device *dev, struct drm_gem_object *obj)
1195 {
1196         struct drm_i915_gem_object *obj_priv = obj->driver_private;
1197         u32 alignment;
1198         int ret;
1199
1200         switch (obj_priv->tiling_mode) {
1201         case I915_TILING_NONE:
1202                 alignment = 64 * 1024;
1203                 break;
1204         case I915_TILING_X:
1205                 /* pin() will align the object as required by fence */
1206                 alignment = 0;
1207                 break;
1208         case I915_TILING_Y:
1209                 /* FIXME: Is this true? */
1210                 DRM_ERROR("Y tiled not allowed for scan out buffers\n");
1211                 return -EINVAL;
1212         default:
1213                 BUG();
1214         }
1215
1216         ret = i915_gem_object_pin(obj, alignment);
1217         if (ret != 0)
1218                 return ret;
1219
1220         /* Install a fence for tiled scan-out. Pre-i965 always needs a
1221          * fence, whereas 965+ only requires a fence if using
1222          * framebuffer compression.  For simplicity, we always install
1223          * a fence as the cost is not that onerous.
1224          */
1225         if (obj_priv->fence_reg == I915_FENCE_REG_NONE &&
1226             obj_priv->tiling_mode != I915_TILING_NONE) {
1227                 ret = i915_gem_object_get_fence_reg(obj);
1228                 if (ret != 0) {
1229                         i915_gem_object_unpin(obj);
1230                         return ret;
1231                 }
1232         }
1233
1234         return 0;
1235 }
1236
1237 static int
1238 intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
1239                     struct drm_framebuffer *old_fb)
1240 {
1241         struct drm_device *dev = crtc->dev;
1242         struct drm_i915_private *dev_priv = dev->dev_private;
1243         struct drm_i915_master_private *master_priv;
1244         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1245         struct intel_framebuffer *intel_fb;
1246         struct drm_i915_gem_object *obj_priv;
1247         struct drm_gem_object *obj;
1248         int pipe = intel_crtc->pipe;
1249         int plane = intel_crtc->plane;
1250         unsigned long Start, Offset;
1251         int dspbase = (plane == 0 ? DSPAADDR : DSPBADDR);
1252         int dspsurf = (plane == 0 ? DSPASURF : DSPBSURF);
1253         int dspstride = (plane == 0) ? DSPASTRIDE : DSPBSTRIDE;
1254         int dsptileoff = (plane == 0 ? DSPATILEOFF : DSPBTILEOFF);
1255         int dspcntr_reg = (plane == 0) ? DSPACNTR : DSPBCNTR;
1256         u32 dspcntr;
1257         int ret;
1258
1259         /* no fb bound */
1260         if (!crtc->fb) {
1261                 DRM_DEBUG_KMS("No FB bound\n");
1262                 return 0;
1263         }
1264
1265         switch (plane) {
1266         case 0:
1267         case 1:
1268                 break;
1269         default:
1270                 DRM_ERROR("Can't update plane %d in SAREA\n", plane);
1271                 return -EINVAL;
1272         }
1273
1274         intel_fb = to_intel_framebuffer(crtc->fb);
1275         obj = intel_fb->obj;
1276         obj_priv = obj->driver_private;
1277
1278         mutex_lock(&dev->struct_mutex);
1279         ret = intel_pin_and_fence_fb_obj(dev, obj);
1280         if (ret != 0) {
1281                 mutex_unlock(&dev->struct_mutex);
1282                 return ret;
1283         }
1284
1285         ret = i915_gem_object_set_to_gtt_domain(obj, 1);
1286         if (ret != 0) {
1287                 i915_gem_object_unpin(obj);
1288                 mutex_unlock(&dev->struct_mutex);
1289                 return ret;
1290         }
1291
1292         dspcntr = I915_READ(dspcntr_reg);
1293         /* Mask out pixel format bits in case we change it */
1294         dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
1295         switch (crtc->fb->bits_per_pixel) {
1296         case 8:
1297                 dspcntr |= DISPPLANE_8BPP;
1298                 break;
1299         case 16:
1300                 if (crtc->fb->depth == 15)
1301                         dspcntr |= DISPPLANE_15_16BPP;
1302                 else
1303                         dspcntr |= DISPPLANE_16BPP;
1304                 break;
1305         case 24:
1306         case 32:
1307                 if (crtc->fb->depth == 30)
1308                         dspcntr |= DISPPLANE_32BPP_30BIT_NO_ALPHA;
1309                 else
1310                         dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
1311                 break;
1312         default:
1313                 DRM_ERROR("Unknown color depth\n");
1314                 i915_gem_object_unpin(obj);
1315                 mutex_unlock(&dev->struct_mutex);
1316                 return -EINVAL;
1317         }
1318         if (IS_I965G(dev)) {
1319                 if (obj_priv->tiling_mode != I915_TILING_NONE)
1320                         dspcntr |= DISPPLANE_TILED;
1321                 else
1322                         dspcntr &= ~DISPPLANE_TILED;
1323         }
1324
1325         if (IS_IRONLAKE(dev))
1326                 /* must disable */
1327                 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
1328
1329         I915_WRITE(dspcntr_reg, dspcntr);
1330
1331         Start = obj_priv->gtt_offset;
1332         Offset = y * crtc->fb->pitch + x * (crtc->fb->bits_per_pixel / 8);
1333
1334         DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d\n", Start, Offset, x, y);
1335         I915_WRITE(dspstride, crtc->fb->pitch);
1336         if (IS_I965G(dev)) {
1337                 I915_WRITE(dspbase, Offset);
1338                 I915_READ(dspbase);
1339                 I915_WRITE(dspsurf, Start);
1340                 I915_READ(dspsurf);
1341                 I915_WRITE(dsptileoff, (y << 16) | x);
1342         } else {
1343                 I915_WRITE(dspbase, Start + Offset);
1344                 I915_READ(dspbase);
1345         }
1346
1347         if ((IS_I965G(dev) || plane == 0))
1348                 intel_update_fbc(crtc, &crtc->mode);
1349
1350         intel_wait_for_vblank(dev);
1351
1352         if (old_fb) {
1353                 intel_fb = to_intel_framebuffer(old_fb);
1354                 obj_priv = intel_fb->obj->driver_private;
1355                 i915_gem_object_unpin(intel_fb->obj);
1356         }
1357         intel_increase_pllclock(crtc, true);
1358
1359         mutex_unlock(&dev->struct_mutex);
1360
1361         if (!dev->primary->master)
1362                 return 0;
1363
1364         master_priv = dev->primary->master->driver_priv;
1365         if (!master_priv->sarea_priv)
1366                 return 0;
1367
1368         if (pipe) {
1369                 master_priv->sarea_priv->pipeB_x = x;
1370                 master_priv->sarea_priv->pipeB_y = y;
1371         } else {
1372                 master_priv->sarea_priv->pipeA_x = x;
1373                 master_priv->sarea_priv->pipeA_y = y;
1374         }
1375
1376         return 0;
1377 }
1378
1379 /* Disable the VGA plane that we never use */
1380 static void i915_disable_vga (struct drm_device *dev)
1381 {
1382         struct drm_i915_private *dev_priv = dev->dev_private;
1383         u8 sr1;
1384         u32 vga_reg;
1385
1386         if (IS_IRONLAKE(dev))
1387                 vga_reg = CPU_VGACNTRL;
1388         else
1389                 vga_reg = VGACNTRL;
1390
1391         if (I915_READ(vga_reg) & VGA_DISP_DISABLE)
1392                 return;
1393
1394         I915_WRITE8(VGA_SR_INDEX, 1);
1395         sr1 = I915_READ8(VGA_SR_DATA);
1396         I915_WRITE8(VGA_SR_DATA, sr1 | (1 << 5));
1397         udelay(100);
1398
1399         I915_WRITE(vga_reg, VGA_DISP_DISABLE);
1400 }
1401
1402 static void ironlake_disable_pll_edp (struct drm_crtc *crtc)
1403 {
1404         struct drm_device *dev = crtc->dev;
1405         struct drm_i915_private *dev_priv = dev->dev_private;
1406         u32 dpa_ctl;
1407
1408         DRM_DEBUG_KMS("\n");
1409         dpa_ctl = I915_READ(DP_A);
1410         dpa_ctl &= ~DP_PLL_ENABLE;
1411         I915_WRITE(DP_A, dpa_ctl);
1412 }
1413
1414 static void ironlake_enable_pll_edp (struct drm_crtc *crtc)
1415 {
1416         struct drm_device *dev = crtc->dev;
1417         struct drm_i915_private *dev_priv = dev->dev_private;
1418         u32 dpa_ctl;
1419
1420         dpa_ctl = I915_READ(DP_A);
1421         dpa_ctl |= DP_PLL_ENABLE;
1422         I915_WRITE(DP_A, dpa_ctl);
1423         udelay(200);
1424 }
1425
1426
1427 static void ironlake_set_pll_edp (struct drm_crtc *crtc, int clock)
1428 {
1429         struct drm_device *dev = crtc->dev;
1430         struct drm_i915_private *dev_priv = dev->dev_private;
1431         u32 dpa_ctl;
1432
1433         DRM_DEBUG_KMS("eDP PLL enable for clock %d\n", clock);
1434         dpa_ctl = I915_READ(DP_A);
1435         dpa_ctl &= ~DP_PLL_FREQ_MASK;
1436
1437         if (clock < 200000) {
1438                 u32 temp;
1439                 dpa_ctl |= DP_PLL_FREQ_160MHZ;
1440                 /* workaround for 160Mhz:
1441                    1) program 0x4600c bits 15:0 = 0x8124
1442                    2) program 0x46010 bit 0 = 1
1443                    3) program 0x46034 bit 24 = 1
1444                    4) program 0x64000 bit 14 = 1
1445                    */
1446                 temp = I915_READ(0x4600c);
1447                 temp &= 0xffff0000;
1448                 I915_WRITE(0x4600c, temp | 0x8124);
1449
1450                 temp = I915_READ(0x46010);
1451                 I915_WRITE(0x46010, temp | 1);
1452
1453                 temp = I915_READ(0x46034);
1454                 I915_WRITE(0x46034, temp | (1 << 24));
1455         } else {
1456                 dpa_ctl |= DP_PLL_FREQ_270MHZ;
1457         }
1458         I915_WRITE(DP_A, dpa_ctl);
1459
1460         udelay(500);
1461 }
1462
1463 static void ironlake_crtc_dpms(struct drm_crtc *crtc, int mode)
1464 {
1465         struct drm_device *dev = crtc->dev;
1466         struct drm_i915_private *dev_priv = dev->dev_private;
1467         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1468         int pipe = intel_crtc->pipe;
1469         int plane = intel_crtc->plane;
1470         int pch_dpll_reg = (pipe == 0) ? PCH_DPLL_A : PCH_DPLL_B;
1471         int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
1472         int dspcntr_reg = (plane == 0) ? DSPACNTR : DSPBCNTR;
1473         int dspbase_reg = (plane == 0) ? DSPAADDR : DSPBADDR;
1474         int fdi_tx_reg = (pipe == 0) ? FDI_TXA_CTL : FDI_TXB_CTL;
1475         int fdi_rx_reg = (pipe == 0) ? FDI_RXA_CTL : FDI_RXB_CTL;
1476         int fdi_rx_iir_reg = (pipe == 0) ? FDI_RXA_IIR : FDI_RXB_IIR;
1477         int fdi_rx_imr_reg = (pipe == 0) ? FDI_RXA_IMR : FDI_RXB_IMR;
1478         int transconf_reg = (pipe == 0) ? TRANSACONF : TRANSBCONF;
1479         int pf_ctl_reg = (pipe == 0) ? PFA_CTL_1 : PFB_CTL_1;
1480         int pf_win_size = (pipe == 0) ? PFA_WIN_SZ : PFB_WIN_SZ;
1481         int pf_win_pos = (pipe == 0) ? PFA_WIN_POS : PFB_WIN_POS;
1482         int cpu_htot_reg = (pipe == 0) ? HTOTAL_A : HTOTAL_B;
1483         int cpu_hblank_reg = (pipe == 0) ? HBLANK_A : HBLANK_B;
1484         int cpu_hsync_reg = (pipe == 0) ? HSYNC_A : HSYNC_B;
1485         int cpu_vtot_reg = (pipe == 0) ? VTOTAL_A : VTOTAL_B;
1486         int cpu_vblank_reg = (pipe == 0) ? VBLANK_A : VBLANK_B;
1487         int cpu_vsync_reg = (pipe == 0) ? VSYNC_A : VSYNC_B;
1488         int trans_htot_reg = (pipe == 0) ? TRANS_HTOTAL_A : TRANS_HTOTAL_B;
1489         int trans_hblank_reg = (pipe == 0) ? TRANS_HBLANK_A : TRANS_HBLANK_B;
1490         int trans_hsync_reg = (pipe == 0) ? TRANS_HSYNC_A : TRANS_HSYNC_B;
1491         int trans_vtot_reg = (pipe == 0) ? TRANS_VTOTAL_A : TRANS_VTOTAL_B;
1492         int trans_vblank_reg = (pipe == 0) ? TRANS_VBLANK_A : TRANS_VBLANK_B;
1493         int trans_vsync_reg = (pipe == 0) ? TRANS_VSYNC_A : TRANS_VSYNC_B;
1494         u32 temp;
1495         int tries = 5, j, n;
1496         u32 pipe_bpc;
1497
1498         temp = I915_READ(pipeconf_reg);
1499         pipe_bpc = temp & PIPE_BPC_MASK;
1500
1501         /* XXX: When our outputs are all unaware of DPMS modes other than off
1502          * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
1503          */
1504         switch (mode) {
1505         case DRM_MODE_DPMS_ON:
1506         case DRM_MODE_DPMS_STANDBY:
1507         case DRM_MODE_DPMS_SUSPEND:
1508                 DRM_DEBUG_KMS("crtc %d dpms on\n", pipe);
1509
1510                 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
1511                         temp = I915_READ(PCH_LVDS);
1512                         if ((temp & LVDS_PORT_EN) == 0) {
1513                                 I915_WRITE(PCH_LVDS, temp | LVDS_PORT_EN);
1514                                 POSTING_READ(PCH_LVDS);
1515                         }
1516                 }
1517
1518                 if (HAS_eDP) {
1519                         /* enable eDP PLL */
1520                         ironlake_enable_pll_edp(crtc);
1521                 } else {
1522                         /* enable PCH DPLL */
1523                         temp = I915_READ(pch_dpll_reg);
1524                         if ((temp & DPLL_VCO_ENABLE) == 0) {
1525                                 I915_WRITE(pch_dpll_reg, temp | DPLL_VCO_ENABLE);
1526                                 I915_READ(pch_dpll_reg);
1527                         }
1528
1529                         /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
1530                         temp = I915_READ(fdi_rx_reg);
1531                         /*
1532                          * make the BPC in FDI Rx be consistent with that in
1533                          * pipeconf reg.
1534                          */
1535                         temp &= ~(0x7 << 16);
1536                         temp |= (pipe_bpc << 11);
1537                         I915_WRITE(fdi_rx_reg, temp | FDI_RX_PLL_ENABLE |
1538                                         FDI_SEL_PCDCLK |
1539                                         FDI_DP_PORT_WIDTH_X4); /* default 4 lanes */
1540                         I915_READ(fdi_rx_reg);
1541                         udelay(200);
1542
1543                         /* Enable CPU FDI TX PLL, always on for Ironlake */
1544                         temp = I915_READ(fdi_tx_reg);
1545                         if ((temp & FDI_TX_PLL_ENABLE) == 0) {
1546                                 I915_WRITE(fdi_tx_reg, temp | FDI_TX_PLL_ENABLE);
1547                                 I915_READ(fdi_tx_reg);
1548                                 udelay(100);
1549                         }
1550                 }
1551
1552                 /* Enable panel fitting for LVDS */
1553                 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
1554                         temp = I915_READ(pf_ctl_reg);
1555                         I915_WRITE(pf_ctl_reg, temp | PF_ENABLE | PF_FILTER_MED_3x3);
1556
1557                         /* currently full aspect */
1558                         I915_WRITE(pf_win_pos, 0);
1559
1560                         I915_WRITE(pf_win_size,
1561                                    (dev_priv->panel_fixed_mode->hdisplay << 16) |
1562                                    (dev_priv->panel_fixed_mode->vdisplay));
1563                 }
1564
1565                 /* Enable CPU pipe */
1566                 temp = I915_READ(pipeconf_reg);
1567                 if ((temp & PIPEACONF_ENABLE) == 0) {
1568                         I915_WRITE(pipeconf_reg, temp | PIPEACONF_ENABLE);
1569                         I915_READ(pipeconf_reg);
1570                         udelay(100);
1571                 }
1572
1573                 /* configure and enable CPU plane */
1574                 temp = I915_READ(dspcntr_reg);
1575                 if ((temp & DISPLAY_PLANE_ENABLE) == 0) {
1576                         I915_WRITE(dspcntr_reg, temp | DISPLAY_PLANE_ENABLE);
1577                         /* Flush the plane changes */
1578                         I915_WRITE(dspbase_reg, I915_READ(dspbase_reg));
1579                 }
1580
1581                 if (!HAS_eDP) {
1582                         /* enable CPU FDI TX and PCH FDI RX */
1583                         temp = I915_READ(fdi_tx_reg);
1584                         temp |= FDI_TX_ENABLE;
1585                         temp |= FDI_DP_PORT_WIDTH_X4; /* default */
1586                         temp &= ~FDI_LINK_TRAIN_NONE;
1587                         temp |= FDI_LINK_TRAIN_PATTERN_1;
1588                         I915_WRITE(fdi_tx_reg, temp);
1589                         I915_READ(fdi_tx_reg);
1590
1591                         temp = I915_READ(fdi_rx_reg);
1592                         temp &= ~FDI_LINK_TRAIN_NONE;
1593                         temp |= FDI_LINK_TRAIN_PATTERN_1;
1594                         I915_WRITE(fdi_rx_reg, temp | FDI_RX_ENABLE);
1595                         I915_READ(fdi_rx_reg);
1596
1597                         udelay(150);
1598
1599                         /* Train FDI. */
1600                         /* umask FDI RX Interrupt symbol_lock and bit_lock bit
1601                            for train result */
1602                         temp = I915_READ(fdi_rx_imr_reg);
1603                         temp &= ~FDI_RX_SYMBOL_LOCK;
1604                         temp &= ~FDI_RX_BIT_LOCK;
1605                         I915_WRITE(fdi_rx_imr_reg, temp);
1606                         I915_READ(fdi_rx_imr_reg);
1607                         udelay(150);
1608
1609                         temp = I915_READ(fdi_rx_iir_reg);
1610                         DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
1611
1612                         if ((temp & FDI_RX_BIT_LOCK) == 0) {
1613                                 for (j = 0; j < tries; j++) {
1614                                         temp = I915_READ(fdi_rx_iir_reg);
1615                                         DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n",
1616                                                                 temp);
1617                                         if (temp & FDI_RX_BIT_LOCK)
1618                                                 break;
1619                                         udelay(200);
1620                                 }
1621                                 if (j != tries)
1622                                         I915_WRITE(fdi_rx_iir_reg,
1623                                                         temp | FDI_RX_BIT_LOCK);
1624                                 else
1625                                         DRM_DEBUG_KMS("train 1 fail\n");
1626                         } else {
1627                                 I915_WRITE(fdi_rx_iir_reg,
1628                                                 temp | FDI_RX_BIT_LOCK);
1629                                 DRM_DEBUG_KMS("train 1 ok 2!\n");
1630                         }
1631                         temp = I915_READ(fdi_tx_reg);
1632                         temp &= ~FDI_LINK_TRAIN_NONE;
1633                         temp |= FDI_LINK_TRAIN_PATTERN_2;
1634                         I915_WRITE(fdi_tx_reg, temp);
1635
1636                         temp = I915_READ(fdi_rx_reg);
1637                         temp &= ~FDI_LINK_TRAIN_NONE;
1638                         temp |= FDI_LINK_TRAIN_PATTERN_2;
1639                         I915_WRITE(fdi_rx_reg, temp);
1640
1641                         udelay(150);
1642
1643                         temp = I915_READ(fdi_rx_iir_reg);
1644                         DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
1645
1646                         if ((temp & FDI_RX_SYMBOL_LOCK) == 0) {
1647                                 for (j = 0; j < tries; j++) {
1648                                         temp = I915_READ(fdi_rx_iir_reg);
1649                                         DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n",
1650                                                                 temp);
1651                                         if (temp & FDI_RX_SYMBOL_LOCK)
1652                                                 break;
1653                                         udelay(200);
1654                                 }
1655                                 if (j != tries) {
1656                                         I915_WRITE(fdi_rx_iir_reg,
1657                                                         temp | FDI_RX_SYMBOL_LOCK);
1658                                         DRM_DEBUG_KMS("train 2 ok 1!\n");
1659                                 } else
1660                                         DRM_DEBUG_KMS("train 2 fail\n");
1661                         } else {
1662                                 I915_WRITE(fdi_rx_iir_reg,
1663                                                 temp | FDI_RX_SYMBOL_LOCK);
1664                                 DRM_DEBUG_KMS("train 2 ok 2!\n");
1665                         }
1666                         DRM_DEBUG_KMS("train done\n");
1667
1668                         /* set transcoder timing */
1669                         I915_WRITE(trans_htot_reg, I915_READ(cpu_htot_reg));
1670                         I915_WRITE(trans_hblank_reg, I915_READ(cpu_hblank_reg));
1671                         I915_WRITE(trans_hsync_reg, I915_READ(cpu_hsync_reg));
1672
1673                         I915_WRITE(trans_vtot_reg, I915_READ(cpu_vtot_reg));
1674                         I915_WRITE(trans_vblank_reg, I915_READ(cpu_vblank_reg));
1675                         I915_WRITE(trans_vsync_reg, I915_READ(cpu_vsync_reg));
1676
1677                         /* enable PCH transcoder */
1678                         temp = I915_READ(transconf_reg);
1679                         /*
1680                          * make the BPC in transcoder be consistent with
1681                          * that in pipeconf reg.
1682                          */
1683                         temp &= ~PIPE_BPC_MASK;
1684                         temp |= pipe_bpc;
1685                         I915_WRITE(transconf_reg, temp | TRANS_ENABLE);
1686                         I915_READ(transconf_reg);
1687
1688                         while ((I915_READ(transconf_reg) & TRANS_STATE_ENABLE) == 0)
1689                                 ;
1690
1691                         /* enable normal */
1692
1693                         temp = I915_READ(fdi_tx_reg);
1694                         temp &= ~FDI_LINK_TRAIN_NONE;
1695                         I915_WRITE(fdi_tx_reg, temp | FDI_LINK_TRAIN_NONE |
1696                                         FDI_TX_ENHANCE_FRAME_ENABLE);
1697                         I915_READ(fdi_tx_reg);
1698
1699                         temp = I915_READ(fdi_rx_reg);
1700                         temp &= ~FDI_LINK_TRAIN_NONE;
1701                         I915_WRITE(fdi_rx_reg, temp | FDI_LINK_TRAIN_NONE |
1702                                         FDI_RX_ENHANCE_FRAME_ENABLE);
1703                         I915_READ(fdi_rx_reg);
1704
1705                         /* wait one idle pattern time */
1706                         udelay(100);
1707
1708                 }
1709
1710                 intel_crtc_load_lut(crtc);
1711
1712         break;
1713         case DRM_MODE_DPMS_OFF:
1714                 DRM_DEBUG_KMS("crtc %d dpms off\n", pipe);
1715
1716                 /* Disable display plane */
1717                 temp = I915_READ(dspcntr_reg);
1718                 if ((temp & DISPLAY_PLANE_ENABLE) != 0) {
1719                         I915_WRITE(dspcntr_reg, temp & ~DISPLAY_PLANE_ENABLE);
1720                         /* Flush the plane changes */
1721                         I915_WRITE(dspbase_reg, I915_READ(dspbase_reg));
1722                         I915_READ(dspbase_reg);
1723                 }
1724
1725                 i915_disable_vga(dev);
1726
1727                 /* disable cpu pipe, disable after all planes disabled */
1728                 temp = I915_READ(pipeconf_reg);
1729                 if ((temp & PIPEACONF_ENABLE) != 0) {
1730                         I915_WRITE(pipeconf_reg, temp & ~PIPEACONF_ENABLE);
1731                         I915_READ(pipeconf_reg);
1732                         n = 0;
1733                         /* wait for cpu pipe off, pipe state */
1734                         while ((I915_READ(pipeconf_reg) & I965_PIPECONF_ACTIVE) != 0) {
1735                                 n++;
1736                                 if (n < 60) {
1737                                         udelay(500);
1738                                         continue;
1739                                 } else {
1740                                         DRM_DEBUG_KMS("pipe %d off delay\n",
1741                                                                 pipe);
1742                                         break;
1743                                 }
1744                         }
1745                 } else
1746                         DRM_DEBUG_KMS("crtc %d is disabled\n", pipe);
1747
1748                 udelay(100);
1749
1750                 /* Disable PF */
1751                 temp = I915_READ(pf_ctl_reg);
1752                 if ((temp & PF_ENABLE) != 0) {
1753                         I915_WRITE(pf_ctl_reg, temp & ~PF_ENABLE);
1754                         I915_READ(pf_ctl_reg);
1755                 }
1756                 I915_WRITE(pf_win_size, 0);
1757
1758                 /* disable CPU FDI tx and PCH FDI rx */
1759                 temp = I915_READ(fdi_tx_reg);
1760                 I915_WRITE(fdi_tx_reg, temp & ~FDI_TX_ENABLE);
1761                 I915_READ(fdi_tx_reg);
1762
1763                 temp = I915_READ(fdi_rx_reg);
1764                 /* BPC in FDI rx is consistent with that in pipeconf */
1765                 temp &= ~(0x07 << 16);
1766                 temp |= (pipe_bpc << 11);
1767                 I915_WRITE(fdi_rx_reg, temp & ~FDI_RX_ENABLE);
1768                 I915_READ(fdi_rx_reg);
1769
1770                 udelay(100);
1771
1772                 /* still set train pattern 1 */
1773                 temp = I915_READ(fdi_tx_reg);
1774                 temp &= ~FDI_LINK_TRAIN_NONE;
1775                 temp |= FDI_LINK_TRAIN_PATTERN_1;
1776                 I915_WRITE(fdi_tx_reg, temp);
1777
1778                 temp = I915_READ(fdi_rx_reg);
1779                 temp &= ~FDI_LINK_TRAIN_NONE;
1780                 temp |= FDI_LINK_TRAIN_PATTERN_1;
1781                 I915_WRITE(fdi_rx_reg, temp);
1782
1783                 udelay(100);
1784
1785                 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
1786                         temp = I915_READ(PCH_LVDS);
1787                         I915_WRITE(PCH_LVDS, temp & ~LVDS_PORT_EN);
1788                         I915_READ(PCH_LVDS);
1789                         udelay(100);
1790                 }
1791
1792                 /* disable PCH transcoder */
1793                 temp = I915_READ(transconf_reg);
1794                 if ((temp & TRANS_ENABLE) != 0) {
1795                         I915_WRITE(transconf_reg, temp & ~TRANS_ENABLE);
1796                         I915_READ(transconf_reg);
1797                         n = 0;
1798                         /* wait for PCH transcoder off, transcoder state */
1799                         while ((I915_READ(transconf_reg) & TRANS_STATE_ENABLE) != 0) {
1800                                 n++;
1801                                 if (n < 60) {
1802                                         udelay(500);
1803                                         continue;
1804                                 } else {
1805                                         DRM_DEBUG_KMS("transcoder %d off "
1806                                                         "delay\n", pipe);
1807                                         break;
1808                                 }
1809                         }
1810                 }
1811                 temp = I915_READ(transconf_reg);
1812                 /* BPC in transcoder is consistent with that in pipeconf */
1813                 temp &= ~PIPE_BPC_MASK;
1814                 temp |= pipe_bpc;
1815                 I915_WRITE(transconf_reg, temp);
1816                 I915_READ(transconf_reg);
1817                 udelay(100);
1818
1819                 /* disable PCH DPLL */
1820                 temp = I915_READ(pch_dpll_reg);
1821                 if ((temp & DPLL_VCO_ENABLE) != 0) {
1822                         I915_WRITE(pch_dpll_reg, temp & ~DPLL_VCO_ENABLE);
1823                         I915_READ(pch_dpll_reg);
1824                 }
1825
1826                 if (HAS_eDP) {
1827                         ironlake_disable_pll_edp(crtc);
1828                 }
1829
1830                 temp = I915_READ(fdi_rx_reg);
1831                 temp &= ~FDI_SEL_PCDCLK;
1832                 I915_WRITE(fdi_rx_reg, temp);
1833                 I915_READ(fdi_rx_reg);
1834
1835                 temp = I915_READ(fdi_rx_reg);
1836                 temp &= ~FDI_RX_PLL_ENABLE;
1837                 I915_WRITE(fdi_rx_reg, temp);
1838                 I915_READ(fdi_rx_reg);
1839
1840                 /* Disable CPU FDI TX PLL */
1841                 temp = I915_READ(fdi_tx_reg);
1842                 if ((temp & FDI_TX_PLL_ENABLE) != 0) {
1843                         I915_WRITE(fdi_tx_reg, temp & ~FDI_TX_PLL_ENABLE);
1844                         I915_READ(fdi_tx_reg);
1845                         udelay(100);
1846                 }
1847
1848                 /* Wait for the clocks to turn off. */
1849                 udelay(100);
1850                 break;
1851         }
1852 }
1853
1854 static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
1855 {
1856         struct intel_overlay *overlay;
1857         int ret;
1858
1859         if (!enable && intel_crtc->overlay) {
1860                 overlay = intel_crtc->overlay;
1861                 mutex_lock(&overlay->dev->struct_mutex);
1862                 for (;;) {
1863                         ret = intel_overlay_switch_off(overlay);
1864                         if (ret == 0)
1865                                 break;
1866
1867                         ret = intel_overlay_recover_from_interrupt(overlay, 0);
1868                         if (ret != 0) {
1869                                 /* overlay doesn't react anymore. Usually
1870                                  * results in a black screen and an unkillable
1871                                  * X server. */
1872                                 BUG();
1873                                 overlay->hw_wedged = HW_WEDGED;
1874                                 break;
1875                         }
1876                 }
1877                 mutex_unlock(&overlay->dev->struct_mutex);
1878         }
1879         /* Let userspace switch the overlay on again. In most cases userspace
1880          * has to recompute where to put it anyway. */
1881
1882         return;
1883 }
1884
1885 static void i9xx_crtc_dpms(struct drm_crtc *crtc, int mode)
1886 {
1887         struct drm_device *dev = crtc->dev;
1888         struct drm_i915_private *dev_priv = dev->dev_private;
1889         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1890         int pipe = intel_crtc->pipe;
1891         int plane = intel_crtc->plane;
1892         int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
1893         int dspcntr_reg = (plane == 0) ? DSPACNTR : DSPBCNTR;
1894         int dspbase_reg = (plane == 0) ? DSPAADDR : DSPBADDR;
1895         int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
1896         u32 temp;
1897
1898         /* XXX: When our outputs are all unaware of DPMS modes other than off
1899          * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
1900          */
1901         switch (mode) {
1902         case DRM_MODE_DPMS_ON:
1903         case DRM_MODE_DPMS_STANDBY:
1904         case DRM_MODE_DPMS_SUSPEND:
1905                 intel_update_watermarks(dev);
1906
1907                 /* Enable the DPLL */
1908                 temp = I915_READ(dpll_reg);
1909                 if ((temp & DPLL_VCO_ENABLE) == 0) {
1910                         I915_WRITE(dpll_reg, temp);
1911                         I915_READ(dpll_reg);
1912                         /* Wait for the clocks to stabilize. */
1913                         udelay(150);
1914                         I915_WRITE(dpll_reg, temp | DPLL_VCO_ENABLE);
1915                         I915_READ(dpll_reg);
1916                         /* Wait for the clocks to stabilize. */
1917                         udelay(150);
1918                         I915_WRITE(dpll_reg, temp | DPLL_VCO_ENABLE);
1919                         I915_READ(dpll_reg);
1920                         /* Wait for the clocks to stabilize. */
1921                         udelay(150);
1922                 }
1923
1924                 /* Enable the pipe */
1925                 temp = I915_READ(pipeconf_reg);
1926                 if ((temp & PIPEACONF_ENABLE) == 0)
1927                         I915_WRITE(pipeconf_reg, temp | PIPEACONF_ENABLE);
1928
1929                 /* Enable the plane */
1930                 temp = I915_READ(dspcntr_reg);
1931                 if ((temp & DISPLAY_PLANE_ENABLE) == 0) {
1932                         I915_WRITE(dspcntr_reg, temp | DISPLAY_PLANE_ENABLE);
1933                         /* Flush the plane changes */
1934                         I915_WRITE(dspbase_reg, I915_READ(dspbase_reg));
1935                 }
1936
1937                 intel_crtc_load_lut(crtc);
1938
1939                 if ((IS_I965G(dev) || plane == 0))
1940                         intel_update_fbc(crtc, &crtc->mode);
1941
1942                 /* Give the overlay scaler a chance to enable if it's on this pipe */
1943                 intel_crtc_dpms_overlay(intel_crtc, true);
1944         break;
1945         case DRM_MODE_DPMS_OFF:
1946                 intel_update_watermarks(dev);
1947
1948                 /* Give the overlay scaler a chance to disable if it's on this pipe */
1949                 intel_crtc_dpms_overlay(intel_crtc, false);
1950                 drm_vblank_off(dev, pipe);
1951
1952                 if (dev_priv->cfb_plane == plane &&
1953                     dev_priv->display.disable_fbc)
1954                         dev_priv->display.disable_fbc(dev);
1955
1956                 /* Disable the VGA plane that we never use */
1957                 i915_disable_vga(dev);
1958
1959                 /* Disable display plane */
1960                 temp = I915_READ(dspcntr_reg);
1961                 if ((temp & DISPLAY_PLANE_ENABLE) != 0) {
1962                         I915_WRITE(dspcntr_reg, temp & ~DISPLAY_PLANE_ENABLE);
1963                         /* Flush the plane changes */
1964                         I915_WRITE(dspbase_reg, I915_READ(dspbase_reg));
1965                         I915_READ(dspbase_reg);
1966                 }
1967
1968                 if (!IS_I9XX(dev)) {
1969                         /* Wait for vblank for the disable to take effect */
1970                         intel_wait_for_vblank(dev);
1971                 }
1972
1973                 /* Next, disable display pipes */
1974                 temp = I915_READ(pipeconf_reg);
1975                 if ((temp & PIPEACONF_ENABLE) != 0) {
1976                         I915_WRITE(pipeconf_reg, temp & ~PIPEACONF_ENABLE);
1977                         I915_READ(pipeconf_reg);
1978                 }
1979
1980                 /* Wait for vblank for the disable to take effect. */
1981                 intel_wait_for_vblank(dev);
1982
1983                 temp = I915_READ(dpll_reg);
1984                 if ((temp & DPLL_VCO_ENABLE) != 0) {
1985                         I915_WRITE(dpll_reg, temp & ~DPLL_VCO_ENABLE);
1986                         I915_READ(dpll_reg);
1987                 }
1988
1989                 /* Wait for the clocks to turn off. */
1990                 udelay(150);
1991                 break;
1992         }
1993 }
1994
1995 /**
1996  * Sets the power management mode of the pipe and plane.
1997  *
1998  * This code should probably grow support for turning the cursor off and back
1999  * on appropriately at the same time as we're turning the pipe off/on.
2000  */
2001 static void intel_crtc_dpms(struct drm_crtc *crtc, int mode)
2002 {
2003         struct drm_device *dev = crtc->dev;
2004         struct drm_i915_private *dev_priv = dev->dev_private;
2005         struct drm_i915_master_private *master_priv;
2006         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2007         int pipe = intel_crtc->pipe;
2008         bool enabled;
2009
2010         dev_priv->display.dpms(crtc, mode);
2011
2012         intel_crtc->dpms_mode = mode;
2013
2014         if (!dev->primary->master)
2015                 return;
2016
2017         master_priv = dev->primary->master->driver_priv;
2018         if (!master_priv->sarea_priv)
2019                 return;
2020
2021         enabled = crtc->enabled && mode != DRM_MODE_DPMS_OFF;
2022
2023         switch (pipe) {
2024         case 0:
2025                 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
2026                 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
2027                 break;
2028         case 1:
2029                 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
2030                 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
2031                 break;
2032         default:
2033                 DRM_ERROR("Can't update pipe %d in SAREA\n", pipe);
2034                 break;
2035         }
2036 }
2037
2038 static void intel_crtc_prepare (struct drm_crtc *crtc)
2039 {
2040         struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
2041         crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
2042 }
2043
2044 static void intel_crtc_commit (struct drm_crtc *crtc)
2045 {
2046         struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
2047         crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
2048 }
2049
2050 void intel_encoder_prepare (struct drm_encoder *encoder)
2051 {
2052         struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
2053         /* lvds has its own version of prepare see intel_lvds_prepare */
2054         encoder_funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
2055 }
2056
2057 void intel_encoder_commit (struct drm_encoder *encoder)
2058 {
2059         struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
2060         /* lvds has its own version of commit see intel_lvds_commit */
2061         encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
2062 }
2063
2064 static bool intel_crtc_mode_fixup(struct drm_crtc *crtc,
2065                                   struct drm_display_mode *mode,
2066                                   struct drm_display_mode *adjusted_mode)
2067 {
2068         struct drm_device *dev = crtc->dev;
2069         if (IS_IRONLAKE(dev)) {
2070                 /* FDI link clock is fixed at 2.7G */
2071                 if (mode->clock * 3 > 27000 * 4)
2072                         return MODE_CLOCK_HIGH;
2073         }
2074         return true;
2075 }
2076
2077 static int i945_get_display_clock_speed(struct drm_device *dev)
2078 {
2079         return 400000;
2080 }
2081
2082 static int i915_get_display_clock_speed(struct drm_device *dev)
2083 {
2084         return 333000;
2085 }
2086
2087 static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
2088 {
2089         return 200000;
2090 }
2091
2092 static int i915gm_get_display_clock_speed(struct drm_device *dev)
2093 {
2094         u16 gcfgc = 0;
2095
2096         pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
2097
2098         if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
2099                 return 133000;
2100         else {
2101                 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
2102                 case GC_DISPLAY_CLOCK_333_MHZ:
2103                         return 333000;
2104                 default:
2105                 case GC_DISPLAY_CLOCK_190_200_MHZ:
2106                         return 190000;
2107                 }
2108         }
2109 }
2110
2111 static int i865_get_display_clock_speed(struct drm_device *dev)
2112 {
2113         return 266000;
2114 }
2115
2116 static int i855_get_display_clock_speed(struct drm_device *dev)
2117 {
2118         u16 hpllcc = 0;
2119         /* Assume that the hardware is in the high speed state.  This
2120          * should be the default.
2121          */
2122         switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
2123         case GC_CLOCK_133_200:
2124         case GC_CLOCK_100_200:
2125                 return 200000;
2126         case GC_CLOCK_166_250:
2127                 return 250000;
2128         case GC_CLOCK_100_133:
2129                 return 133000;
2130         }
2131
2132         /* Shouldn't happen */
2133         return 0;
2134 }
2135
2136 static int i830_get_display_clock_speed(struct drm_device *dev)
2137 {
2138         return 133000;
2139 }
2140
2141 /**
2142  * Return the pipe currently connected to the panel fitter,
2143  * or -1 if the panel fitter is not present or not in use
2144  */
2145 int intel_panel_fitter_pipe (struct drm_device *dev)
2146 {
2147         struct drm_i915_private *dev_priv = dev->dev_private;
2148         u32  pfit_control;
2149
2150         /* i830 doesn't have a panel fitter */
2151         if (IS_I830(dev))
2152                 return -1;
2153
2154         pfit_control = I915_READ(PFIT_CONTROL);
2155
2156         /* See if the panel fitter is in use */
2157         if ((pfit_control & PFIT_ENABLE) == 0)
2158                 return -1;
2159
2160         /* 965 can place panel fitter on either pipe */
2161         if (IS_I965G(dev))
2162                 return (pfit_control >> 29) & 0x3;
2163
2164         /* older chips can only use pipe 1 */
2165         return 1;
2166 }
2167
2168 struct fdi_m_n {
2169         u32        tu;
2170         u32        gmch_m;
2171         u32        gmch_n;
2172         u32        link_m;
2173         u32        link_n;
2174 };
2175
2176 static void
2177 fdi_reduce_ratio(u32 *num, u32 *den)
2178 {
2179         while (*num > 0xffffff || *den > 0xffffff) {
2180                 *num >>= 1;
2181                 *den >>= 1;
2182         }
2183 }
2184
2185 #define DATA_N 0x800000
2186 #define LINK_N 0x80000
2187
2188 static void
2189 ironlake_compute_m_n(int bits_per_pixel, int nlanes, int pixel_clock,
2190                      int link_clock, struct fdi_m_n *m_n)
2191 {
2192         u64 temp;
2193
2194         m_n->tu = 64; /* default size */
2195
2196         temp = (u64) DATA_N * pixel_clock;
2197         temp = div_u64(temp, link_clock);
2198         m_n->gmch_m = div_u64(temp * bits_per_pixel, nlanes);
2199         m_n->gmch_m >>= 3; /* convert to bytes_per_pixel */
2200         m_n->gmch_n = DATA_N;
2201         fdi_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
2202
2203         temp = (u64) LINK_N * pixel_clock;
2204         m_n->link_m = div_u64(temp, link_clock);
2205         m_n->link_n = LINK_N;
2206         fdi_reduce_ratio(&m_n->link_m, &m_n->link_n);
2207 }
2208
2209
2210 struct intel_watermark_params {
2211         unsigned long fifo_size;
2212         unsigned long max_wm;
2213         unsigned long default_wm;
2214         unsigned long guard_size;
2215         unsigned long cacheline_size;
2216 };
2217
2218 /* Pineview has different values for various configs */
2219 static struct intel_watermark_params pineview_display_wm = {
2220         PINEVIEW_DISPLAY_FIFO,
2221         PINEVIEW_MAX_WM,
2222         PINEVIEW_DFT_WM,
2223         PINEVIEW_GUARD_WM,
2224         PINEVIEW_FIFO_LINE_SIZE
2225 };
2226 static struct intel_watermark_params pineview_display_hplloff_wm = {
2227         PINEVIEW_DISPLAY_FIFO,
2228         PINEVIEW_MAX_WM,
2229         PINEVIEW_DFT_HPLLOFF_WM,
2230         PINEVIEW_GUARD_WM,
2231         PINEVIEW_FIFO_LINE_SIZE
2232 };
2233 static struct intel_watermark_params pineview_cursor_wm = {
2234         PINEVIEW_CURSOR_FIFO,
2235         PINEVIEW_CURSOR_MAX_WM,
2236         PINEVIEW_CURSOR_DFT_WM,
2237         PINEVIEW_CURSOR_GUARD_WM,
2238         PINEVIEW_FIFO_LINE_SIZE,
2239 };
2240 static struct intel_watermark_params pineview_cursor_hplloff_wm = {
2241         PINEVIEW_CURSOR_FIFO,
2242         PINEVIEW_CURSOR_MAX_WM,
2243         PINEVIEW_CURSOR_DFT_WM,
2244         PINEVIEW_CURSOR_GUARD_WM,
2245         PINEVIEW_FIFO_LINE_SIZE
2246 };
2247 static struct intel_watermark_params g4x_wm_info = {
2248         G4X_FIFO_SIZE,
2249         G4X_MAX_WM,
2250         G4X_MAX_WM,
2251         2,
2252         G4X_FIFO_LINE_SIZE,
2253 };
2254 static struct intel_watermark_params i945_wm_info = {
2255         I945_FIFO_SIZE,
2256         I915_MAX_WM,
2257         1,
2258         2,
2259         I915_FIFO_LINE_SIZE
2260 };
2261 static struct intel_watermark_params i915_wm_info = {
2262         I915_FIFO_SIZE,
2263         I915_MAX_WM,
2264         1,
2265         2,
2266         I915_FIFO_LINE_SIZE
2267 };
2268 static struct intel_watermark_params i855_wm_info = {
2269         I855GM_FIFO_SIZE,
2270         I915_MAX_WM,
2271         1,
2272         2,
2273         I830_FIFO_LINE_SIZE
2274 };
2275 static struct intel_watermark_params i830_wm_info = {
2276         I830_FIFO_SIZE,
2277         I915_MAX_WM,
2278         1,
2279         2,
2280         I830_FIFO_LINE_SIZE
2281 };
2282
2283 /**
2284  * intel_calculate_wm - calculate watermark level
2285  * @clock_in_khz: pixel clock
2286  * @wm: chip FIFO params
2287  * @pixel_size: display pixel size
2288  * @latency_ns: memory latency for the platform
2289  *
2290  * Calculate the watermark level (the level at which the display plane will
2291  * start fetching from memory again).  Each chip has a different display
2292  * FIFO size and allocation, so the caller needs to figure that out and pass
2293  * in the correct intel_watermark_params structure.
2294  *
2295  * As the pixel clock runs, the FIFO will be drained at a rate that depends
2296  * on the pixel size.  When it reaches the watermark level, it'll start
2297  * fetching FIFO line sized based chunks from memory until the FIFO fills
2298  * past the watermark point.  If the FIFO drains completely, a FIFO underrun
2299  * will occur, and a display engine hang could result.
2300  */
2301 static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
2302                                         struct intel_watermark_params *wm,
2303                                         int pixel_size,
2304                                         unsigned long latency_ns)
2305 {
2306         long entries_required, wm_size;
2307
2308         /*
2309          * Note: we need to make sure we don't overflow for various clock &
2310          * latency values.
2311          * clocks go from a few thousand to several hundred thousand.
2312          * latency is usually a few thousand
2313          */
2314         entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
2315                 1000;
2316         entries_required /= wm->cacheline_size;
2317
2318         DRM_DEBUG_KMS("FIFO entries required for mode: %d\n", entries_required);
2319
2320         wm_size = wm->fifo_size - (entries_required + wm->guard_size);
2321
2322         DRM_DEBUG_KMS("FIFO watermark level: %d\n", wm_size);
2323
2324         /* Don't promote wm_size to unsigned... */
2325         if (wm_size > (long)wm->max_wm)
2326                 wm_size = wm->max_wm;
2327         if (wm_size <= 0)
2328                 wm_size = wm->default_wm;
2329         return wm_size;
2330 }
2331
2332 struct cxsr_latency {
2333         int is_desktop;
2334         unsigned long fsb_freq;
2335         unsigned long mem_freq;
2336         unsigned long display_sr;
2337         unsigned long display_hpll_disable;
2338         unsigned long cursor_sr;
2339         unsigned long cursor_hpll_disable;
2340 };
2341
2342 static struct cxsr_latency cxsr_latency_table[] = {
2343         {1, 800, 400, 3382, 33382, 3983, 33983},    /* DDR2-400 SC */
2344         {1, 800, 667, 3354, 33354, 3807, 33807},    /* DDR2-667 SC */
2345         {1, 800, 800, 3347, 33347, 3763, 33763},    /* DDR2-800 SC */
2346
2347         {1, 667, 400, 3400, 33400, 4021, 34021},    /* DDR2-400 SC */
2348         {1, 667, 667, 3372, 33372, 3845, 33845},    /* DDR2-667 SC */
2349         {1, 667, 800, 3386, 33386, 3822, 33822},    /* DDR2-800 SC */
2350
2351         {1, 400, 400, 3472, 33472, 4173, 34173},    /* DDR2-400 SC */
2352         {1, 400, 667, 3443, 33443, 3996, 33996},    /* DDR2-667 SC */
2353         {1, 400, 800, 3430, 33430, 3946, 33946},    /* DDR2-800 SC */
2354
2355         {0, 800, 400, 3438, 33438, 4065, 34065},    /* DDR2-400 SC */
2356         {0, 800, 667, 3410, 33410, 3889, 33889},    /* DDR2-667 SC */
2357         {0, 800, 800, 3403, 33403, 3845, 33845},    /* DDR2-800 SC */
2358
2359         {0, 667, 400, 3456, 33456, 4103, 34106},    /* DDR2-400 SC */
2360         {0, 667, 667, 3428, 33428, 3927, 33927},    /* DDR2-667 SC */
2361         {0, 667, 800, 3443, 33443, 3905, 33905},    /* DDR2-800 SC */
2362
2363         {0, 400, 400, 3528, 33528, 4255, 34255},    /* DDR2-400 SC */
2364         {0, 400, 667, 3500, 33500, 4079, 34079},    /* DDR2-667 SC */
2365         {0, 400, 800, 3487, 33487, 4029, 34029},    /* DDR2-800 SC */
2366 };
2367
2368 static struct cxsr_latency *intel_get_cxsr_latency(int is_desktop, int fsb,
2369                                                    int mem)
2370 {
2371         int i;
2372         struct cxsr_latency *latency;
2373
2374         if (fsb == 0 || mem == 0)
2375                 return NULL;
2376
2377         for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
2378                 latency = &cxsr_latency_table[i];
2379                 if (is_desktop == latency->is_desktop &&
2380                     fsb == latency->fsb_freq && mem == latency->mem_freq)
2381                         return latency;
2382         }
2383
2384         DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
2385
2386         return NULL;
2387 }
2388
2389 static void pineview_disable_cxsr(struct drm_device *dev)
2390 {
2391         struct drm_i915_private *dev_priv = dev->dev_private;
2392         u32 reg;
2393
2394         /* deactivate cxsr */
2395         reg = I915_READ(DSPFW3);
2396         reg &= ~(PINEVIEW_SELF_REFRESH_EN);
2397         I915_WRITE(DSPFW3, reg);
2398         DRM_INFO("Big FIFO is disabled\n");
2399 }
2400
2401 static void pineview_enable_cxsr(struct drm_device *dev, unsigned long clock,
2402                                  int pixel_size)
2403 {
2404         struct drm_i915_private *dev_priv = dev->dev_private;
2405         u32 reg;
2406         unsigned long wm;
2407         struct cxsr_latency *latency;
2408
2409         latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->fsb_freq,
2410                 dev_priv->mem_freq);
2411         if (!latency) {
2412                 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
2413                 pineview_disable_cxsr(dev);
2414                 return;
2415         }
2416
2417         /* Display SR */
2418         wm = intel_calculate_wm(clock, &pineview_display_wm, pixel_size,
2419                                 latency->display_sr);
2420         reg = I915_READ(DSPFW1);
2421         reg &= 0x7fffff;
2422         reg |= wm << 23;
2423         I915_WRITE(DSPFW1, reg);
2424         DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
2425
2426         /* cursor SR */
2427         wm = intel_calculate_wm(clock, &pineview_cursor_wm, pixel_size,
2428                                 latency->cursor_sr);
2429         reg = I915_READ(DSPFW3);
2430         reg &= ~(0x3f << 24);
2431         reg |= (wm & 0x3f) << 24;
2432         I915_WRITE(DSPFW3, reg);
2433
2434         /* Display HPLL off SR */
2435         wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
2436                 latency->display_hpll_disable, I915_FIFO_LINE_SIZE);
2437         reg = I915_READ(DSPFW3);
2438         reg &= 0xfffffe00;
2439         reg |= wm & 0x1ff;
2440         I915_WRITE(DSPFW3, reg);
2441
2442         /* cursor HPLL off SR */
2443         wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm, pixel_size,
2444                                 latency->cursor_hpll_disable);
2445         reg = I915_READ(DSPFW3);
2446         reg &= ~(0x3f << 16);
2447         reg |= (wm & 0x3f) << 16;
2448         I915_WRITE(DSPFW3, reg);
2449         DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
2450
2451         /* activate cxsr */
2452         reg = I915_READ(DSPFW3);
2453         reg |= PINEVIEW_SELF_REFRESH_EN;
2454         I915_WRITE(DSPFW3, reg);
2455
2456         DRM_INFO("Big FIFO is enabled\n");
2457
2458         return;
2459 }
2460
2461 /*
2462  * Latency for FIFO fetches is dependent on several factors:
2463  *   - memory configuration (speed, channels)
2464  *   - chipset
2465  *   - current MCH state
2466  * It can be fairly high in some situations, so here we assume a fairly
2467  * pessimal value.  It's a tradeoff between extra memory fetches (if we
2468  * set this value too high, the FIFO will fetch frequently to stay full)
2469  * and power consumption (set it too low to save power and we might see
2470  * FIFO underruns and display "flicker").
2471  *
2472  * A value of 5us seems to be a good balance; safe for very low end
2473  * platforms but not overly aggressive on lower latency configs.
2474  */
2475 static const int latency_ns = 5000;
2476
2477 static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
2478 {
2479         struct drm_i915_private *dev_priv = dev->dev_private;
2480         uint32_t dsparb = I915_READ(DSPARB);
2481         int size;
2482
2483         if (plane == 0)
2484                 size = dsparb & 0x7f;
2485         else
2486                 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) -
2487                         (dsparb & 0x7f);
2488
2489         DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
2490                         plane ? "B" : "A", size);
2491
2492         return size;
2493 }
2494
2495 static int i85x_get_fifo_size(struct drm_device *dev, int plane)
2496 {
2497         struct drm_i915_private *dev_priv = dev->dev_private;
2498         uint32_t dsparb = I915_READ(DSPARB);
2499         int size;
2500
2501         if (plane == 0)
2502                 size = dsparb & 0x1ff;
2503         else
2504                 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) -
2505                         (dsparb & 0x1ff);
2506         size >>= 1; /* Convert to cachelines */
2507
2508         DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
2509                         plane ? "B" : "A", size);
2510
2511         return size;
2512 }
2513
2514 static int i845_get_fifo_size(struct drm_device *dev, int plane)
2515 {
2516         struct drm_i915_private *dev_priv = dev->dev_private;
2517         uint32_t dsparb = I915_READ(DSPARB);
2518         int size;
2519
2520         size = dsparb & 0x7f;
2521         size >>= 2; /* Convert to cachelines */
2522
2523         DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
2524                         plane ? "B" : "A",
2525                   size);
2526
2527         return size;
2528 }
2529
2530 static int i830_get_fifo_size(struct drm_device *dev, int plane)
2531 {
2532         struct drm_i915_private *dev_priv = dev->dev_private;
2533         uint32_t dsparb = I915_READ(DSPARB);
2534         int size;
2535
2536         size = dsparb & 0x7f;
2537         size >>= 1; /* Convert to cachelines */
2538
2539         DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
2540                         plane ? "B" : "A", size);
2541
2542         return size;
2543 }
2544
2545 static void g4x_update_wm(struct drm_device *dev,  int planea_clock,
2546                           int planeb_clock, int sr_hdisplay, int pixel_size)
2547 {
2548         struct drm_i915_private *dev_priv = dev->dev_private;
2549         int total_size, cacheline_size;
2550         int planea_wm, planeb_wm, cursora_wm, cursorb_wm, cursor_sr;
2551         struct intel_watermark_params planea_params, planeb_params;
2552         unsigned long line_time_us;
2553         int sr_clock, sr_entries = 0, entries_required;
2554
2555         /* Create copies of the base settings for each pipe */
2556         planea_params = planeb_params = g4x_wm_info;
2557
2558         /* Grab a couple of global values before we overwrite them */
2559         total_size = planea_params.fifo_size;
2560         cacheline_size = planea_params.cacheline_size;
2561
2562         /*
2563          * Note: we need to make sure we don't overflow for various clock &
2564          * latency values.
2565          * clocks go from a few thousand to several hundred thousand.
2566          * latency is usually a few thousand
2567          */
2568         entries_required = ((planea_clock / 1000) * pixel_size * latency_ns) /
2569                 1000;
2570         entries_required /= G4X_FIFO_LINE_SIZE;
2571         planea_wm = entries_required + planea_params.guard_size;
2572
2573         entries_required = ((planeb_clock / 1000) * pixel_size * latency_ns) /
2574                 1000;
2575         entries_required /= G4X_FIFO_LINE_SIZE;
2576         planeb_wm = entries_required + planeb_params.guard_size;
2577
2578         cursora_wm = cursorb_wm = 16;
2579         cursor_sr = 32;
2580
2581         DRM_DEBUG("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
2582
2583         /* Calc sr entries for one plane configs */
2584         if (sr_hdisplay && (!planea_clock || !planeb_clock)) {
2585                 /* self-refresh has much higher latency */
2586                 static const int sr_latency_ns = 12000;
2587
2588                 sr_clock = planea_clock ? planea_clock : planeb_clock;
2589                 line_time_us = ((sr_hdisplay * 1000) / sr_clock);
2590
2591                 /* Use ns/us then divide to preserve precision */
2592                 sr_entries = (((sr_latency_ns / line_time_us) + 1) *
2593                               pixel_size * sr_hdisplay) / 1000;
2594                 sr_entries = roundup(sr_entries / cacheline_size, 1);
2595                 DRM_DEBUG("self-refresh entries: %d\n", sr_entries);
2596                 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
2597         }
2598
2599         DRM_DEBUG("Setting FIFO watermarks - A: %d, B: %d, SR %d\n",
2600                   planea_wm, planeb_wm, sr_entries);
2601
2602         planea_wm &= 0x3f;
2603         planeb_wm &= 0x3f;
2604
2605         I915_WRITE(DSPFW1, (sr_entries << DSPFW_SR_SHIFT) |
2606                    (cursorb_wm << DSPFW_CURSORB_SHIFT) |
2607                    (planeb_wm << DSPFW_PLANEB_SHIFT) | planea_wm);
2608         I915_WRITE(DSPFW2, (I915_READ(DSPFW2) & DSPFW_CURSORA_MASK) |
2609                    (cursora_wm << DSPFW_CURSORA_SHIFT));
2610         /* HPLL off in SR has some issues on G4x... disable it */
2611         I915_WRITE(DSPFW3, (I915_READ(DSPFW3) & ~DSPFW_HPLL_SR_EN) |
2612                    (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
2613 }
2614
2615 static void i965_update_wm(struct drm_device *dev, int planea_clock,
2616                            int planeb_clock, int sr_hdisplay, int pixel_size)
2617 {
2618         struct drm_i915_private *dev_priv = dev->dev_private;
2619         unsigned long line_time_us;
2620         int sr_clock, sr_entries, srwm = 1;
2621
2622         /* Calc sr entries for one plane configs */
2623         if (sr_hdisplay && (!planea_clock || !planeb_clock)) {
2624                 /* self-refresh has much higher latency */
2625                 static const int sr_latency_ns = 12000;
2626
2627                 sr_clock = planea_clock ? planea_clock : planeb_clock;
2628                 line_time_us = ((sr_hdisplay * 1000) / sr_clock);
2629
2630                 /* Use ns/us then divide to preserve precision */
2631                 sr_entries = (((sr_latency_ns / line_time_us) + 1) *
2632                               pixel_size * sr_hdisplay) / 1000;
2633                 sr_entries = roundup(sr_entries / I915_FIFO_LINE_SIZE, 1);
2634                 DRM_DEBUG("self-refresh entries: %d\n", sr_entries);
2635                 srwm = I945_FIFO_SIZE - sr_entries;
2636                 if (srwm < 0)
2637                         srwm = 1;
2638                 srwm &= 0x3f;
2639                 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
2640         }
2641
2642         DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
2643                       srwm);
2644
2645         /* 965 has limitations... */
2646         I915_WRITE(DSPFW1, (srwm << DSPFW_SR_SHIFT) | (8 << 16) | (8 << 8) |
2647                    (8 << 0));
2648         I915_WRITE(DSPFW2, (8 << 8) | (8 << 0));
2649 }
2650
2651 static void i9xx_update_wm(struct drm_device *dev, int planea_clock,
2652                            int planeb_clock, int sr_hdisplay, int pixel_size)
2653 {
2654         struct drm_i915_private *dev_priv = dev->dev_private;
2655         uint32_t fwater_lo;
2656         uint32_t fwater_hi;
2657         int total_size, cacheline_size, cwm, srwm = 1;
2658         int planea_wm, planeb_wm;
2659         struct intel_watermark_params planea_params, planeb_params;
2660         unsigned long line_time_us;
2661         int sr_clock, sr_entries = 0;
2662
2663         /* Create copies of the base settings for each pipe */
2664         if (IS_I965GM(dev) || IS_I945GM(dev))
2665                 planea_params = planeb_params = i945_wm_info;
2666         else if (IS_I9XX(dev))
2667                 planea_params = planeb_params = i915_wm_info;
2668         else
2669                 planea_params = planeb_params = i855_wm_info;
2670
2671         /* Grab a couple of global values before we overwrite them */
2672         total_size = planea_params.fifo_size;
2673         cacheline_size = planea_params.cacheline_size;
2674
2675         /* Update per-plane FIFO sizes */
2676         planea_params.fifo_size = dev_priv->display.get_fifo_size(dev, 0);
2677         planeb_params.fifo_size = dev_priv->display.get_fifo_size(dev, 1);
2678
2679         planea_wm = intel_calculate_wm(planea_clock, &planea_params,
2680                                        pixel_size, latency_ns);
2681         planeb_wm = intel_calculate_wm(planeb_clock, &planeb_params,
2682                                        pixel_size, latency_ns);
2683         DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
2684
2685         /*
2686          * Overlay gets an aggressive default since video jitter is bad.
2687          */
2688         cwm = 2;
2689
2690         /* Calc sr entries for one plane configs */
2691         if (HAS_FW_BLC(dev) && sr_hdisplay &&
2692             (!planea_clock || !planeb_clock)) {
2693                 /* self-refresh has much higher latency */
2694                 static const int sr_latency_ns = 6000;
2695
2696                 sr_clock = planea_clock ? planea_clock : planeb_clock;
2697                 line_time_us = ((sr_hdisplay * 1000) / sr_clock);
2698
2699                 /* Use ns/us then divide to preserve precision */
2700                 sr_entries = (((sr_latency_ns / line_time_us) + 1) *
2701                               pixel_size * sr_hdisplay) / 1000;
2702                 sr_entries = roundup(sr_entries / cacheline_size, 1);
2703                 DRM_DEBUG_KMS("self-refresh entries: %d\n", sr_entries);
2704                 srwm = total_size - sr_entries;
2705                 if (srwm < 0)
2706                         srwm = 1;
2707                 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN | (srwm & 0x3f));
2708         }
2709
2710         DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
2711                   planea_wm, planeb_wm, cwm, srwm);
2712
2713         fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
2714         fwater_hi = (cwm & 0x1f);
2715
2716         /* Set request length to 8 cachelines per fetch */
2717         fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
2718         fwater_hi = fwater_hi | (1 << 8);
2719
2720         I915_WRITE(FW_BLC, fwater_lo);
2721         I915_WRITE(FW_BLC2, fwater_hi);
2722 }
2723
2724 static void i830_update_wm(struct drm_device *dev, int planea_clock, int unused,
2725                            int unused2, int pixel_size)
2726 {
2727         struct drm_i915_private *dev_priv = dev->dev_private;
2728         uint32_t fwater_lo = I915_READ(FW_BLC) & ~0xfff;
2729         int planea_wm;
2730
2731         i830_wm_info.fifo_size = dev_priv->display.get_fifo_size(dev, 0);
2732
2733         planea_wm = intel_calculate_wm(planea_clock, &i830_wm_info,
2734                                        pixel_size, latency_ns);
2735         fwater_lo |= (3<<8) | planea_wm;
2736
2737         DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
2738
2739         I915_WRITE(FW_BLC, fwater_lo);
2740 }
2741
2742 /**
2743  * intel_update_watermarks - update FIFO watermark values based on current modes
2744  *
2745  * Calculate watermark values for the various WM regs based on current mode
2746  * and plane configuration.
2747  *
2748  * There are several cases to deal with here:
2749  *   - normal (i.e. non-self-refresh)
2750  *   - self-refresh (SR) mode
2751  *   - lines are large relative to FIFO size (buffer can hold up to 2)
2752  *   - lines are small relative to FIFO size (buffer can hold more than 2
2753  *     lines), so need to account for TLB latency
2754  *
2755  *   The normal calculation is:
2756  *     watermark = dotclock * bytes per pixel * latency
2757  *   where latency is platform & configuration dependent (we assume pessimal
2758  *   values here).
2759  *
2760  *   The SR calculation is:
2761  *     watermark = (trunc(latency/line time)+1) * surface width *
2762  *       bytes per pixel
2763  *   where
2764  *     line time = htotal / dotclock
2765  *   and latency is assumed to be high, as above.
2766  *
2767  * The final value programmed to the register should always be rounded up,
2768  * and include an extra 2 entries to account for clock crossings.
2769  *
2770  * We don't use the sprite, so we can ignore that.  And on Crestline we have
2771  * to set the non-SR watermarks to 8.
2772   */
2773 static void intel_update_watermarks(struct drm_device *dev)
2774 {
2775         struct drm_i915_private *dev_priv = dev->dev_private;
2776         struct drm_crtc *crtc;
2777         struct intel_crtc *intel_crtc;
2778         int sr_hdisplay = 0;
2779         unsigned long planea_clock = 0, planeb_clock = 0, sr_clock = 0;
2780         int enabled = 0, pixel_size = 0;
2781
2782         if (!dev_priv->display.update_wm)
2783                 return;
2784
2785         /* Get the clock config from both planes */
2786         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2787                 intel_crtc = to_intel_crtc(crtc);
2788                 if (crtc->enabled) {
2789                         enabled++;
2790                         if (intel_crtc->plane == 0) {
2791                                 DRM_DEBUG_KMS("plane A (pipe %d) clock: %d\n",
2792                                           intel_crtc->pipe, crtc->mode.clock);
2793                                 planea_clock = crtc->mode.clock;
2794                         } else {
2795                                 DRM_DEBUG_KMS("plane B (pipe %d) clock: %d\n",
2796                                           intel_crtc->pipe, crtc->mode.clock);
2797                                 planeb_clock = crtc->mode.clock;
2798                         }
2799                         sr_hdisplay = crtc->mode.hdisplay;
2800                         sr_clock = crtc->mode.clock;
2801                         if (crtc->fb)
2802                                 pixel_size = crtc->fb->bits_per_pixel / 8;
2803                         else
2804                                 pixel_size = 4; /* by default */
2805                 }
2806         }
2807
2808         if (enabled <= 0)
2809                 return;
2810
2811         /* Single plane configs can enable self refresh */
2812         if (enabled == 1 && IS_PINEVIEW(dev))
2813                 pineview_enable_cxsr(dev, sr_clock, pixel_size);
2814         else if (IS_PINEVIEW(dev))
2815                 pineview_disable_cxsr(dev);
2816
2817         dev_priv->display.update_wm(dev, planea_clock, planeb_clock,
2818                                     sr_hdisplay, pixel_size);
2819 }
2820
2821 static int intel_crtc_mode_set(struct drm_crtc *crtc,
2822                                struct drm_display_mode *mode,
2823                                struct drm_display_mode *adjusted_mode,
2824                                int x, int y,
2825                                struct drm_framebuffer *old_fb)
2826 {
2827         struct drm_device *dev = crtc->dev;
2828         struct drm_i915_private *dev_priv = dev->dev_private;
2829         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2830         int pipe = intel_crtc->pipe;
2831         int plane = intel_crtc->plane;
2832         int fp_reg = (pipe == 0) ? FPA0 : FPB0;
2833         int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
2834         int dpll_md_reg = (intel_crtc->pipe == 0) ? DPLL_A_MD : DPLL_B_MD;
2835         int dspcntr_reg = (plane == 0) ? DSPACNTR : DSPBCNTR;
2836         int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
2837         int htot_reg = (pipe == 0) ? HTOTAL_A : HTOTAL_B;
2838         int hblank_reg = (pipe == 0) ? HBLANK_A : HBLANK_B;
2839         int hsync_reg = (pipe == 0) ? HSYNC_A : HSYNC_B;
2840         int vtot_reg = (pipe == 0) ? VTOTAL_A : VTOTAL_B;
2841         int vblank_reg = (pipe == 0) ? VBLANK_A : VBLANK_B;
2842         int vsync_reg = (pipe == 0) ? VSYNC_A : VSYNC_B;
2843         int dspsize_reg = (plane == 0) ? DSPASIZE : DSPBSIZE;
2844         int dsppos_reg = (plane == 0) ? DSPAPOS : DSPBPOS;
2845         int pipesrc_reg = (pipe == 0) ? PIPEASRC : PIPEBSRC;
2846         int refclk, num_outputs = 0;
2847         intel_clock_t clock, reduced_clock;
2848         u32 dpll = 0, fp = 0, fp2 = 0, dspcntr, pipeconf;
2849         bool ok, has_reduced_clock = false, is_sdvo = false, is_dvo = false;
2850         bool is_crt = false, is_lvds = false, is_tv = false, is_dp = false;
2851         bool is_edp = false;
2852         struct drm_mode_config *mode_config = &dev->mode_config;
2853         struct drm_connector *connector;
2854         const intel_limit_t *limit;
2855         int ret;
2856         struct fdi_m_n m_n = {0};
2857         int data_m1_reg = (pipe == 0) ? PIPEA_DATA_M1 : PIPEB_DATA_M1;
2858         int data_n1_reg = (pipe == 0) ? PIPEA_DATA_N1 : PIPEB_DATA_N1;
2859         int link_m1_reg = (pipe == 0) ? PIPEA_LINK_M1 : PIPEB_LINK_M1;
2860         int link_n1_reg = (pipe == 0) ? PIPEA_LINK_N1 : PIPEB_LINK_N1;
2861         int pch_fp_reg = (pipe == 0) ? PCH_FPA0 : PCH_FPB0;
2862         int pch_dpll_reg = (pipe == 0) ? PCH_DPLL_A : PCH_DPLL_B;
2863         int fdi_rx_reg = (pipe == 0) ? FDI_RXA_CTL : FDI_RXB_CTL;
2864         int lvds_reg = LVDS;
2865         u32 temp;
2866         int sdvo_pixel_multiply;
2867         int target_clock;
2868
2869         drm_vblank_pre_modeset(dev, pipe);
2870
2871         list_for_each_entry(connector, &mode_config->connector_list, head) {
2872                 struct intel_output *intel_output = to_intel_output(connector);
2873
2874                 if (!connector->encoder || connector->encoder->crtc != crtc)
2875                         continue;
2876
2877                 switch (intel_output->type) {
2878                 case INTEL_OUTPUT_LVDS:
2879                         is_lvds = true;
2880                         break;
2881                 case INTEL_OUTPUT_SDVO:
2882                 case INTEL_OUTPUT_HDMI:
2883                         is_sdvo = true;
2884                         if (intel_output->needs_tv_clock)
2885                                 is_tv = true;
2886                         break;
2887                 case INTEL_OUTPUT_DVO:
2888                         is_dvo = true;
2889                         break;
2890                 case INTEL_OUTPUT_TVOUT:
2891                         is_tv = true;
2892                         break;
2893                 case INTEL_OUTPUT_ANALOG:
2894                         is_crt = true;
2895                         break;
2896                 case INTEL_OUTPUT_DISPLAYPORT:
2897                         is_dp = true;
2898                         break;
2899                 case INTEL_OUTPUT_EDP:
2900                         is_edp = true;
2901                         break;
2902                 }
2903
2904                 num_outputs++;
2905         }
2906
2907         if (is_lvds && dev_priv->lvds_use_ssc && num_outputs < 2) {
2908                 refclk = dev_priv->lvds_ssc_freq * 1000;
2909                 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
2910                                         refclk / 1000);
2911         } else if (IS_I9XX(dev)) {
2912                 refclk = 96000;
2913                 if (IS_IRONLAKE(dev))
2914                         refclk = 120000; /* 120Mhz refclk */
2915         } else {
2916                 refclk = 48000;
2917         }
2918         
2919
2920         /*
2921          * Returns a set of divisors for the desired target clock with the given
2922          * refclk, or FALSE.  The returned values represent the clock equation:
2923          * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
2924          */
2925         limit = intel_limit(crtc);
2926         ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, &clock);
2927         if (!ok) {
2928                 DRM_ERROR("Couldn't find PLL settings for mode!\n");
2929                 drm_vblank_post_modeset(dev, pipe);
2930                 return -EINVAL;
2931         }
2932
2933         if (is_lvds && limit->find_reduced_pll &&
2934                         dev_priv->lvds_downclock_avail) {
2935                 memcpy(&reduced_clock, &clock, sizeof(intel_clock_t));
2936                 has_reduced_clock = limit->find_reduced_pll(limit, crtc,
2937                                                             dev_priv->lvds_downclock,
2938                                                             refclk,
2939                                                             &reduced_clock);
2940                 if (has_reduced_clock && (clock.p != reduced_clock.p)) {
2941                         /*
2942                          * If the different P is found, it means that we can't
2943                          * switch the display clock by using the FP0/FP1.
2944                          * In such case we will disable the LVDS downclock
2945                          * feature.
2946                          */
2947                         DRM_DEBUG_KMS("Different P is found for "
2948                                                 "LVDS clock/downclock\n");
2949                         has_reduced_clock = 0;
2950                 }
2951         }
2952         /* SDVO TV has fixed PLL values depend on its clock range,
2953            this mirrors vbios setting. */
2954         if (is_sdvo && is_tv) {
2955                 if (adjusted_mode->clock >= 100000
2956                                 && adjusted_mode->clock < 140500) {
2957                         clock.p1 = 2;
2958                         clock.p2 = 10;
2959                         clock.n = 3;
2960                         clock.m1 = 16;
2961                         clock.m2 = 8;
2962                 } else if (adjusted_mode->clock >= 140500
2963                                 && adjusted_mode->clock <= 200000) {
2964                         clock.p1 = 1;
2965                         clock.p2 = 10;
2966                         clock.n = 6;
2967                         clock.m1 = 12;
2968                         clock.m2 = 8;
2969                 }
2970         }
2971
2972         /* FDI link */
2973         if (IS_IRONLAKE(dev)) {
2974                 int lane, link_bw, bpp;
2975                 /* eDP doesn't require FDI link, so just set DP M/N
2976                    according to current link config */
2977                 if (is_edp) {
2978                         struct drm_connector *edp;
2979                         target_clock = mode->clock;
2980                         edp = intel_pipe_get_output(crtc);
2981                         intel_edp_link_config(to_intel_output(edp),
2982                                         &lane, &link_bw);
2983                 } else {
2984                         /* DP over FDI requires target mode clock
2985                            instead of link clock */
2986                         if (is_dp)
2987                                 target_clock = mode->clock;
2988                         else
2989                                 target_clock = adjusted_mode->clock;
2990                         lane = 4;
2991                         link_bw = 270000;
2992                 }
2993
2994                 /* determine panel color depth */
2995                 temp = I915_READ(pipeconf_reg);
2996                 temp &= ~PIPE_BPC_MASK;
2997                 if (is_lvds) {
2998                         int lvds_reg = I915_READ(PCH_LVDS);
2999                         /* the BPC will be 6 if it is 18-bit LVDS panel */
3000                         if ((lvds_reg & LVDS_A3_POWER_MASK) == LVDS_A3_POWER_UP)
3001                                 temp |= PIPE_8BPC;
3002                         else
3003                                 temp |= PIPE_6BPC;
3004                 } else
3005                         temp |= PIPE_8BPC;
3006                 I915_WRITE(pipeconf_reg, temp);
3007                 I915_READ(pipeconf_reg);
3008
3009                 switch (temp & PIPE_BPC_MASK) {
3010                 case PIPE_8BPC:
3011                         bpp = 24;
3012                         break;
3013                 case PIPE_10BPC:
3014                         bpp = 30;
3015                         break;
3016                 case PIPE_6BPC:
3017                         bpp = 18;
3018                         break;
3019                 case PIPE_12BPC:
3020                         bpp = 36;
3021                         break;
3022                 default:
3023                         DRM_ERROR("unknown pipe bpc value\n");
3024                         bpp = 24;
3025                 }
3026
3027                 ironlake_compute_m_n(bpp, lane, target_clock, link_bw, &m_n);
3028         }
3029
3030         /* Ironlake: try to setup display ref clock before DPLL
3031          * enabling. This is only under driver's control after
3032          * PCH B stepping, previous chipset stepping should be
3033          * ignoring this setting.
3034          */
3035         if (IS_IRONLAKE(dev)) {
3036                 temp = I915_READ(PCH_DREF_CONTROL);
3037                 /* Always enable nonspread source */
3038                 temp &= ~DREF_NONSPREAD_SOURCE_MASK;
3039                 temp |= DREF_NONSPREAD_SOURCE_ENABLE;
3040                 I915_WRITE(PCH_DREF_CONTROL, temp);
3041                 POSTING_READ(PCH_DREF_CONTROL);
3042
3043                 temp &= ~DREF_SSC_SOURCE_MASK;
3044                 temp |= DREF_SSC_SOURCE_ENABLE;
3045                 I915_WRITE(PCH_DREF_CONTROL, temp);
3046                 POSTING_READ(PCH_DREF_CONTROL);
3047
3048                 udelay(200);
3049
3050                 if (is_edp) {
3051                         if (dev_priv->lvds_use_ssc) {
3052                                 temp |= DREF_SSC1_ENABLE;
3053                                 I915_WRITE(PCH_DREF_CONTROL, temp);
3054                                 POSTING_READ(PCH_DREF_CONTROL);
3055
3056                                 udelay(200);
3057
3058                                 temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
3059                                 temp |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
3060                                 I915_WRITE(PCH_DREF_CONTROL, temp);
3061                                 POSTING_READ(PCH_DREF_CONTROL);
3062                         } else {
3063                                 temp |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
3064                                 I915_WRITE(PCH_DREF_CONTROL, temp);
3065                                 POSTING_READ(PCH_DREF_CONTROL);
3066                         }
3067                 }
3068         }
3069
3070         if (IS_PINEVIEW(dev)) {
3071                 fp = (1 << clock.n) << 16 | clock.m1 << 8 | clock.m2;
3072                 if (has_reduced_clock)
3073                         fp2 = (1 << reduced_clock.n) << 16 |
3074                                 reduced_clock.m1 << 8 | reduced_clock.m2;
3075         } else {
3076                 fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
3077                 if (has_reduced_clock)
3078                         fp2 = reduced_clock.n << 16 | reduced_clock.m1 << 8 |
3079                                 reduced_clock.m2;
3080         }
3081
3082         if (!IS_IRONLAKE(dev))
3083                 dpll = DPLL_VGA_MODE_DIS;
3084
3085         if (IS_I9XX(dev)) {
3086                 if (is_lvds)
3087                         dpll |= DPLLB_MODE_LVDS;
3088                 else
3089                         dpll |= DPLLB_MODE_DAC_SERIAL;
3090                 if (is_sdvo) {
3091                         dpll |= DPLL_DVO_HIGH_SPEED;
3092                         sdvo_pixel_multiply = adjusted_mode->clock / mode->clock;
3093                         if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
3094                                 dpll |= (sdvo_pixel_multiply - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
3095                         else if (IS_IRONLAKE(dev))
3096                                 dpll |= (sdvo_pixel_multiply - 1) << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
3097                 }
3098                 if (is_dp)
3099                         dpll |= DPLL_DVO_HIGH_SPEED;
3100
3101                 /* compute bitmask from p1 value */
3102                 if (IS_PINEVIEW(dev))
3103                         dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
3104                 else {
3105                         dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
3106                         /* also FPA1 */
3107                         if (IS_IRONLAKE(dev))
3108                                 dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
3109                         if (IS_G4X(dev) && has_reduced_clock)
3110                                 dpll |= (1 << (reduced_clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
3111                 }
3112                 switch (clock.p2) {
3113                 case 5:
3114                         dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
3115                         break;
3116                 case 7:
3117                         dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
3118                         break;
3119                 case 10:
3120                         dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
3121                         break;
3122                 case 14:
3123                         dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
3124                         break;
3125                 }
3126                 if (IS_I965G(dev) && !IS_IRONLAKE(dev))
3127                         dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
3128         } else {
3129                 if (is_lvds) {
3130                         dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
3131                 } else {
3132                         if (clock.p1 == 2)
3133                                 dpll |= PLL_P1_DIVIDE_BY_TWO;
3134                         else
3135                                 dpll |= (clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
3136                         if (clock.p2 == 4)
3137                                 dpll |= PLL_P2_DIVIDE_BY_4;
3138                 }
3139         }
3140
3141         if (is_sdvo && is_tv)
3142                 dpll |= PLL_REF_INPUT_TVCLKINBC;
3143         else if (is_tv)
3144                 /* XXX: just matching BIOS for now */
3145                 /*      dpll |= PLL_REF_INPUT_TVCLKINBC; */
3146                 dpll |= 3;
3147         else if (is_lvds && dev_priv->lvds_use_ssc && num_outputs < 2)
3148                 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
3149         else
3150                 dpll |= PLL_REF_INPUT_DREFCLK;
3151
3152         /* setup pipeconf */
3153         pipeconf = I915_READ(pipeconf_reg);
3154
3155         /* Set up the display plane register */
3156         dspcntr = DISPPLANE_GAMMA_ENABLE;
3157
3158         /* Ironlake's plane is forced to pipe, bit 24 is to
3159            enable color space conversion */
3160         if (!IS_IRONLAKE(dev)) {
3161                 if (pipe == 0)
3162                         dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
3163                 else
3164                         dspcntr |= DISPPLANE_SEL_PIPE_B;
3165         }
3166
3167         if (pipe == 0 && !IS_I965G(dev)) {
3168                 /* Enable pixel doubling when the dot clock is > 90% of the (display)
3169                  * core speed.
3170                  *
3171                  * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
3172                  * pipe == 0 check?
3173                  */
3174                 if (mode->clock >
3175                     dev_priv->display.get_display_clock_speed(dev) * 9 / 10)
3176                         pipeconf |= PIPEACONF_DOUBLE_WIDE;
3177                 else
3178                         pipeconf &= ~PIPEACONF_DOUBLE_WIDE;
3179         }
3180
3181         dspcntr |= DISPLAY_PLANE_ENABLE;
3182         pipeconf |= PIPEACONF_ENABLE;
3183         dpll |= DPLL_VCO_ENABLE;
3184
3185
3186         /* Disable the panel fitter if it was on our pipe */
3187         if (!IS_IRONLAKE(dev) && intel_panel_fitter_pipe(dev) == pipe)
3188                 I915_WRITE(PFIT_CONTROL, 0);
3189
3190         DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
3191         drm_mode_debug_printmodeline(mode);
3192
3193         /* assign to Ironlake registers */
3194         if (IS_IRONLAKE(dev)) {
3195                 fp_reg = pch_fp_reg;
3196                 dpll_reg = pch_dpll_reg;
3197         }
3198
3199         if (is_edp) {
3200                 ironlake_disable_pll_edp(crtc);
3201         } else if ((dpll & DPLL_VCO_ENABLE)) {
3202                 I915_WRITE(fp_reg, fp);
3203                 I915_WRITE(dpll_reg, dpll & ~DPLL_VCO_ENABLE);
3204                 I915_READ(dpll_reg);
3205                 udelay(150);
3206         }
3207
3208         /* The LVDS pin pair needs to be on before the DPLLs are enabled.
3209          * This is an exception to the general rule that mode_set doesn't turn
3210          * things on.
3211          */
3212         if (is_lvds) {
3213                 u32 lvds;
3214
3215                 if (IS_IRONLAKE(dev))
3216                         lvds_reg = PCH_LVDS;
3217
3218                 lvds = I915_READ(lvds_reg);
3219                 lvds |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP | LVDS_PIPEB_SELECT;
3220                 /* set the corresponsding LVDS_BORDER bit */
3221                 lvds |= dev_priv->lvds_border_bits;
3222                 /* Set the B0-B3 data pairs corresponding to whether we're going to
3223                  * set the DPLLs for dual-channel mode or not.
3224                  */
3225                 if (clock.p2 == 7)
3226                         lvds |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
3227                 else
3228                         lvds &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
3229
3230                 /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
3231                  * appropriately here, but we need to look more thoroughly into how
3232                  * panels behave in the two modes.
3233                  */
3234                 /* set the dithering flag */
3235                 if (IS_I965G(dev)) {
3236                         if (dev_priv->lvds_dither) {
3237                                 if (IS_IRONLAKE(dev))
3238                                         pipeconf |= PIPE_ENABLE_DITHER;
3239                                 else
3240                                         lvds |= LVDS_ENABLE_DITHER;
3241                         } else {
3242                                 if (IS_IRONLAKE(dev))
3243                                         pipeconf &= ~PIPE_ENABLE_DITHER;
3244                                 else
3245                                         lvds &= ~LVDS_ENABLE_DITHER;
3246                         }
3247                 }
3248                 I915_WRITE(lvds_reg, lvds);
3249                 I915_READ(lvds_reg);
3250         }
3251         if (is_dp)
3252                 intel_dp_set_m_n(crtc, mode, adjusted_mode);
3253
3254         if (!is_edp) {
3255                 I915_WRITE(fp_reg, fp);
3256                 I915_WRITE(dpll_reg, dpll);
3257                 I915_READ(dpll_reg);
3258                 /* Wait for the clocks to stabilize. */
3259                 udelay(150);
3260
3261                 if (IS_I965G(dev) && !IS_IRONLAKE(dev)) {
3262                         if (is_sdvo) {
3263                                 sdvo_pixel_multiply = adjusted_mode->clock / mode->clock;
3264                                 I915_WRITE(dpll_md_reg, (0 << DPLL_MD_UDI_DIVIDER_SHIFT) |
3265                                         ((sdvo_pixel_multiply - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT));
3266                         } else
3267                                 I915_WRITE(dpll_md_reg, 0);
3268                 } else {
3269                         /* write it again -- the BIOS does, after all */
3270                         I915_WRITE(dpll_reg, dpll);
3271                 }
3272                 I915_READ(dpll_reg);
3273                 /* Wait for the clocks to stabilize. */
3274                 udelay(150);
3275         }
3276
3277         if (is_lvds && has_reduced_clock && i915_powersave) {
3278                 I915_WRITE(fp_reg + 4, fp2);
3279                 intel_crtc->lowfreq_avail = true;
3280                 if (HAS_PIPE_CXSR(dev)) {
3281                         DRM_DEBUG_KMS("enabling CxSR downclocking\n");
3282                         pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
3283                 }
3284         } else {
3285                 I915_WRITE(fp_reg + 4, fp);
3286                 intel_crtc->lowfreq_avail = false;
3287                 if (HAS_PIPE_CXSR(dev)) {
3288                         DRM_DEBUG_KMS("disabling CxSR downclocking\n");
3289                         pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
3290                 }
3291         }
3292
3293         I915_WRITE(htot_reg, (adjusted_mode->crtc_hdisplay - 1) |
3294                    ((adjusted_mode->crtc_htotal - 1) << 16));
3295         I915_WRITE(hblank_reg, (adjusted_mode->crtc_hblank_start - 1) |
3296                    ((adjusted_mode->crtc_hblank_end - 1) << 16));
3297         I915_WRITE(hsync_reg, (adjusted_mode->crtc_hsync_start - 1) |
3298                    ((adjusted_mode->crtc_hsync_end - 1) << 16));
3299         I915_WRITE(vtot_reg, (adjusted_mode->crtc_vdisplay - 1) |
3300                    ((adjusted_mode->crtc_vtotal - 1) << 16));
3301         I915_WRITE(vblank_reg, (adjusted_mode->crtc_vblank_start - 1) |
3302                    ((adjusted_mode->crtc_vblank_end - 1) << 16));
3303         I915_WRITE(vsync_reg, (adjusted_mode->crtc_vsync_start - 1) |
3304                    ((adjusted_mode->crtc_vsync_end - 1) << 16));
3305         /* pipesrc and dspsize control the size that is scaled from, which should
3306          * always be the user's requested size.
3307          */
3308         if (!IS_IRONLAKE(dev)) {
3309                 I915_WRITE(dspsize_reg, ((mode->vdisplay - 1) << 16) |
3310                                 (mode->hdisplay - 1));
3311                 I915_WRITE(dsppos_reg, 0);
3312         }
3313         I915_WRITE(pipesrc_reg, ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
3314
3315         if (IS_IRONLAKE(dev)) {
3316                 I915_WRITE(data_m1_reg, TU_SIZE(m_n.tu) | m_n.gmch_m);
3317                 I915_WRITE(data_n1_reg, TU_SIZE(m_n.tu) | m_n.gmch_n);
3318                 I915_WRITE(link_m1_reg, m_n.link_m);
3319                 I915_WRITE(link_n1_reg, m_n.link_n);
3320
3321                 if (is_edp) {
3322                         ironlake_set_pll_edp(crtc, adjusted_mode->clock);
3323                 } else {
3324                         /* enable FDI RX PLL too */
3325                         temp = I915_READ(fdi_rx_reg);
3326                         I915_WRITE(fdi_rx_reg, temp | FDI_RX_PLL_ENABLE);
3327                         udelay(200);
3328                 }
3329         }
3330
3331         I915_WRITE(pipeconf_reg, pipeconf);
3332         I915_READ(pipeconf_reg);
3333
3334         intel_wait_for_vblank(dev);
3335
3336         if (IS_IRONLAKE(dev)) {
3337                 /* enable address swizzle for tiling buffer */
3338                 temp = I915_READ(DISP_ARB_CTL);
3339                 I915_WRITE(DISP_ARB_CTL, temp | DISP_TILE_SURFACE_SWIZZLING);
3340         }
3341
3342         I915_WRITE(dspcntr_reg, dspcntr);
3343
3344         /* Flush the plane changes */
3345         ret = intel_pipe_set_base(crtc, x, y, old_fb);
3346
3347         if ((IS_I965G(dev) || plane == 0))
3348                 intel_update_fbc(crtc, &crtc->mode);
3349
3350         intel_update_watermarks(dev);
3351
3352         drm_vblank_post_modeset(dev, pipe);
3353
3354         return ret;
3355 }
3356
3357 /** Loads the palette/gamma unit for the CRTC with the prepared values */
3358 void intel_crtc_load_lut(struct drm_crtc *crtc)
3359 {
3360         struct drm_device *dev = crtc->dev;
3361         struct drm_i915_private *dev_priv = dev->dev_private;
3362         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3363         int palreg = (intel_crtc->pipe == 0) ? PALETTE_A : PALETTE_B;
3364         int i;
3365
3366         /* The clocks have to be on to load the palette. */
3367         if (!crtc->enabled)
3368                 return;
3369
3370         /* use legacy palette for Ironlake */
3371         if (IS_IRONLAKE(dev))
3372                 palreg = (intel_crtc->pipe == 0) ? LGC_PALETTE_A :
3373                                                    LGC_PALETTE_B;
3374
3375         for (i = 0; i < 256; i++) {
3376                 I915_WRITE(palreg + 4 * i,
3377                            (intel_crtc->lut_r[i] << 16) |
3378                            (intel_crtc->lut_g[i] << 8) |
3379                            intel_crtc->lut_b[i]);
3380         }
3381 }
3382
3383 static int intel_crtc_cursor_set(struct drm_crtc *crtc,
3384                                  struct drm_file *file_priv,
3385                                  uint32_t handle,
3386                                  uint32_t width, uint32_t height)
3387 {
3388         struct drm_device *dev = crtc->dev;
3389         struct drm_i915_private *dev_priv = dev->dev_private;
3390         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3391         struct drm_gem_object *bo;
3392         struct drm_i915_gem_object *obj_priv;
3393         int pipe = intel_crtc->pipe;
3394         uint32_t control = (pipe == 0) ? CURACNTR : CURBCNTR;
3395         uint32_t base = (pipe == 0) ? CURABASE : CURBBASE;
3396         uint32_t temp = I915_READ(control);
3397         size_t addr;
3398         int ret;
3399
3400         DRM_DEBUG_KMS("\n");
3401
3402         /* if we want to turn off the cursor ignore width and height */
3403         if (!handle) {
3404                 DRM_DEBUG_KMS("cursor off\n");
3405                 if (IS_MOBILE(dev) || IS_I9XX(dev)) {
3406                         temp &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
3407                         temp |= CURSOR_MODE_DISABLE;
3408                 } else {
3409                         temp &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
3410                 }
3411                 addr = 0;
3412                 bo = NULL;
3413                 mutex_lock(&dev->struct_mutex);
3414                 goto finish;
3415         }
3416
3417         /* Currently we only support 64x64 cursors */
3418         if (width != 64 || height != 64) {
3419                 DRM_ERROR("we currently only support 64x64 cursors\n");
3420                 return -EINVAL;
3421         }
3422
3423         bo = drm_gem_object_lookup(dev, file_priv, handle);
3424         if (!bo)
3425                 return -ENOENT;
3426
3427         obj_priv = bo->driver_private;
3428
3429         if (bo->size < width * height * 4) {
3430                 DRM_ERROR("buffer is to small\n");
3431                 ret = -ENOMEM;
3432                 goto fail;
3433         }
3434
3435         /* we only need to pin inside GTT if cursor is non-phy */
3436         mutex_lock(&dev->struct_mutex);
3437         if (!dev_priv->info->cursor_needs_physical) {
3438                 ret = i915_gem_object_pin(bo, PAGE_SIZE);
3439                 if (ret) {
3440                         DRM_ERROR("failed to pin cursor bo\n");
3441                         goto fail_locked;
3442                 }
3443                 addr = obj_priv->gtt_offset;
3444         } else {
3445                 ret = i915_gem_attach_phys_object(dev, bo, (pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1);
3446                 if (ret) {
3447                         DRM_ERROR("failed to attach phys object\n");
3448                         goto fail_locked;
3449                 }
3450                 addr = obj_priv->phys_obj->handle->busaddr;
3451         }
3452
3453         if (!IS_I9XX(dev))
3454                 I915_WRITE(CURSIZE, (height << 12) | width);
3455
3456         /* Hooray for CUR*CNTR differences */
3457         if (IS_MOBILE(dev) || IS_I9XX(dev)) {
3458                 temp &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
3459                 temp |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
3460                 temp |= (pipe << 28); /* Connect to correct pipe */
3461         } else {
3462                 temp &= ~(CURSOR_FORMAT_MASK);
3463                 temp |= CURSOR_ENABLE;
3464                 temp |= CURSOR_FORMAT_ARGB | CURSOR_GAMMA_ENABLE;
3465         }
3466
3467  finish:
3468         I915_WRITE(control, temp);
3469         I915_WRITE(base, addr);
3470
3471         if (intel_crtc->cursor_bo) {
3472                 if (dev_priv->info->cursor_needs_physical) {
3473                         if (intel_crtc->cursor_bo != bo)
3474                                 i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
3475                 } else
3476                         i915_gem_object_unpin(intel_crtc->cursor_bo);
3477                 drm_gem_object_unreference(intel_crtc->cursor_bo);
3478         }
3479
3480         mutex_unlock(&dev->struct_mutex);
3481
3482         intel_crtc->cursor_addr = addr;
3483         intel_crtc->cursor_bo = bo;
3484
3485         return 0;
3486 fail:
3487         mutex_lock(&dev->struct_mutex);
3488 fail_locked:
3489         drm_gem_object_unreference(bo);
3490         mutex_unlock(&dev->struct_mutex);
3491         return ret;
3492 }
3493
3494 static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
3495 {
3496         struct drm_device *dev = crtc->dev;
3497         struct drm_i915_private *dev_priv = dev->dev_private;
3498         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3499         struct intel_framebuffer *intel_fb;
3500         int pipe = intel_crtc->pipe;
3501         uint32_t temp = 0;
3502         uint32_t adder;
3503
3504         if (crtc->fb) {
3505                 intel_fb = to_intel_framebuffer(crtc->fb);
3506                 intel_mark_busy(dev, intel_fb->obj);
3507         }
3508
3509         if (x < 0) {
3510                 temp |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
3511                 x = -x;
3512         }
3513         if (y < 0) {
3514                 temp |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
3515                 y = -y;
3516         }
3517
3518         temp |= x << CURSOR_X_SHIFT;
3519         temp |= y << CURSOR_Y_SHIFT;
3520
3521         adder = intel_crtc->cursor_addr;
3522         I915_WRITE((pipe == 0) ? CURAPOS : CURBPOS, temp);
3523         I915_WRITE((pipe == 0) ? CURABASE : CURBBASE, adder);
3524
3525         return 0;
3526 }
3527
3528 /** Sets the color ramps on behalf of RandR */
3529 void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
3530                                  u16 blue, int regno)
3531 {
3532         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3533
3534         intel_crtc->lut_r[regno] = red >> 8;
3535         intel_crtc->lut_g[regno] = green >> 8;
3536         intel_crtc->lut_b[regno] = blue >> 8;
3537 }
3538
3539 void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
3540                              u16 *blue, int regno)
3541 {
3542         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3543
3544         *red = intel_crtc->lut_r[regno] << 8;
3545         *green = intel_crtc->lut_g[regno] << 8;
3546         *blue = intel_crtc->lut_b[regno] << 8;
3547 }
3548
3549 static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
3550                                  u16 *blue, uint32_t size)
3551 {
3552         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3553         int i;
3554
3555         if (size != 256)
3556                 return;
3557
3558         for (i = 0; i < 256; i++) {
3559                 intel_crtc->lut_r[i] = red[i] >> 8;
3560                 intel_crtc->lut_g[i] = green[i] >> 8;
3561                 intel_crtc->lut_b[i] = blue[i] >> 8;
3562         }
3563
3564         intel_crtc_load_lut(crtc);
3565 }
3566
3567 /**
3568  * Get a pipe with a simple mode set on it for doing load-based monitor
3569  * detection.
3570  *
3571  * It will be up to the load-detect code to adjust the pipe as appropriate for
3572  * its requirements.  The pipe will be connected to no other outputs.
3573  *
3574  * Currently this code will only succeed if there is a pipe with no outputs
3575  * configured for it.  In the future, it could choose to temporarily disable
3576  * some outputs to free up a pipe for its use.
3577  *
3578  * \return crtc, or NULL if no pipes are available.
3579  */
3580
3581 /* VESA 640x480x72Hz mode to set on the pipe */
3582 static struct drm_display_mode load_detect_mode = {
3583         DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
3584                  704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
3585 };
3586
3587 struct drm_crtc *intel_get_load_detect_pipe(struct intel_output *intel_output,
3588                                             struct drm_display_mode *mode,
3589                                             int *dpms_mode)
3590 {
3591         struct intel_crtc *intel_crtc;
3592         struct drm_crtc *possible_crtc;
3593         struct drm_crtc *supported_crtc =NULL;
3594         struct drm_encoder *encoder = &intel_output->enc;
3595         struct drm_crtc *crtc = NULL;
3596         struct drm_device *dev = encoder->dev;
3597         struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
3598         struct drm_crtc_helper_funcs *crtc_funcs;
3599         int i = -1;
3600
3601         /*
3602          * Algorithm gets a little messy:
3603          *   - if the connector already has an assigned crtc, use it (but make
3604          *     sure it's on first)
3605          *   - try to find the first unused crtc that can drive this connector,
3606          *     and use that if we find one
3607          *   - if there are no unused crtcs available, try to use the first
3608          *     one we found that supports the connector
3609          */
3610
3611         /* See if we already have a CRTC for this connector */
3612         if (encoder->crtc) {
3613                 crtc = encoder->crtc;
3614                 /* Make sure the crtc and connector are running */
3615                 intel_crtc = to_intel_crtc(crtc);
3616                 *dpms_mode = intel_crtc->dpms_mode;
3617                 if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
3618                         crtc_funcs = crtc->helper_private;
3619                         crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
3620                         encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
3621                 }
3622                 return crtc;
3623         }
3624
3625         /* Find an unused one (if possible) */
3626         list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
3627                 i++;
3628                 if (!(encoder->possible_crtcs & (1 << i)))
3629                         continue;
3630                 if (!possible_crtc->enabled) {
3631                         crtc = possible_crtc;
3632                         break;
3633                 }
3634                 if (!supported_crtc)
3635                         supported_crtc = possible_crtc;
3636         }
3637
3638         /*
3639          * If we didn't find an unused CRTC, don't use any.
3640          */
3641         if (!crtc) {
3642                 return NULL;
3643         }
3644
3645         encoder->crtc = crtc;
3646         intel_output->base.encoder = encoder;
3647         intel_output->load_detect_temp = true;
3648
3649         intel_crtc = to_intel_crtc(crtc);
3650         *dpms_mode = intel_crtc->dpms_mode;
3651
3652         if (!crtc->enabled) {
3653                 if (!mode)
3654                         mode = &load_detect_mode;
3655                 drm_crtc_helper_set_mode(crtc, mode, 0, 0, crtc->fb);
3656         } else {
3657                 if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
3658                         crtc_funcs = crtc->helper_private;
3659                         crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
3660                 }
3661
3662                 /* Add this connector to the crtc */
3663                 encoder_funcs->mode_set(encoder, &crtc->mode, &crtc->mode);
3664                 encoder_funcs->commit(encoder);
3665         }
3666         /* let the connector get through one full cycle before testing */
3667         intel_wait_for_vblank(dev);
3668
3669         return crtc;
3670 }
3671
3672 void intel_release_load_detect_pipe(struct intel_output *intel_output, int dpms_mode)
3673 {
3674         struct drm_encoder *encoder = &intel_output->enc;
3675         struct drm_device *dev = encoder->dev;
3676         struct drm_crtc *crtc = encoder->crtc;
3677         struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
3678         struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
3679
3680         if (intel_output->load_detect_temp) {
3681                 encoder->crtc = NULL;
3682                 intel_output->base.encoder = NULL;
3683                 intel_output->load_detect_temp = false;
3684                 crtc->enabled = drm_helper_crtc_in_use(crtc);
3685                 drm_helper_disable_unused_functions(dev);
3686         }
3687
3688         /* Switch crtc and output back off if necessary */
3689         if (crtc->enabled && dpms_mode != DRM_MODE_DPMS_ON) {
3690                 if (encoder->crtc == crtc)
3691                         encoder_funcs->dpms(encoder, dpms_mode);
3692                 crtc_funcs->dpms(crtc, dpms_mode);
3693         }
3694 }
3695
3696 /* Returns the clock of the currently programmed mode of the given pipe. */
3697 static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
3698 {
3699         struct drm_i915_private *dev_priv = dev->dev_private;
3700         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3701         int pipe = intel_crtc->pipe;
3702         u32 dpll = I915_READ((pipe == 0) ? DPLL_A : DPLL_B);
3703         u32 fp;
3704         intel_clock_t clock;
3705
3706         if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
3707                 fp = I915_READ((pipe == 0) ? FPA0 : FPB0);
3708         else
3709                 fp = I915_READ((pipe == 0) ? FPA1 : FPB1);
3710
3711         clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
3712         if (IS_PINEVIEW(dev)) {
3713                 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
3714                 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
3715         } else {
3716                 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
3717                 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
3718         }
3719
3720         if (IS_I9XX(dev)) {
3721                 if (IS_PINEVIEW(dev))
3722                         clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
3723                                 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
3724                 else
3725                         clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
3726                                DPLL_FPA01_P1_POST_DIV_SHIFT);
3727
3728                 switch (dpll & DPLL_MODE_MASK) {
3729                 case DPLLB_MODE_DAC_SERIAL:
3730                         clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
3731                                 5 : 10;
3732                         break;
3733                 case DPLLB_MODE_LVDS:
3734                         clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
3735                                 7 : 14;
3736                         break;
3737                 default:
3738                         DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
3739                                   "mode\n", (int)(dpll & DPLL_MODE_MASK));
3740                         return 0;
3741                 }
3742
3743                 /* XXX: Handle the 100Mhz refclk */
3744                 intel_clock(dev, 96000, &clock);
3745         } else {
3746                 bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
3747
3748                 if (is_lvds) {
3749                         clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
3750                                        DPLL_FPA01_P1_POST_DIV_SHIFT);
3751                         clock.p2 = 14;
3752
3753                         if ((dpll & PLL_REF_INPUT_MASK) ==
3754                             PLLB_REF_INPUT_SPREADSPECTRUMIN) {
3755                                 /* XXX: might not be 66MHz */
3756                                 intel_clock(dev, 66000, &clock);
3757                         } else
3758                                 intel_clock(dev, 48000, &clock);
3759                 } else {
3760                         if (dpll & PLL_P1_DIVIDE_BY_TWO)
3761                                 clock.p1 = 2;
3762                         else {
3763                                 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
3764                                             DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
3765                         }
3766                         if (dpll & PLL_P2_DIVIDE_BY_4)
3767                                 clock.p2 = 4;
3768                         else
3769                                 clock.p2 = 2;
3770
3771                         intel_clock(dev, 48000, &clock);
3772                 }
3773         }
3774
3775         /* XXX: It would be nice to validate the clocks, but we can't reuse
3776          * i830PllIsValid() because it relies on the xf86_config connector
3777          * configuration being accurate, which it isn't necessarily.
3778          */
3779
3780         return clock.dot;
3781 }
3782
3783 /** Returns the currently programmed mode of the given pipe. */
3784 struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
3785                                              struct drm_crtc *crtc)
3786 {
3787         struct drm_i915_private *dev_priv = dev->dev_private;
3788         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3789         int pipe = intel_crtc->pipe;
3790         struct drm_display_mode *mode;
3791         int htot = I915_READ((pipe == 0) ? HTOTAL_A : HTOTAL_B);
3792         int hsync = I915_READ((pipe == 0) ? HSYNC_A : HSYNC_B);
3793         int vtot = I915_READ((pipe == 0) ? VTOTAL_A : VTOTAL_B);
3794         int vsync = I915_READ((pipe == 0) ? VSYNC_A : VSYNC_B);
3795
3796         mode = kzalloc(sizeof(*mode), GFP_KERNEL);
3797         if (!mode)
3798                 return NULL;
3799
3800         mode->clock = intel_crtc_clock_get(dev, crtc);
3801         mode->hdisplay = (htot & 0xffff) + 1;
3802         mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
3803         mode->hsync_start = (hsync & 0xffff) + 1;
3804         mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
3805         mode->vdisplay = (vtot & 0xffff) + 1;
3806         mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
3807         mode->vsync_start = (vsync & 0xffff) + 1;
3808         mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
3809
3810         drm_mode_set_name(mode);
3811         drm_mode_set_crtcinfo(mode, 0);
3812
3813         return mode;
3814 }
3815
3816 #define GPU_IDLE_TIMEOUT 500 /* ms */
3817
3818 /* When this timer fires, we've been idle for awhile */
3819 static void intel_gpu_idle_timer(unsigned long arg)
3820 {
3821         struct drm_device *dev = (struct drm_device *)arg;
3822         drm_i915_private_t *dev_priv = dev->dev_private;
3823
3824         DRM_DEBUG_DRIVER("idle timer fired, downclocking\n");
3825
3826         dev_priv->busy = false;
3827
3828         queue_work(dev_priv->wq, &dev_priv->idle_work);
3829 }
3830
3831 #define CRTC_IDLE_TIMEOUT 1000 /* ms */
3832
3833 static void intel_crtc_idle_timer(unsigned long arg)
3834 {
3835         struct intel_crtc *intel_crtc = (struct intel_crtc *)arg;
3836         struct drm_crtc *crtc = &intel_crtc->base;
3837         drm_i915_private_t *dev_priv = crtc->dev->dev_private;
3838
3839         DRM_DEBUG_DRIVER("idle timer fired, downclocking\n");
3840
3841         intel_crtc->busy = false;
3842
3843         queue_work(dev_priv->wq, &dev_priv->idle_work);
3844 }
3845
3846 static void intel_increase_pllclock(struct drm_crtc *crtc, bool schedule)
3847 {
3848         struct drm_device *dev = crtc->dev;
3849         drm_i915_private_t *dev_priv = dev->dev_private;
3850         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3851         int pipe = intel_crtc->pipe;
3852         int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
3853         int dpll = I915_READ(dpll_reg);
3854
3855         if (IS_IRONLAKE(dev))
3856                 return;
3857
3858         if (!dev_priv->lvds_downclock_avail)
3859                 return;
3860
3861         if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
3862                 DRM_DEBUG_DRIVER("upclocking LVDS\n");
3863
3864                 /* Unlock panel regs */
3865                 I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) | (0xabcd << 16));
3866
3867                 dpll &= ~DISPLAY_RATE_SELECT_FPA1;
3868                 I915_WRITE(dpll_reg, dpll);
3869                 dpll = I915_READ(dpll_reg);
3870                 intel_wait_for_vblank(dev);
3871                 dpll = I915_READ(dpll_reg);
3872                 if (dpll & DISPLAY_RATE_SELECT_FPA1)
3873                         DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
3874
3875                 /* ...and lock them again */
3876                 I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) & 0x3);
3877         }
3878
3879         /* Schedule downclock */
3880         if (schedule)
3881                 mod_timer(&intel_crtc->idle_timer, jiffies +
3882                           msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
3883 }
3884
3885 static void intel_decrease_pllclock(struct drm_crtc *crtc)
3886 {
3887         struct drm_device *dev = crtc->dev;
3888         drm_i915_private_t *dev_priv = dev->dev_private;
3889         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3890         int pipe = intel_crtc->pipe;
3891         int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
3892         int dpll = I915_READ(dpll_reg);
3893
3894         if (IS_IRONLAKE(dev))
3895                 return;
3896
3897         if (!dev_priv->lvds_downclock_avail)
3898                 return;
3899
3900         /*
3901          * Since this is called by a timer, we should never get here in
3902          * the manual case.
3903          */
3904         if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
3905                 DRM_DEBUG_DRIVER("downclocking LVDS\n");
3906
3907                 /* Unlock panel regs */
3908                 I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) | (0xabcd << 16));
3909
3910                 dpll |= DISPLAY_RATE_SELECT_FPA1;
3911                 I915_WRITE(dpll_reg, dpll);
3912                 dpll = I915_READ(dpll_reg);
3913                 intel_wait_for_vblank(dev);
3914                 dpll = I915_READ(dpll_reg);
3915                 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
3916                         DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
3917
3918                 /* ...and lock them again */
3919                 I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) & 0x3);
3920         }
3921
3922 }
3923
3924 /**
3925  * intel_idle_update - adjust clocks for idleness
3926  * @work: work struct
3927  *
3928  * Either the GPU or display (or both) went idle.  Check the busy status
3929  * here and adjust the CRTC and GPU clocks as necessary.
3930  */
3931 static void intel_idle_update(struct work_struct *work)
3932 {
3933         drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
3934                                                     idle_work);
3935         struct drm_device *dev = dev_priv->dev;
3936         struct drm_crtc *crtc;
3937         struct intel_crtc *intel_crtc;
3938
3939         if (!i915_powersave)
3940                 return;
3941
3942         mutex_lock(&dev->struct_mutex);
3943
3944         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
3945                 /* Skip inactive CRTCs */
3946                 if (!crtc->fb)
3947                         continue;
3948
3949                 intel_crtc = to_intel_crtc(crtc);
3950                 if (!intel_crtc->busy)
3951                         intel_decrease_pllclock(crtc);
3952         }
3953
3954         mutex_unlock(&dev->struct_mutex);
3955 }
3956
3957 /**
3958  * intel_mark_busy - mark the GPU and possibly the display busy
3959  * @dev: drm device
3960  * @obj: object we're operating on
3961  *
3962  * Callers can use this function to indicate that the GPU is busy processing
3963  * commands.  If @obj matches one of the CRTC objects (i.e. it's a scanout
3964  * buffer), we'll also mark the display as busy, so we know to increase its
3965  * clock frequency.
3966  */
3967 void intel_mark_busy(struct drm_device *dev, struct drm_gem_object *obj)
3968 {
3969         drm_i915_private_t *dev_priv = dev->dev_private;
3970         struct drm_crtc *crtc = NULL;
3971         struct intel_framebuffer *intel_fb;
3972         struct intel_crtc *intel_crtc;
3973
3974         if (!drm_core_check_feature(dev, DRIVER_MODESET))
3975                 return;
3976
3977         if (!dev_priv->busy)
3978                 dev_priv->busy = true;
3979         else
3980                 mod_timer(&dev_priv->idle_timer, jiffies +
3981                           msecs_to_jiffies(GPU_IDLE_TIMEOUT));
3982
3983         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
3984                 if (!crtc->fb)
3985                         continue;
3986
3987                 intel_crtc = to_intel_crtc(crtc);
3988                 intel_fb = to_intel_framebuffer(crtc->fb);
3989                 if (intel_fb->obj == obj) {
3990                         if (!intel_crtc->busy) {
3991                                 /* Non-busy -> busy, upclock */
3992                                 intel_increase_pllclock(crtc, true);
3993                                 intel_crtc->busy = true;
3994                         } else {
3995                                 /* Busy -> busy, put off timer */
3996                                 mod_timer(&intel_crtc->idle_timer, jiffies +
3997                                           msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
3998                         }
3999                 }
4000         }
4001 }
4002
4003 static void intel_crtc_destroy(struct drm_crtc *crtc)
4004 {
4005         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4006
4007         drm_crtc_cleanup(crtc);
4008         kfree(intel_crtc);
4009 }
4010
4011 struct intel_unpin_work {
4012         struct work_struct work;
4013         struct drm_device *dev;
4014         struct drm_gem_object *obj;
4015         struct drm_pending_vblank_event *event;
4016         int pending;
4017 };
4018
4019 static void intel_unpin_work_fn(struct work_struct *__work)
4020 {
4021         struct intel_unpin_work *work =
4022                 container_of(__work, struct intel_unpin_work, work);
4023
4024         mutex_lock(&work->dev->struct_mutex);
4025         i915_gem_object_unpin(work->obj);
4026         drm_gem_object_unreference(work->obj);
4027         mutex_unlock(&work->dev->struct_mutex);
4028         kfree(work);
4029 }
4030
4031 void intel_finish_page_flip(struct drm_device *dev, int pipe)
4032 {
4033         drm_i915_private_t *dev_priv = dev->dev_private;
4034         struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
4035         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4036         struct intel_unpin_work *work;
4037         struct drm_i915_gem_object *obj_priv;
4038         struct drm_pending_vblank_event *e;
4039         struct timeval now;
4040         unsigned long flags;
4041
4042         /* Ignore early vblank irqs */
4043         if (intel_crtc == NULL)
4044                 return;
4045
4046         spin_lock_irqsave(&dev->event_lock, flags);
4047         work = intel_crtc->unpin_work;
4048         if (work == NULL || !work->pending) {
4049                 spin_unlock_irqrestore(&dev->event_lock, flags);
4050                 return;
4051         }
4052
4053         intel_crtc->unpin_work = NULL;
4054         drm_vblank_put(dev, intel_crtc->pipe);
4055
4056         if (work->event) {
4057                 e = work->event;
4058                 do_gettimeofday(&now);
4059                 e->event.sequence = drm_vblank_count(dev, intel_crtc->pipe);
4060                 e->event.tv_sec = now.tv_sec;
4061                 e->event.tv_usec = now.tv_usec;
4062                 list_add_tail(&e->base.link,
4063                               &e->base.file_priv->event_list);
4064                 wake_up_interruptible(&e->base.file_priv->event_wait);
4065         }
4066
4067         spin_unlock_irqrestore(&dev->event_lock, flags);
4068
4069         obj_priv = work->obj->driver_private;
4070         if (atomic_dec_and_test(&obj_priv->pending_flip))
4071                 DRM_WAKEUP(&dev_priv->pending_flip_queue);
4072         schedule_work(&work->work);
4073 }
4074
4075 void intel_prepare_page_flip(struct drm_device *dev, int plane)
4076 {
4077         drm_i915_private_t *dev_priv = dev->dev_private;
4078         struct intel_crtc *intel_crtc =
4079                 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
4080         unsigned long flags;
4081
4082         spin_lock_irqsave(&dev->event_lock, flags);
4083         if (intel_crtc->unpin_work)
4084                 intel_crtc->unpin_work->pending = 1;
4085         spin_unlock_irqrestore(&dev->event_lock, flags);
4086 }
4087
4088 static int intel_crtc_page_flip(struct drm_crtc *crtc,
4089                                 struct drm_framebuffer *fb,
4090                                 struct drm_pending_vblank_event *event)
4091 {
4092         struct drm_device *dev = crtc->dev;
4093         struct drm_i915_private *dev_priv = dev->dev_private;
4094         struct intel_framebuffer *intel_fb;
4095         struct drm_i915_gem_object *obj_priv;
4096         struct drm_gem_object *obj;
4097         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4098         struct intel_unpin_work *work;
4099         unsigned long flags;
4100         int ret;
4101         RING_LOCALS;
4102
4103         work = kzalloc(sizeof *work, GFP_KERNEL);
4104         if (work == NULL)
4105                 return -ENOMEM;
4106
4107         mutex_lock(&dev->struct_mutex);
4108
4109         work->event = event;
4110         work->dev = crtc->dev;
4111         intel_fb = to_intel_framebuffer(crtc->fb);
4112         work->obj = intel_fb->obj;
4113         INIT_WORK(&work->work, intel_unpin_work_fn);
4114
4115         /* We borrow the event spin lock for protecting unpin_work */
4116         spin_lock_irqsave(&dev->event_lock, flags);
4117         if (intel_crtc->unpin_work) {
4118                 spin_unlock_irqrestore(&dev->event_lock, flags);
4119                 kfree(work);
4120                 mutex_unlock(&dev->struct_mutex);
4121                 return -EBUSY;
4122         }
4123         intel_crtc->unpin_work = work;
4124         spin_unlock_irqrestore(&dev->event_lock, flags);
4125
4126         intel_fb = to_intel_framebuffer(fb);
4127         obj = intel_fb->obj;
4128
4129         ret = intel_pin_and_fence_fb_obj(dev, obj);
4130         if (ret != 0) {
4131                 kfree(work);
4132                 mutex_unlock(&dev->struct_mutex);
4133                 return ret;
4134         }
4135
4136         /* Reference the old fb object for the scheduled work. */
4137         drm_gem_object_reference(work->obj);
4138
4139         crtc->fb = fb;
4140         i915_gem_object_flush_write_domain(obj);
4141         drm_vblank_get(dev, intel_crtc->pipe);
4142         obj_priv = obj->driver_private;
4143         atomic_inc(&obj_priv->pending_flip);
4144
4145         BEGIN_LP_RING(4);
4146         OUT_RING(MI_DISPLAY_FLIP |
4147                  MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
4148         OUT_RING(fb->pitch);
4149         if (IS_I965G(dev)) {
4150                 OUT_RING(obj_priv->gtt_offset | obj_priv->tiling_mode);
4151                 OUT_RING((fb->width << 16) | fb->height);
4152         } else {
4153                 OUT_RING(obj_priv->gtt_offset);
4154                 OUT_RING(MI_NOOP);
4155         }
4156         ADVANCE_LP_RING();
4157
4158         mutex_unlock(&dev->struct_mutex);
4159
4160         return 0;
4161 }
4162
4163 static const struct drm_crtc_helper_funcs intel_helper_funcs = {
4164         .dpms = intel_crtc_dpms,
4165         .mode_fixup = intel_crtc_mode_fixup,
4166         .mode_set = intel_crtc_mode_set,
4167         .mode_set_base = intel_pipe_set_base,
4168         .prepare = intel_crtc_prepare,
4169         .commit = intel_crtc_commit,
4170         .load_lut = intel_crtc_load_lut,
4171 };
4172
4173 static const struct drm_crtc_funcs intel_crtc_funcs = {
4174         .cursor_set = intel_crtc_cursor_set,
4175         .cursor_move = intel_crtc_cursor_move,
4176         .gamma_set = intel_crtc_gamma_set,
4177         .set_config = drm_crtc_helper_set_config,
4178         .destroy = intel_crtc_destroy,
4179         .page_flip = intel_crtc_page_flip,
4180 };
4181
4182
4183 static void intel_crtc_init(struct drm_device *dev, int pipe)
4184 {
4185         drm_i915_private_t *dev_priv = dev->dev_private;
4186         struct intel_crtc *intel_crtc;
4187         int i;
4188
4189         intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
4190         if (intel_crtc == NULL)
4191                 return;
4192
4193         drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
4194
4195         drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
4196         intel_crtc->pipe = pipe;
4197         intel_crtc->plane = pipe;
4198         for (i = 0; i < 256; i++) {
4199                 intel_crtc->lut_r[i] = i;
4200                 intel_crtc->lut_g[i] = i;
4201                 intel_crtc->lut_b[i] = i;
4202         }
4203
4204         /* Swap pipes & planes for FBC on pre-965 */
4205         intel_crtc->pipe = pipe;
4206         intel_crtc->plane = pipe;
4207         if (IS_MOBILE(dev) && (IS_I9XX(dev) && !IS_I965G(dev))) {
4208                 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
4209                 intel_crtc->plane = ((pipe == 0) ? 1 : 0);
4210         }
4211
4212         BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
4213                dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
4214         dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
4215         dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
4216
4217         intel_crtc->cursor_addr = 0;
4218         intel_crtc->dpms_mode = DRM_MODE_DPMS_OFF;
4219         drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
4220
4221         intel_crtc->busy = false;
4222
4223         setup_timer(&intel_crtc->idle_timer, intel_crtc_idle_timer,
4224                     (unsigned long)intel_crtc);
4225 }
4226
4227 int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
4228                                 struct drm_file *file_priv)
4229 {
4230         drm_i915_private_t *dev_priv = dev->dev_private;
4231         struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
4232         struct drm_mode_object *drmmode_obj;
4233         struct intel_crtc *crtc;
4234
4235         if (!dev_priv) {
4236                 DRM_ERROR("called with no initialization\n");
4237                 return -EINVAL;
4238         }
4239
4240         drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
4241                         DRM_MODE_OBJECT_CRTC);
4242
4243         if (!drmmode_obj) {
4244                 DRM_ERROR("no such CRTC id\n");
4245                 return -EINVAL;
4246         }
4247
4248         crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
4249         pipe_from_crtc_id->pipe = crtc->pipe;
4250
4251         return 0;
4252 }
4253
4254 struct drm_crtc *intel_get_crtc_from_pipe(struct drm_device *dev, int pipe)
4255 {
4256         struct drm_crtc *crtc = NULL;
4257
4258         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
4259                 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4260                 if (intel_crtc->pipe == pipe)
4261                         break;
4262         }
4263         return crtc;
4264 }
4265
4266 static int intel_connector_clones(struct drm_device *dev, int type_mask)
4267 {
4268         int index_mask = 0;
4269         struct drm_connector *connector;
4270         int entry = 0;
4271
4272         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4273                 struct intel_output *intel_output = to_intel_output(connector);
4274                 if (type_mask & intel_output->clone_mask)
4275                         index_mask |= (1 << entry);
4276                 entry++;
4277         }
4278         return index_mask;
4279 }
4280
4281
4282 static void intel_setup_outputs(struct drm_device *dev)
4283 {
4284         struct drm_i915_private *dev_priv = dev->dev_private;
4285         struct drm_connector *connector;
4286
4287         intel_crt_init(dev);
4288
4289         /* Set up integrated LVDS */
4290         if (IS_MOBILE(dev) && !IS_I830(dev))
4291                 intel_lvds_init(dev);
4292
4293         if (IS_IRONLAKE(dev)) {
4294                 int found;
4295
4296                 if (IS_MOBILE(dev) && (I915_READ(DP_A) & DP_DETECTED))
4297                         intel_dp_init(dev, DP_A);
4298
4299                 if (I915_READ(HDMIB) & PORT_DETECTED) {
4300                         /* check SDVOB */
4301                         /* found = intel_sdvo_init(dev, HDMIB); */
4302                         found = 0;
4303                         if (!found)
4304                                 intel_hdmi_init(dev, HDMIB);
4305                         if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
4306                                 intel_dp_init(dev, PCH_DP_B);
4307                 }
4308
4309                 if (I915_READ(HDMIC) & PORT_DETECTED)
4310                         intel_hdmi_init(dev, HDMIC);
4311
4312                 if (I915_READ(HDMID) & PORT_DETECTED)
4313                         intel_hdmi_init(dev, HDMID);
4314
4315                 if (I915_READ(PCH_DP_C) & DP_DETECTED)
4316                         intel_dp_init(dev, PCH_DP_C);
4317
4318                 if (I915_READ(PCH_DP_D) & DP_DETECTED)
4319                         intel_dp_init(dev, PCH_DP_D);
4320
4321         } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
4322                 bool found = false;
4323
4324                 if (I915_READ(SDVOB) & SDVO_DETECTED) {
4325                         DRM_DEBUG_KMS("probing SDVOB\n");
4326                         found = intel_sdvo_init(dev, SDVOB);
4327                         if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
4328                                 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
4329                                 intel_hdmi_init(dev, SDVOB);
4330                         }
4331
4332                         if (!found && SUPPORTS_INTEGRATED_DP(dev)) {
4333                                 DRM_DEBUG_KMS("probing DP_B\n");
4334                                 intel_dp_init(dev, DP_B);
4335                         }
4336                 }
4337
4338                 /* Before G4X SDVOC doesn't have its own detect register */
4339
4340                 if (I915_READ(SDVOB) & SDVO_DETECTED) {
4341                         DRM_DEBUG_KMS("probing SDVOC\n");
4342                         found = intel_sdvo_init(dev, SDVOC);
4343                 }
4344
4345                 if (!found && (I915_READ(SDVOC) & SDVO_DETECTED)) {
4346
4347                         if (SUPPORTS_INTEGRATED_HDMI(dev)) {
4348                                 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
4349                                 intel_hdmi_init(dev, SDVOC);
4350                         }
4351                         if (SUPPORTS_INTEGRATED_DP(dev)) {
4352                                 DRM_DEBUG_KMS("probing DP_C\n");
4353                                 intel_dp_init(dev, DP_C);
4354                         }
4355                 }
4356
4357                 if (SUPPORTS_INTEGRATED_DP(dev) &&
4358                     (I915_READ(DP_D) & DP_DETECTED)) {
4359                         DRM_DEBUG_KMS("probing DP_D\n");
4360                         intel_dp_init(dev, DP_D);
4361                 }
4362         } else if (IS_I8XX(dev))
4363                 intel_dvo_init(dev);
4364
4365         if (SUPPORTS_TV(dev))
4366                 intel_tv_init(dev);
4367
4368         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4369                 struct intel_output *intel_output = to_intel_output(connector);
4370                 struct drm_encoder *encoder = &intel_output->enc;
4371
4372                 encoder->possible_crtcs = intel_output->crtc_mask;
4373                 encoder->possible_clones = intel_connector_clones(dev,
4374                                                 intel_output->clone_mask);
4375         }
4376 }
4377
4378 static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
4379 {
4380         struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
4381         struct drm_device *dev = fb->dev;
4382
4383         if (fb->fbdev)
4384                 intelfb_remove(dev, fb);
4385
4386         drm_framebuffer_cleanup(fb);
4387         mutex_lock(&dev->struct_mutex);
4388         drm_gem_object_unreference(intel_fb->obj);
4389         mutex_unlock(&dev->struct_mutex);
4390
4391         kfree(intel_fb);
4392 }
4393
4394 static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
4395                                                 struct drm_file *file_priv,
4396                                                 unsigned int *handle)
4397 {
4398         struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
4399         struct drm_gem_object *object = intel_fb->obj;
4400
4401         return drm_gem_handle_create(file_priv, object, handle);
4402 }
4403
4404 static const struct drm_framebuffer_funcs intel_fb_funcs = {
4405         .destroy = intel_user_framebuffer_destroy,
4406         .create_handle = intel_user_framebuffer_create_handle,
4407 };
4408
4409 int intel_framebuffer_create(struct drm_device *dev,
4410                              struct drm_mode_fb_cmd *mode_cmd,
4411                              struct drm_framebuffer **fb,
4412                              struct drm_gem_object *obj)
4413 {
4414         struct intel_framebuffer *intel_fb;
4415         int ret;
4416
4417         intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
4418         if (!intel_fb)
4419                 return -ENOMEM;
4420
4421         ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
4422         if (ret) {
4423                 DRM_ERROR("framebuffer init failed %d\n", ret);
4424                 return ret;
4425         }
4426
4427         drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
4428
4429         intel_fb->obj = obj;
4430
4431         *fb = &intel_fb->base;
4432
4433         return 0;
4434 }
4435
4436
4437 static struct drm_framebuffer *
4438 intel_user_framebuffer_create(struct drm_device *dev,
4439                               struct drm_file *filp,
4440                               struct drm_mode_fb_cmd *mode_cmd)
4441 {
4442         struct drm_gem_object *obj;
4443         struct drm_framebuffer *fb;
4444         int ret;
4445
4446         obj = drm_gem_object_lookup(dev, filp, mode_cmd->handle);
4447         if (!obj)
4448                 return NULL;
4449
4450         ret = intel_framebuffer_create(dev, mode_cmd, &fb, obj);
4451         if (ret) {
4452                 mutex_lock(&dev->struct_mutex);
4453                 drm_gem_object_unreference(obj);
4454                 mutex_unlock(&dev->struct_mutex);
4455                 return NULL;
4456         }
4457
4458         return fb;
4459 }
4460
4461 static const struct drm_mode_config_funcs intel_mode_funcs = {
4462         .fb_create = intel_user_framebuffer_create,
4463         .fb_changed = intelfb_probe,
4464 };
4465
4466 static struct drm_gem_object *
4467 intel_alloc_power_context(struct drm_device *dev)
4468 {
4469         struct drm_gem_object *pwrctx;
4470         int ret;
4471
4472         pwrctx = drm_gem_object_alloc(dev, 4096);
4473         if (!pwrctx) {
4474                 DRM_DEBUG("failed to alloc power context, RC6 disabled\n");
4475                 return NULL;
4476         }
4477
4478         mutex_lock(&dev->struct_mutex);
4479         ret = i915_gem_object_pin(pwrctx, 4096);
4480         if (ret) {
4481                 DRM_ERROR("failed to pin power context: %d\n", ret);
4482                 goto err_unref;
4483         }
4484
4485         ret = i915_gem_object_set_to_gtt_domain(pwrctx, 1);
4486         if (ret) {
4487                 DRM_ERROR("failed to set-domain on power context: %d\n", ret);
4488                 goto err_unpin;
4489         }
4490         mutex_unlock(&dev->struct_mutex);
4491
4492         return pwrctx;
4493
4494 err_unpin:
4495         i915_gem_object_unpin(pwrctx);
4496 err_unref:
4497         drm_gem_object_unreference(pwrctx);
4498         mutex_unlock(&dev->struct_mutex);
4499         return NULL;
4500 }
4501
4502 void intel_init_clock_gating(struct drm_device *dev)
4503 {
4504         struct drm_i915_private *dev_priv = dev->dev_private;
4505
4506         /*
4507          * Disable clock gating reported to work incorrectly according to the
4508          * specs, but enable as much else as we can.
4509          */
4510         if (IS_IRONLAKE(dev)) {
4511                 return;
4512         } else if (IS_G4X(dev)) {
4513                 uint32_t dspclk_gate;
4514                 I915_WRITE(RENCLK_GATE_D1, 0);
4515                 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
4516                        GS_UNIT_CLOCK_GATE_DISABLE |
4517                        CL_UNIT_CLOCK_GATE_DISABLE);
4518                 I915_WRITE(RAMCLK_GATE_D, 0);
4519                 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
4520                         OVRUNIT_CLOCK_GATE_DISABLE |
4521                         OVCUNIT_CLOCK_GATE_DISABLE;
4522                 if (IS_GM45(dev))
4523                         dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
4524                 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
4525         } else if (IS_I965GM(dev)) {
4526                 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
4527                 I915_WRITE(RENCLK_GATE_D2, 0);
4528                 I915_WRITE(DSPCLK_GATE_D, 0);
4529                 I915_WRITE(RAMCLK_GATE_D, 0);
4530                 I915_WRITE16(DEUC, 0);
4531         } else if (IS_I965G(dev)) {
4532                 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
4533                        I965_RCC_CLOCK_GATE_DISABLE |
4534                        I965_RCPB_CLOCK_GATE_DISABLE |
4535                        I965_ISC_CLOCK_GATE_DISABLE |
4536                        I965_FBC_CLOCK_GATE_DISABLE);
4537                 I915_WRITE(RENCLK_GATE_D2, 0);
4538         } else if (IS_I9XX(dev)) {
4539                 u32 dstate = I915_READ(D_STATE);
4540
4541                 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
4542                         DSTATE_DOT_CLOCK_GATING;
4543                 I915_WRITE(D_STATE, dstate);
4544         } else if (IS_I85X(dev) || IS_I865G(dev)) {
4545                 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
4546         } else if (IS_I830(dev)) {
4547                 I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
4548         }
4549
4550         /*
4551          * GPU can automatically power down the render unit if given a page
4552          * to save state.
4553          */
4554         if (I915_HAS_RC6(dev) && drm_core_check_feature(dev, DRIVER_MODESET)) {
4555                 struct drm_i915_gem_object *obj_priv = NULL;
4556
4557                 if (dev_priv->pwrctx) {
4558                         obj_priv = dev_priv->pwrctx->driver_private;
4559                 } else {
4560                         struct drm_gem_object *pwrctx;
4561
4562                         pwrctx = intel_alloc_power_context(dev);
4563                         if (pwrctx) {
4564                                 dev_priv->pwrctx = pwrctx;
4565                                 obj_priv = pwrctx->driver_private;
4566                         }
4567                 }
4568
4569                 if (obj_priv) {
4570                         I915_WRITE(PWRCTXA, obj_priv->gtt_offset | PWRCTX_EN);
4571                         I915_WRITE(MCHBAR_RENDER_STANDBY,
4572                                    I915_READ(MCHBAR_RENDER_STANDBY) & ~RCX_SW_EXIT);
4573                 }
4574         }
4575 }
4576
4577 /* Set up chip specific display functions */
4578 static void intel_init_display(struct drm_device *dev)
4579 {
4580         struct drm_i915_private *dev_priv = dev->dev_private;
4581
4582         /* We always want a DPMS function */
4583         if (IS_IRONLAKE(dev))
4584                 dev_priv->display.dpms = ironlake_crtc_dpms;
4585         else
4586                 dev_priv->display.dpms = i9xx_crtc_dpms;
4587
4588         /* Only mobile has FBC, leave pointers NULL for other chips */
4589         if (IS_MOBILE(dev)) {
4590                 if (IS_GM45(dev)) {
4591                         dev_priv->display.fbc_enabled = g4x_fbc_enabled;
4592                         dev_priv->display.enable_fbc = g4x_enable_fbc;
4593                         dev_priv->display.disable_fbc = g4x_disable_fbc;
4594                 } else if (IS_I965GM(dev) || IS_I945GM(dev) || IS_I915GM(dev)) {
4595                         dev_priv->display.fbc_enabled = i8xx_fbc_enabled;
4596                         dev_priv->display.enable_fbc = i8xx_enable_fbc;
4597                         dev_priv->display.disable_fbc = i8xx_disable_fbc;
4598                 }
4599                 /* 855GM needs testing */
4600         }
4601
4602         /* Returns the core display clock speed */
4603         if (IS_I945G(dev) || (IS_G33(dev) && ! IS_PINEVIEW_M(dev)))
4604                 dev_priv->display.get_display_clock_speed =
4605                         i945_get_display_clock_speed;
4606         else if (IS_I915G(dev))
4607                 dev_priv->display.get_display_clock_speed =
4608                         i915_get_display_clock_speed;
4609         else if (IS_I945GM(dev) || IS_845G(dev) || IS_PINEVIEW_M(dev))
4610                 dev_priv->display.get_display_clock_speed =
4611                         i9xx_misc_get_display_clock_speed;
4612         else if (IS_I915GM(dev))
4613                 dev_priv->display.get_display_clock_speed =
4614                         i915gm_get_display_clock_speed;
4615         else if (IS_I865G(dev))
4616                 dev_priv->display.get_display_clock_speed =
4617                         i865_get_display_clock_speed;
4618         else if (IS_I85X(dev))
4619                 dev_priv->display.get_display_clock_speed =
4620                         i855_get_display_clock_speed;
4621         else /* 852, 830 */
4622                 dev_priv->display.get_display_clock_speed =
4623                         i830_get_display_clock_speed;
4624
4625         /* For FIFO watermark updates */
4626         if (IS_IRONLAKE(dev))
4627                 dev_priv->display.update_wm = NULL;
4628         else if (IS_G4X(dev))
4629                 dev_priv->display.update_wm = g4x_update_wm;
4630         else if (IS_I965G(dev))
4631                 dev_priv->display.update_wm = i965_update_wm;
4632         else if (IS_I9XX(dev) || IS_MOBILE(dev)) {
4633                 dev_priv->display.update_wm = i9xx_update_wm;
4634                 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
4635         } else {
4636                 if (IS_I85X(dev))
4637                         dev_priv->display.get_fifo_size = i85x_get_fifo_size;
4638                 else if (IS_845G(dev))
4639                         dev_priv->display.get_fifo_size = i845_get_fifo_size;
4640                 else
4641                         dev_priv->display.get_fifo_size = i830_get_fifo_size;
4642                 dev_priv->display.update_wm = i830_update_wm;
4643         }
4644 }
4645
4646 void intel_modeset_init(struct drm_device *dev)
4647 {
4648         struct drm_i915_private *dev_priv = dev->dev_private;
4649         int num_pipe;
4650         int i;
4651
4652         drm_mode_config_init(dev);
4653
4654         dev->mode_config.min_width = 0;
4655         dev->mode_config.min_height = 0;
4656
4657         dev->mode_config.funcs = (void *)&intel_mode_funcs;
4658
4659         intel_init_display(dev);
4660
4661         if (IS_I965G(dev)) {
4662                 dev->mode_config.max_width = 8192;
4663                 dev->mode_config.max_height = 8192;
4664         } else if (IS_I9XX(dev)) {
4665                 dev->mode_config.max_width = 4096;
4666                 dev->mode_config.max_height = 4096;
4667         } else {
4668                 dev->mode_config.max_width = 2048;
4669                 dev->mode_config.max_height = 2048;
4670         }
4671
4672         /* set memory base */
4673         if (IS_I9XX(dev))
4674                 dev->mode_config.fb_base = pci_resource_start(dev->pdev, 2);
4675         else
4676                 dev->mode_config.fb_base = pci_resource_start(dev->pdev, 0);
4677
4678         if (IS_MOBILE(dev) || IS_I9XX(dev))
4679                 num_pipe = 2;
4680         else
4681                 num_pipe = 1;
4682         DRM_DEBUG_KMS("%d display pipe%s available.\n",
4683                   num_pipe, num_pipe > 1 ? "s" : "");
4684
4685         if (IS_I85X(dev))
4686                 pci_read_config_word(dev->pdev, HPLLCC, &dev_priv->orig_clock);
4687         else if (IS_I9XX(dev) || IS_G4X(dev))
4688                 pci_read_config_word(dev->pdev, GCFGC, &dev_priv->orig_clock);
4689
4690         for (i = 0; i < num_pipe; i++) {
4691                 intel_crtc_init(dev, i);
4692         }
4693
4694         intel_setup_outputs(dev);
4695
4696         intel_init_clock_gating(dev);
4697
4698         INIT_WORK(&dev_priv->idle_work, intel_idle_update);
4699         setup_timer(&dev_priv->idle_timer, intel_gpu_idle_timer,
4700                     (unsigned long)dev);
4701
4702         intel_setup_overlay(dev);
4703
4704         if (IS_PINEVIEW(dev) && !intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
4705                                                         dev_priv->fsb_freq,
4706                                                         dev_priv->mem_freq))
4707                 DRM_INFO("failed to find known CxSR latency "
4708                          "(found fsb freq %d, mem freq %d), disabling CxSR\n",
4709                          dev_priv->fsb_freq, dev_priv->mem_freq);
4710 }
4711
4712 void intel_modeset_cleanup(struct drm_device *dev)
4713 {
4714         struct drm_i915_private *dev_priv = dev->dev_private;
4715         struct drm_crtc *crtc;
4716         struct intel_crtc *intel_crtc;
4717
4718         mutex_lock(&dev->struct_mutex);
4719
4720         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
4721                 /* Skip inactive CRTCs */
4722                 if (!crtc->fb)
4723                         continue;
4724
4725                 intel_crtc = to_intel_crtc(crtc);
4726                 intel_increase_pllclock(crtc, false);
4727                 del_timer_sync(&intel_crtc->idle_timer);
4728         }
4729
4730         del_timer_sync(&dev_priv->idle_timer);
4731
4732         if (dev_priv->display.disable_fbc)
4733                 dev_priv->display.disable_fbc(dev);
4734
4735         if (dev_priv->pwrctx) {
4736                 struct drm_i915_gem_object *obj_priv;
4737
4738                 obj_priv = dev_priv->pwrctx->driver_private;
4739                 I915_WRITE(PWRCTXA, obj_priv->gtt_offset &~ PWRCTX_EN);
4740                 I915_READ(PWRCTXA);
4741                 i915_gem_object_unpin(dev_priv->pwrctx);
4742                 drm_gem_object_unreference(dev_priv->pwrctx);
4743         }
4744
4745         mutex_unlock(&dev->struct_mutex);
4746
4747         drm_mode_config_cleanup(dev);
4748 }
4749
4750
4751 /* current intel driver doesn't take advantage of encoders
4752    always give back the encoder for the connector
4753 */
4754 struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
4755 {
4756         struct intel_output *intel_output = to_intel_output(connector);
4757
4758         return &intel_output->enc;
4759 }
4760
4761 /*
4762  * set vga decode state - true == enable VGA decode
4763  */
4764 int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
4765 {
4766         struct drm_i915_private *dev_priv = dev->dev_private;
4767         u16 gmch_ctrl;
4768
4769         pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
4770         if (state)
4771                 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
4772         else
4773                 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
4774         pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
4775         return 0;
4776 }