drm/i915: More initialization code from Linux
[dragonfly.git] / sys / dev / 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  * $FreeBSD: src/sys/dev/drm2/i915/intel_display.c,v 1.2 2012/05/24 19:13:54 dim Exp $
27  */
28
29 #include <ddb/ddb.h>
30 #include <sys/limits.h>
31
32 #include <drm/drmP.h>
33 #include <drm/drm_edid.h>
34 #include "intel_drv.h"
35 #include <drm/i915_drm.h>
36 #include "i915_drv.h"
37 #include <drm/drm_dp_helper.h>
38 #include <drm/drm_crtc_helper.h>
39
40 #include <linux/err.h>
41
42 #define HAS_eDP (intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
43
44 bool intel_pipe_has_type(struct drm_crtc *crtc, int type);
45 static void intel_increase_pllclock(struct drm_crtc *crtc);
46 static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
47
48 typedef struct {
49         /* given values */
50         int n;
51         int m1, m2;
52         int p1, p2;
53         /* derived values */
54         int     dot;
55         int     vco;
56         int     m;
57         int     p;
58 } intel_clock_t;
59
60 typedef struct {
61         int     min, max;
62 } intel_range_t;
63
64 typedef struct {
65         int     dot_limit;
66         int     p2_slow, p2_fast;
67 } intel_p2_t;
68
69 #define INTEL_P2_NUM                  2
70 typedef struct intel_limit intel_limit_t;
71 struct intel_limit {
72         intel_range_t   dot, vco, n, m, m1, m2, p, p1;
73         intel_p2_t          p2;
74         bool (* find_pll)(const intel_limit_t *, struct drm_crtc *,
75                         int, int, intel_clock_t *, intel_clock_t *);
76 };
77
78 /* FDI */
79 #define IRONLAKE_FDI_FREQ               2700000 /* in kHz for mode->clock */
80
81 static bool
82 intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
83                     int target, int refclk, intel_clock_t *match_clock,
84                     intel_clock_t *best_clock);
85 static bool
86 intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
87                         int target, int refclk, intel_clock_t *match_clock,
88                         intel_clock_t *best_clock);
89
90 static bool
91 intel_find_pll_g4x_dp(const intel_limit_t *, struct drm_crtc *crtc,
92                       int target, int refclk, intel_clock_t *match_clock,
93                       intel_clock_t *best_clock);
94 static bool
95 intel_find_pll_ironlake_dp(const intel_limit_t *, struct drm_crtc *crtc,
96                            int target, int refclk, intel_clock_t *match_clock,
97                            intel_clock_t *best_clock);
98
99 static inline u32 /* units of 100MHz */
100 intel_fdi_link_freq(struct drm_device *dev)
101 {
102         if (IS_GEN5(dev)) {
103                 struct drm_i915_private *dev_priv = dev->dev_private;
104                 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
105         } else
106                 return 27;
107 }
108
109 static const intel_limit_t intel_limits_i8xx_dvo = {
110         .dot = { .min = 25000, .max = 350000 },
111         .vco = { .min = 930000, .max = 1400000 },
112         .n = { .min = 3, .max = 16 },
113         .m = { .min = 96, .max = 140 },
114         .m1 = { .min = 18, .max = 26 },
115         .m2 = { .min = 6, .max = 16 },
116         .p = { .min = 4, .max = 128 },
117         .p1 = { .min = 2, .max = 33 },
118         .p2 = { .dot_limit = 165000,
119                 .p2_slow = 4, .p2_fast = 2 },
120         .find_pll = intel_find_best_PLL,
121 };
122
123 static const intel_limit_t intel_limits_i8xx_lvds = {
124         .dot = { .min = 25000, .max = 350000 },
125         .vco = { .min = 930000, .max = 1400000 },
126         .n = { .min = 3, .max = 16 },
127         .m = { .min = 96, .max = 140 },
128         .m1 = { .min = 18, .max = 26 },
129         .m2 = { .min = 6, .max = 16 },
130         .p = { .min = 4, .max = 128 },
131         .p1 = { .min = 1, .max = 6 },
132         .p2 = { .dot_limit = 165000,
133                 .p2_slow = 14, .p2_fast = 7 },
134         .find_pll = intel_find_best_PLL,
135 };
136
137 static const intel_limit_t intel_limits_i9xx_sdvo = {
138         .dot = { .min = 20000, .max = 400000 },
139         .vco = { .min = 1400000, .max = 2800000 },
140         .n = { .min = 1, .max = 6 },
141         .m = { .min = 70, .max = 120 },
142         .m1 = { .min = 10, .max = 22 },
143         .m2 = { .min = 5, .max = 9 },
144         .p = { .min = 5, .max = 80 },
145         .p1 = { .min = 1, .max = 8 },
146         .p2 = { .dot_limit = 200000,
147                 .p2_slow = 10, .p2_fast = 5 },
148         .find_pll = intel_find_best_PLL,
149 };
150
151 static const intel_limit_t intel_limits_i9xx_lvds = {
152         .dot = { .min = 20000, .max = 400000 },
153         .vco = { .min = 1400000, .max = 2800000 },
154         .n = { .min = 1, .max = 6 },
155         .m = { .min = 70, .max = 120 },
156         .m1 = { .min = 10, .max = 22 },
157         .m2 = { .min = 5, .max = 9 },
158         .p = { .min = 7, .max = 98 },
159         .p1 = { .min = 1, .max = 8 },
160         .p2 = { .dot_limit = 112000,
161                 .p2_slow = 14, .p2_fast = 7 },
162         .find_pll = intel_find_best_PLL,
163 };
164
165
166 static const intel_limit_t intel_limits_g4x_sdvo = {
167         .dot = { .min = 25000, .max = 270000 },
168         .vco = { .min = 1750000, .max = 3500000},
169         .n = { .min = 1, .max = 4 },
170         .m = { .min = 104, .max = 138 },
171         .m1 = { .min = 17, .max = 23 },
172         .m2 = { .min = 5, .max = 11 },
173         .p = { .min = 10, .max = 30 },
174         .p1 = { .min = 1, .max = 3},
175         .p2 = { .dot_limit = 270000,
176                 .p2_slow = 10,
177                 .p2_fast = 10
178         },
179         .find_pll = intel_g4x_find_best_PLL,
180 };
181
182 static const intel_limit_t intel_limits_g4x_hdmi = {
183         .dot = { .min = 22000, .max = 400000 },
184         .vco = { .min = 1750000, .max = 3500000},
185         .n = { .min = 1, .max = 4 },
186         .m = { .min = 104, .max = 138 },
187         .m1 = { .min = 16, .max = 23 },
188         .m2 = { .min = 5, .max = 11 },
189         .p = { .min = 5, .max = 80 },
190         .p1 = { .min = 1, .max = 8},
191         .p2 = { .dot_limit = 165000,
192                 .p2_slow = 10, .p2_fast = 5 },
193         .find_pll = intel_g4x_find_best_PLL,
194 };
195
196 static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
197         .dot = { .min = 20000, .max = 115000 },
198         .vco = { .min = 1750000, .max = 3500000 },
199         .n = { .min = 1, .max = 3 },
200         .m = { .min = 104, .max = 138 },
201         .m1 = { .min = 17, .max = 23 },
202         .m2 = { .min = 5, .max = 11 },
203         .p = { .min = 28, .max = 112 },
204         .p1 = { .min = 2, .max = 8 },
205         .p2 = { .dot_limit = 0,
206                 .p2_slow = 14, .p2_fast = 14
207         },
208         .find_pll = intel_g4x_find_best_PLL,
209 };
210
211 static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
212         .dot = { .min = 80000, .max = 224000 },
213         .vco = { .min = 1750000, .max = 3500000 },
214         .n = { .min = 1, .max = 3 },
215         .m = { .min = 104, .max = 138 },
216         .m1 = { .min = 17, .max = 23 },
217         .m2 = { .min = 5, .max = 11 },
218         .p = { .min = 14, .max = 42 },
219         .p1 = { .min = 2, .max = 6 },
220         .p2 = { .dot_limit = 0,
221                 .p2_slow = 7, .p2_fast = 7
222         },
223         .find_pll = intel_g4x_find_best_PLL,
224 };
225
226 static const intel_limit_t intel_limits_g4x_display_port = {
227         .dot = { .min = 161670, .max = 227000 },
228         .vco = { .min = 1750000, .max = 3500000},
229         .n = { .min = 1, .max = 2 },
230         .m = { .min = 97, .max = 108 },
231         .m1 = { .min = 0x10, .max = 0x12 },
232         .m2 = { .min = 0x05, .max = 0x06 },
233         .p = { .min = 10, .max = 20 },
234         .p1 = { .min = 1, .max = 2},
235         .p2 = { .dot_limit = 0,
236                 .p2_slow = 10, .p2_fast = 10 },
237         .find_pll = intel_find_pll_g4x_dp,
238 };
239
240 static const intel_limit_t intel_limits_pineview_sdvo = {
241         .dot = { .min = 20000, .max = 400000},
242         .vco = { .min = 1700000, .max = 3500000 },
243         /* Pineview's Ncounter is a ring counter */
244         .n = { .min = 3, .max = 6 },
245         .m = { .min = 2, .max = 256 },
246         /* Pineview only has one combined m divider, which we treat as m2. */
247         .m1 = { .min = 0, .max = 0 },
248         .m2 = { .min = 0, .max = 254 },
249         .p = { .min = 5, .max = 80 },
250         .p1 = { .min = 1, .max = 8 },
251         .p2 = { .dot_limit = 200000,
252                 .p2_slow = 10, .p2_fast = 5 },
253         .find_pll = intel_find_best_PLL,
254 };
255
256 static const intel_limit_t intel_limits_pineview_lvds = {
257         .dot = { .min = 20000, .max = 400000 },
258         .vco = { .min = 1700000, .max = 3500000 },
259         .n = { .min = 3, .max = 6 },
260         .m = { .min = 2, .max = 256 },
261         .m1 = { .min = 0, .max = 0 },
262         .m2 = { .min = 0, .max = 254 },
263         .p = { .min = 7, .max = 112 },
264         .p1 = { .min = 1, .max = 8 },
265         .p2 = { .dot_limit = 112000,
266                 .p2_slow = 14, .p2_fast = 14 },
267         .find_pll = intel_find_best_PLL,
268 };
269
270 /* Ironlake / Sandybridge
271  *
272  * We calculate clock using (register_value + 2) for N/M1/M2, so here
273  * the range value for them is (actual_value - 2).
274  */
275 static const intel_limit_t intel_limits_ironlake_dac = {
276         .dot = { .min = 25000, .max = 350000 },
277         .vco = { .min = 1760000, .max = 3510000 },
278         .n = { .min = 1, .max = 5 },
279         .m = { .min = 79, .max = 127 },
280         .m1 = { .min = 12, .max = 22 },
281         .m2 = { .min = 5, .max = 9 },
282         .p = { .min = 5, .max = 80 },
283         .p1 = { .min = 1, .max = 8 },
284         .p2 = { .dot_limit = 225000,
285                 .p2_slow = 10, .p2_fast = 5 },
286         .find_pll = intel_g4x_find_best_PLL,
287 };
288
289 static const intel_limit_t intel_limits_ironlake_single_lvds = {
290         .dot = { .min = 25000, .max = 350000 },
291         .vco = { .min = 1760000, .max = 3510000 },
292         .n = { .min = 1, .max = 3 },
293         .m = { .min = 79, .max = 118 },
294         .m1 = { .min = 12, .max = 22 },
295         .m2 = { .min = 5, .max = 9 },
296         .p = { .min = 28, .max = 112 },
297         .p1 = { .min = 2, .max = 8 },
298         .p2 = { .dot_limit = 225000,
299                 .p2_slow = 14, .p2_fast = 14 },
300         .find_pll = intel_g4x_find_best_PLL,
301 };
302
303 static const intel_limit_t intel_limits_ironlake_dual_lvds = {
304         .dot = { .min = 25000, .max = 350000 },
305         .vco = { .min = 1760000, .max = 3510000 },
306         .n = { .min = 1, .max = 3 },
307         .m = { .min = 79, .max = 127 },
308         .m1 = { .min = 12, .max = 22 },
309         .m2 = { .min = 5, .max = 9 },
310         .p = { .min = 14, .max = 56 },
311         .p1 = { .min = 2, .max = 8 },
312         .p2 = { .dot_limit = 225000,
313                 .p2_slow = 7, .p2_fast = 7 },
314         .find_pll = intel_g4x_find_best_PLL,
315 };
316
317 /* LVDS 100mhz refclk limits. */
318 static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
319         .dot = { .min = 25000, .max = 350000 },
320         .vco = { .min = 1760000, .max = 3510000 },
321         .n = { .min = 1, .max = 2 },
322         .m = { .min = 79, .max = 126 },
323         .m1 = { .min = 12, .max = 22 },
324         .m2 = { .min = 5, .max = 9 },
325         .p = { .min = 28, .max = 112 },
326         .p1 = { .min = 2, .max = 8 },
327         .p2 = { .dot_limit = 225000,
328                 .p2_slow = 14, .p2_fast = 14 },
329         .find_pll = intel_g4x_find_best_PLL,
330 };
331
332 static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
333         .dot = { .min = 25000, .max = 350000 },
334         .vco = { .min = 1760000, .max = 3510000 },
335         .n = { .min = 1, .max = 3 },
336         .m = { .min = 79, .max = 126 },
337         .m1 = { .min = 12, .max = 22 },
338         .m2 = { .min = 5, .max = 9 },
339         .p = { .min = 14, .max = 42 },
340         .p1 = { .min = 2, .max = 6 },
341         .p2 = { .dot_limit = 225000,
342                 .p2_slow = 7, .p2_fast = 7 },
343         .find_pll = intel_g4x_find_best_PLL,
344 };
345
346 static const intel_limit_t intel_limits_ironlake_display_port = {
347         .dot = { .min = 25000, .max = 350000 },
348         .vco = { .min = 1760000, .max = 3510000},
349         .n = { .min = 1, .max = 2 },
350         .m = { .min = 81, .max = 90 },
351         .m1 = { .min = 12, .max = 22 },
352         .m2 = { .min = 5, .max = 9 },
353         .p = { .min = 10, .max = 20 },
354         .p1 = { .min = 1, .max = 2},
355         .p2 = { .dot_limit = 0,
356                 .p2_slow = 10, .p2_fast = 10 },
357         .find_pll = intel_find_pll_ironlake_dp,
358 };
359
360 static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
361                                                 int refclk)
362 {
363         struct drm_device *dev = crtc->dev;
364         struct drm_i915_private *dev_priv = dev->dev_private;
365         const intel_limit_t *limit;
366
367         if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
368                 if ((I915_READ(PCH_LVDS) & LVDS_CLKB_POWER_MASK) ==
369                     LVDS_CLKB_POWER_UP) {
370                         /* LVDS dual channel */
371                         if (refclk == 100000)
372                                 limit = &intel_limits_ironlake_dual_lvds_100m;
373                         else
374                                 limit = &intel_limits_ironlake_dual_lvds;
375                 } else {
376                         if (refclk == 100000)
377                                 limit = &intel_limits_ironlake_single_lvds_100m;
378                         else
379                                 limit = &intel_limits_ironlake_single_lvds;
380                 }
381         } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
382                         HAS_eDP)
383                 limit = &intel_limits_ironlake_display_port;
384         else
385                 limit = &intel_limits_ironlake_dac;
386
387         return limit;
388 }
389
390 static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
391 {
392         struct drm_device *dev = crtc->dev;
393         struct drm_i915_private *dev_priv = dev->dev_private;
394         const intel_limit_t *limit;
395
396         if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
397                 if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
398                     LVDS_CLKB_POWER_UP)
399                         /* LVDS with dual channel */
400                         limit = &intel_limits_g4x_dual_channel_lvds;
401                 else
402                         /* LVDS with dual channel */
403                         limit = &intel_limits_g4x_single_channel_lvds;
404         } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
405                    intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
406                 limit = &intel_limits_g4x_hdmi;
407         } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
408                 limit = &intel_limits_g4x_sdvo;
409         } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
410                 limit = &intel_limits_g4x_display_port;
411         } else /* The option is for other outputs */
412                 limit = &intel_limits_i9xx_sdvo;
413
414         return limit;
415 }
416
417 static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
418 {
419         struct drm_device *dev = crtc->dev;
420         const intel_limit_t *limit;
421
422         if (HAS_PCH_SPLIT(dev))
423                 limit = intel_ironlake_limit(crtc, refclk);
424         else if (IS_G4X(dev)) {
425                 limit = intel_g4x_limit(crtc);
426         } else if (IS_PINEVIEW(dev)) {
427                 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
428                         limit = &intel_limits_pineview_lvds;
429                 else
430                         limit = &intel_limits_pineview_sdvo;
431         } else if (!IS_GEN2(dev)) {
432                 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
433                         limit = &intel_limits_i9xx_lvds;
434                 else
435                         limit = &intel_limits_i9xx_sdvo;
436         } else {
437                 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
438                         limit = &intel_limits_i8xx_lvds;
439                 else
440                         limit = &intel_limits_i8xx_dvo;
441         }
442         return limit;
443 }
444
445 /* m1 is reserved as 0 in Pineview, n is a ring counter */
446 static void pineview_clock(int refclk, intel_clock_t *clock)
447 {
448         clock->m = clock->m2 + 2;
449         clock->p = clock->p1 * clock->p2;
450         clock->vco = refclk * clock->m / clock->n;
451         clock->dot = clock->vco / clock->p;
452 }
453
454 static void intel_clock(struct drm_device *dev, int refclk, intel_clock_t *clock)
455 {
456         if (IS_PINEVIEW(dev)) {
457                 pineview_clock(refclk, clock);
458                 return;
459         }
460         clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
461         clock->p = clock->p1 * clock->p2;
462         clock->vco = refclk * clock->m / (clock->n + 2);
463         clock->dot = clock->vco / clock->p;
464 }
465
466 /**
467  * Returns whether any output on the specified pipe is of the specified type
468  */
469 bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
470 {
471         struct drm_device *dev = crtc->dev;
472         struct drm_mode_config *mode_config = &dev->mode_config;
473         struct intel_encoder *encoder;
474
475         list_for_each_entry(encoder, &mode_config->encoder_list, base.head)
476                 if (encoder->base.crtc == crtc && encoder->type == type)
477                         return true;
478
479         return false;
480 }
481
482 #define INTELPllInvalid(s)   do { /* DRM_DEBUG(s); */ return false; } while (0)
483 /**
484  * Returns whether the given set of divisors are valid for a given refclk with
485  * the given connectors.
486  */
487
488 static bool intel_PLL_is_valid(struct drm_device *dev,
489                                const intel_limit_t *limit,
490                                const intel_clock_t *clock)
491 {
492         if (clock->p1  < limit->p1.min  || limit->p1.max  < clock->p1)
493                 INTELPllInvalid("p1 out of range\n");
494         if (clock->p   < limit->p.min   || limit->p.max   < clock->p)
495                 INTELPllInvalid("p out of range\n");
496         if (clock->m2  < limit->m2.min  || limit->m2.max  < clock->m2)
497                 INTELPllInvalid("m2 out of range\n");
498         if (clock->m1  < limit->m1.min  || limit->m1.max  < clock->m1)
499                 INTELPllInvalid("m1 out of range\n");
500         if (clock->m1 <= clock->m2 && !IS_PINEVIEW(dev))
501                 INTELPllInvalid("m1 <= m2\n");
502         if (clock->m   < limit->m.min   || limit->m.max   < clock->m)
503                 INTELPllInvalid("m out of range\n");
504         if (clock->n   < limit->n.min   || limit->n.max   < clock->n)
505                 INTELPllInvalid("n out of range\n");
506         if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
507                 INTELPllInvalid("vco out of range\n");
508         /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
509          * connector, etc., rather than just a single range.
510          */
511         if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
512                 INTELPllInvalid("dot out of range\n");
513
514         return true;
515 }
516
517 static bool
518 intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
519                     int target, int refclk, intel_clock_t *match_clock,
520                     intel_clock_t *best_clock)
521
522 {
523         struct drm_device *dev = crtc->dev;
524         struct drm_i915_private *dev_priv = dev->dev_private;
525         intel_clock_t clock;
526         int err = target;
527
528         if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
529             (I915_READ(LVDS)) != 0) {
530                 /*
531                  * For LVDS, if the panel is on, just rely on its current
532                  * settings for dual-channel.  We haven't figured out how to
533                  * reliably set up different single/dual channel state, if we
534                  * even can.
535                  */
536                 if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
537                     LVDS_CLKB_POWER_UP)
538                         clock.p2 = limit->p2.p2_fast;
539                 else
540                         clock.p2 = limit->p2.p2_slow;
541         } else {
542                 if (target < limit->p2.dot_limit)
543                         clock.p2 = limit->p2.p2_slow;
544                 else
545                         clock.p2 = limit->p2.p2_fast;
546         }
547
548         memset(best_clock, 0, sizeof(*best_clock));
549
550         for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
551              clock.m1++) {
552                 for (clock.m2 = limit->m2.min;
553                      clock.m2 <= limit->m2.max; clock.m2++) {
554                         /* m1 is always 0 in Pineview */
555                         if (clock.m2 >= clock.m1 && !IS_PINEVIEW(dev))
556                                 break;
557                         for (clock.n = limit->n.min;
558                              clock.n <= limit->n.max; clock.n++) {
559                                 for (clock.p1 = limit->p1.min;
560                                         clock.p1 <= limit->p1.max; clock.p1++) {
561                                         int this_err;
562
563                                         intel_clock(dev, refclk, &clock);
564                                         if (!intel_PLL_is_valid(dev, limit,
565                                                                 &clock))
566                                                 continue;
567                                         if (match_clock &&
568                                             clock.p != match_clock->p)
569                                                 continue;
570
571                                         this_err = abs(clock.dot - target);
572                                         if (this_err < err) {
573                                                 *best_clock = clock;
574                                                 err = this_err;
575                                         }
576                                 }
577                         }
578                 }
579         }
580
581         return (err != target);
582 }
583
584 static bool
585 intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
586                         int target, int refclk, intel_clock_t *match_clock,
587                         intel_clock_t *best_clock)
588 {
589         struct drm_device *dev = crtc->dev;
590         struct drm_i915_private *dev_priv = dev->dev_private;
591         intel_clock_t clock;
592         int max_n;
593         bool found;
594         /* approximately equals target * 0.00585 */
595         int err_most = (target >> 8) + (target >> 9);
596         found = false;
597
598         if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
599                 int lvds_reg;
600
601                 if (HAS_PCH_SPLIT(dev))
602                         lvds_reg = PCH_LVDS;
603                 else
604                         lvds_reg = LVDS;
605                 if ((I915_READ(lvds_reg) & LVDS_CLKB_POWER_MASK) ==
606                     LVDS_CLKB_POWER_UP)
607                         clock.p2 = limit->p2.p2_fast;
608                 else
609                         clock.p2 = limit->p2.p2_slow;
610         } else {
611                 if (target < limit->p2.dot_limit)
612                         clock.p2 = limit->p2.p2_slow;
613                 else
614                         clock.p2 = limit->p2.p2_fast;
615         }
616
617         memset(best_clock, 0, sizeof(*best_clock));
618         max_n = limit->n.max;
619         /* based on hardware requirement, prefer smaller n to precision */
620         for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
621                 /* based on hardware requirement, prefere larger m1,m2 */
622                 for (clock.m1 = limit->m1.max;
623                      clock.m1 >= limit->m1.min; clock.m1--) {
624                         for (clock.m2 = limit->m2.max;
625                              clock.m2 >= limit->m2.min; clock.m2--) {
626                                 for (clock.p1 = limit->p1.max;
627                                      clock.p1 >= limit->p1.min; clock.p1--) {
628                                         int this_err;
629
630                                         intel_clock(dev, refclk, &clock);
631                                         if (!intel_PLL_is_valid(dev, limit,
632                                                                 &clock))
633                                                 continue;
634                                         if (match_clock &&
635                                             clock.p != match_clock->p)
636                                                 continue;
637
638                                         this_err = abs(clock.dot - target);
639                                         if (this_err < err_most) {
640                                                 *best_clock = clock;
641                                                 err_most = this_err;
642                                                 max_n = clock.n;
643                                                 found = true;
644                                         }
645                                 }
646                         }
647                 }
648         }
649         return found;
650 }
651
652 static bool
653 intel_find_pll_ironlake_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
654                            int target, int refclk, intel_clock_t *match_clock,
655                            intel_clock_t *best_clock)
656 {
657         struct drm_device *dev = crtc->dev;
658         intel_clock_t clock;
659
660         if (target < 200000) {
661                 clock.n = 1;
662                 clock.p1 = 2;
663                 clock.p2 = 10;
664                 clock.m1 = 12;
665                 clock.m2 = 9;
666         } else {
667                 clock.n = 2;
668                 clock.p1 = 1;
669                 clock.p2 = 10;
670                 clock.m1 = 14;
671                 clock.m2 = 8;
672         }
673         intel_clock(dev, refclk, &clock);
674         memcpy(best_clock, &clock, sizeof(intel_clock_t));
675         return true;
676 }
677
678 /* DisplayPort has only two frequencies, 162MHz and 270MHz */
679 static bool
680 intel_find_pll_g4x_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
681                       int target, int refclk, intel_clock_t *match_clock,
682                       intel_clock_t *best_clock)
683 {
684         intel_clock_t clock;
685         if (target < 200000) {
686                 clock.p1 = 2;
687                 clock.p2 = 10;
688                 clock.n = 2;
689                 clock.m1 = 23;
690                 clock.m2 = 8;
691         } else {
692                 clock.p1 = 1;
693                 clock.p2 = 10;
694                 clock.n = 1;
695                 clock.m1 = 14;
696                 clock.m2 = 2;
697         }
698         clock.m = 5 * (clock.m1 + 2) + (clock.m2 + 2);
699         clock.p = (clock.p1 * clock.p2);
700         clock.dot = 96000 * clock.m / (clock.n + 2) / clock.p;
701         clock.vco = 0;
702         memcpy(best_clock, &clock, sizeof(intel_clock_t));
703         return true;
704 }
705
706 /**
707  * intel_wait_for_vblank - wait for vblank on a given pipe
708  * @dev: drm device
709  * @pipe: pipe to wait for
710  *
711  * Wait for vblank to occur on a given pipe.  Needed for various bits of
712  * mode setting code.
713  */
714 void intel_wait_for_vblank(struct drm_device *dev, int pipe)
715 {
716         struct drm_i915_private *dev_priv = dev->dev_private;
717         int pipestat_reg = PIPESTAT(pipe);
718
719         /* Clear existing vblank status. Note this will clear any other
720          * sticky status fields as well.
721          *
722          * This races with i915_driver_irq_handler() with the result
723          * that either function could miss a vblank event.  Here it is not
724          * fatal, as we will either wait upon the next vblank interrupt or
725          * timeout.  Generally speaking intel_wait_for_vblank() is only
726          * called during modeset at which time the GPU should be idle and
727          * should *not* be performing page flips and thus not waiting on
728          * vblanks...
729          * Currently, the result of us stealing a vblank from the irq
730          * handler is that a single frame will be skipped during swapbuffers.
731          */
732         I915_WRITE(pipestat_reg,
733                    I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
734
735         /* Wait for vblank interrupt bit to set */
736         if (_intel_wait_for(dev,
737             I915_READ(pipestat_reg) & PIPE_VBLANK_INTERRUPT_STATUS,
738             50, 1, "915vbl"))
739                 DRM_DEBUG_KMS("vblank wait timed out\n");
740 }
741
742 /*
743  * intel_wait_for_pipe_off - wait for pipe to turn off
744  * @dev: drm device
745  * @pipe: pipe to wait for
746  *
747  * After disabling a pipe, we can't wait for vblank in the usual way,
748  * spinning on the vblank interrupt status bit, since we won't actually
749  * see an interrupt when the pipe is disabled.
750  *
751  * On Gen4 and above:
752  *   wait for the pipe register state bit to turn off
753  *
754  * Otherwise:
755  *   wait for the display line value to settle (it usually
756  *   ends up stopping at the start of the next frame).
757  *
758  */
759 void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
760 {
761         struct drm_i915_private *dev_priv = dev->dev_private;
762
763         if (INTEL_INFO(dev)->gen >= 4) {
764                 int reg = PIPECONF(pipe);
765
766                 /* Wait for the Pipe State to go off */
767                 if (_intel_wait_for(dev,
768                     (I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0, 100,
769                     1, "915pip"))
770                         DRM_DEBUG_KMS("pipe_off wait timed out\n");
771         } else {
772                 u32 last_line, line_mask;
773                 int reg = PIPEDSL(pipe);
774                 unsigned long timeout = jiffies + msecs_to_jiffies(100);
775
776                 if (IS_GEN2(dev))
777                         line_mask = DSL_LINEMASK_GEN2;
778                 else
779                         line_mask = DSL_LINEMASK_GEN3;
780
781                 /* Wait for the display line to settle */
782                 do {
783                         last_line = I915_READ(reg) & line_mask;
784                         DELAY(5000);
785                 } while (((I915_READ(reg) & line_mask) != last_line) &&
786                          time_after(timeout, jiffies));
787                 if (time_after(jiffies, timeout))
788                         DRM_DEBUG_KMS("pipe_off wait timed out\n");
789         }
790 }
791
792 static const char *state_string(bool enabled)
793 {
794         return enabled ? "on" : "off";
795 }
796
797 /* Only for pre-ILK configs */
798 static void assert_pll(struct drm_i915_private *dev_priv,
799                        enum i915_pipe pipe, bool state)
800 {
801         int reg;
802         u32 val;
803         bool cur_state;
804
805         reg = DPLL(pipe);
806         val = I915_READ(reg);
807         cur_state = !!(val & DPLL_VCO_ENABLE);
808         if (cur_state != state)
809                 kprintf("PLL state assertion failure (expected %s, current %s)\n",
810                     state_string(state), state_string(cur_state));
811 }
812 #define assert_pll_enabled(d, p) assert_pll(d, p, true)
813 #define assert_pll_disabled(d, p) assert_pll(d, p, false)
814
815 /* For ILK+ */
816 static void assert_pch_pll(struct drm_i915_private *dev_priv,
817                            enum i915_pipe pipe, bool state)
818 {
819         int reg;
820         u32 val;
821         bool cur_state;
822
823         if (HAS_PCH_CPT(dev_priv->dev)) {
824                 u32 pch_dpll;
825
826                 pch_dpll = I915_READ(PCH_DPLL_SEL);
827
828                 /* Make sure the selected PLL is enabled to the transcoder */
829                 KASSERT(((pch_dpll >> (4 * pipe)) & 8) != 0,
830                     ("transcoder %d PLL not enabled\n", pipe));
831
832                 /* Convert the transcoder pipe number to a pll pipe number */
833                 pipe = (pch_dpll >> (4 * pipe)) & 1;
834         }
835
836         reg = _PCH_DPLL(pipe);
837         val = I915_READ(reg);
838         cur_state = !!(val & DPLL_VCO_ENABLE);
839         if (cur_state != state)
840                 kprintf("PCH PLL state assertion failure (expected %s, current %s)\n",
841                     state_string(state), state_string(cur_state));
842 }
843 #define assert_pch_pll_enabled(d, p) assert_pch_pll(d, p, true)
844 #define assert_pch_pll_disabled(d, p) assert_pch_pll(d, p, false)
845
846 static void assert_fdi_tx(struct drm_i915_private *dev_priv,
847                           enum i915_pipe pipe, bool state)
848 {
849         int reg;
850         u32 val;
851         bool cur_state;
852
853         reg = FDI_TX_CTL(pipe);
854         val = I915_READ(reg);
855         cur_state = !!(val & FDI_TX_ENABLE);
856         if (cur_state != state)
857                 kprintf("FDI TX state assertion failure (expected %s, current %s)\n",
858                     state_string(state), state_string(cur_state));
859 }
860 #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
861 #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
862
863 static void assert_fdi_rx(struct drm_i915_private *dev_priv,
864                           enum i915_pipe pipe, bool state)
865 {
866         int reg;
867         u32 val;
868         bool cur_state;
869
870         reg = FDI_RX_CTL(pipe);
871         val = I915_READ(reg);
872         cur_state = !!(val & FDI_RX_ENABLE);
873         if (cur_state != state)
874                 kprintf("FDI RX state assertion failure (expected %s, current %s)\n",
875                     state_string(state), state_string(cur_state));
876 }
877 #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
878 #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
879
880 static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
881                                       enum i915_pipe pipe)
882 {
883         int reg;
884         u32 val;
885
886         /* ILK FDI PLL is always enabled */
887         if (dev_priv->info->gen == 5)
888                 return;
889
890         reg = FDI_TX_CTL(pipe);
891         val = I915_READ(reg);
892         if (!(val & FDI_TX_PLL_ENABLE))
893                 kprintf("FDI TX PLL assertion failure, should be active but is disabled\n");
894 }
895
896 static void assert_fdi_rx_pll_enabled(struct drm_i915_private *dev_priv,
897                                       enum i915_pipe pipe)
898 {
899         int reg;
900         u32 val;
901
902         reg = FDI_RX_CTL(pipe);
903         val = I915_READ(reg);
904         if (!(val & FDI_RX_PLL_ENABLE))
905                 kprintf("FDI RX PLL assertion failure, should be active but is disabled\n");
906 }
907
908 static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
909                                   enum i915_pipe pipe)
910 {
911         int pp_reg, lvds_reg;
912         u32 val;
913         enum i915_pipe panel_pipe = PIPE_A;
914         bool locked = true;
915
916         if (HAS_PCH_SPLIT(dev_priv->dev)) {
917                 pp_reg = PCH_PP_CONTROL;
918                 lvds_reg = PCH_LVDS;
919         } else {
920                 pp_reg = PP_CONTROL;
921                 lvds_reg = LVDS;
922         }
923
924         val = I915_READ(pp_reg);
925         if (!(val & PANEL_POWER_ON) ||
926             ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
927                 locked = false;
928
929         if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
930                 panel_pipe = PIPE_B;
931
932         if (panel_pipe == pipe && locked)
933                 kprintf("panel assertion failure, pipe %c regs locked\n",
934              pipe_name(pipe));
935 }
936
937 void assert_pipe(struct drm_i915_private *dev_priv,
938                  enum i915_pipe pipe, bool state)
939 {
940         int reg;
941         u32 val;
942         bool cur_state;
943
944         /* if we need the pipe A quirk it must be always on */
945         if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
946                 state = true;
947
948         reg = PIPECONF(pipe);
949         val = I915_READ(reg);
950         cur_state = !!(val & PIPECONF_ENABLE);
951         if (cur_state != state)
952                 kprintf("pipe %c assertion failure (expected %s, current %s)\n",
953                     pipe_name(pipe), state_string(state), state_string(cur_state));
954 }
955
956 static void assert_plane(struct drm_i915_private *dev_priv,
957                          enum plane plane, bool state)
958 {
959         int reg;
960         u32 val;
961         bool cur_state;
962
963         reg = DSPCNTR(plane);
964         val = I915_READ(reg);
965         cur_state = !!(val & DISPLAY_PLANE_ENABLE);
966         if (cur_state != state)
967                 kprintf("plane %c assertion failure, (expected %s, current %s)\n",
968                        plane_name(plane), state_string(state), state_string(cur_state));
969 }
970
971 #define assert_plane_enabled(d, p) assert_plane(d, p, true)
972 #define assert_plane_disabled(d, p) assert_plane(d, p, false)
973
974 static void assert_planes_disabled(struct drm_i915_private *dev_priv,
975                                    enum i915_pipe pipe)
976 {
977         int reg, i;
978         u32 val;
979         int cur_pipe;
980
981         /* Planes are fixed to pipes on ILK+ */
982         if (HAS_PCH_SPLIT(dev_priv->dev)) {
983                 reg = DSPCNTR(pipe);
984                 val = I915_READ(reg);
985                 if ((val & DISPLAY_PLANE_ENABLE) != 0)
986                         kprintf("plane %c assertion failure, should be disabled but not\n",
987                                plane_name(pipe));
988                 return;
989         }
990
991         /* Need to check both planes against the pipe */
992         for (i = 0; i < 2; i++) {
993                 reg = DSPCNTR(i);
994                 val = I915_READ(reg);
995                 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
996                         DISPPLANE_SEL_PIPE_SHIFT;
997                 if ((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe)
998                         kprintf("plane %c assertion failure, should be off on pipe %c but is still active\n",
999                      plane_name(i), pipe_name(pipe));
1000         }
1001 }
1002
1003 static void assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
1004 {
1005         u32 val;
1006         bool enabled;
1007
1008         val = I915_READ(PCH_DREF_CONTROL);
1009         enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1010                             DREF_SUPERSPREAD_SOURCE_MASK));
1011         if (!enabled)
1012                 kprintf("PCH refclk assertion failure, should be active but is disabled\n");
1013 }
1014
1015 static void assert_transcoder_disabled(struct drm_i915_private *dev_priv,
1016                                        enum i915_pipe pipe)
1017 {
1018         int reg;
1019         u32 val;
1020         bool enabled;
1021
1022         reg = TRANSCONF(pipe);
1023         val = I915_READ(reg);
1024         enabled = !!(val & TRANS_ENABLE);
1025         if (enabled)
1026                 kprintf("transcoder assertion failed, should be off on pipe %c but is still active\n",
1027              pipe_name(pipe));
1028 }
1029
1030 static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1031                               enum i915_pipe pipe, u32 val)
1032 {
1033         if ((val & PORT_ENABLE) == 0)
1034                 return false;
1035
1036         if (HAS_PCH_CPT(dev_priv->dev)) {
1037                 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1038                         return false;
1039         } else {
1040                 if ((val & TRANSCODER_MASK) != TRANSCODER(pipe))
1041                         return false;
1042         }
1043         return true;
1044 }
1045
1046 static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1047                               enum i915_pipe pipe, u32 val)
1048 {
1049         if ((val & LVDS_PORT_EN) == 0)
1050                 return false;
1051
1052         if (HAS_PCH_CPT(dev_priv->dev)) {
1053                 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1054                         return false;
1055         } else {
1056                 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1057                         return false;
1058         }
1059         return true;
1060 }
1061
1062 static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1063                               enum i915_pipe pipe, u32 val)
1064 {
1065         if ((val & ADPA_DAC_ENABLE) == 0)
1066                 return false;
1067         if (HAS_PCH_CPT(dev_priv->dev)) {
1068                 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1069                         return false;
1070         } else {
1071                 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1072                         return false;
1073         }
1074         return true;
1075 }
1076
1077 static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1078                             enum i915_pipe pipe, u32 port_sel, u32 val)
1079 {
1080         if ((val & DP_PORT_EN) == 0)
1081                 return false;
1082
1083         if (HAS_PCH_CPT(dev_priv->dev)) {
1084                 u32     trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1085                 u32     trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1086                 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1087                         return false;
1088         } else {
1089                 if ((val & DP_PIPE_MASK) != (pipe << 30))
1090                         return false;
1091         }
1092         return true;
1093 }
1094
1095 static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
1096                                    enum i915_pipe pipe, int reg, u32 port_sel)
1097 {
1098         u32 val = I915_READ(reg);
1099         if (dp_pipe_enabled(dev_priv, pipe, port_sel, val))
1100                 kprintf("PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
1101              reg, pipe_name(pipe));
1102 }
1103
1104 static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1105                                      enum i915_pipe pipe, int reg)
1106 {
1107         u32 val = I915_READ(reg);
1108         if (hdmi_pipe_enabled(dev_priv, val, pipe))
1109                 kprintf("PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
1110              reg, pipe_name(pipe));
1111 }
1112
1113 static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1114                                       enum i915_pipe pipe)
1115 {
1116         int reg;
1117         u32 val;
1118
1119         assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1120         assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1121         assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
1122
1123         reg = PCH_ADPA;
1124         val = I915_READ(reg);
1125         if (adpa_pipe_enabled(dev_priv, val, pipe))
1126                 kprintf("PCH VGA enabled on transcoder %c, should be disabled\n",
1127              pipe_name(pipe));
1128
1129         reg = PCH_LVDS;
1130         val = I915_READ(reg);
1131         if (lvds_pipe_enabled(dev_priv, val, pipe))
1132                 kprintf("PCH LVDS enabled on transcoder %c, should be disabled\n",
1133              pipe_name(pipe));
1134
1135         assert_pch_hdmi_disabled(dev_priv, pipe, HDMIB);
1136         assert_pch_hdmi_disabled(dev_priv, pipe, HDMIC);
1137         assert_pch_hdmi_disabled(dev_priv, pipe, HDMID);
1138 }
1139
1140 /**
1141  * intel_enable_pll - enable a PLL
1142  * @dev_priv: i915 private structure
1143  * @pipe: pipe PLL to enable
1144  *
1145  * Enable @pipe's PLL so we can start pumping pixels from a plane.  Check to
1146  * make sure the PLL reg is writable first though, since the panel write
1147  * protect mechanism may be enabled.
1148  *
1149  * Note!  This is for pre-ILK only.
1150  */
1151 static void intel_enable_pll(struct drm_i915_private *dev_priv, enum i915_pipe pipe)
1152 {
1153         int reg;
1154         u32 val;
1155
1156         /* No really, not for ILK+ */
1157         KASSERT(dev_priv->info->gen < 5, ("Wrong device gen"));
1158
1159         /* PLL is protected by panel, make sure we can write it */
1160         if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
1161                 assert_panel_unlocked(dev_priv, pipe);
1162
1163         reg = DPLL(pipe);
1164         val = I915_READ(reg);
1165         val |= DPLL_VCO_ENABLE;
1166
1167         /* We do this three times for luck */
1168         I915_WRITE(reg, val);
1169         POSTING_READ(reg);
1170         DELAY(150); /* wait for warmup */
1171         I915_WRITE(reg, val);
1172         POSTING_READ(reg);
1173         DELAY(150); /* wait for warmup */
1174         I915_WRITE(reg, val);
1175         POSTING_READ(reg);
1176         DELAY(150); /* wait for warmup */
1177 }
1178
1179 /**
1180  * intel_disable_pll - disable a PLL
1181  * @dev_priv: i915 private structure
1182  * @pipe: pipe PLL to disable
1183  *
1184  * Disable the PLL for @pipe, making sure the pipe is off first.
1185  *
1186  * Note!  This is for pre-ILK only.
1187  */
1188 static void intel_disable_pll(struct drm_i915_private *dev_priv, enum i915_pipe pipe)
1189 {
1190         int reg;
1191         u32 val;
1192
1193         /* Don't disable pipe A or pipe A PLLs if needed */
1194         if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1195                 return;
1196
1197         /* Make sure the pipe isn't still relying on us */
1198         assert_pipe_disabled(dev_priv, pipe);
1199
1200         reg = DPLL(pipe);
1201         val = I915_READ(reg);
1202         val &= ~DPLL_VCO_ENABLE;
1203         I915_WRITE(reg, val);
1204         POSTING_READ(reg);
1205 }
1206
1207 /**
1208  * intel_enable_pch_pll - enable PCH PLL
1209  * @dev_priv: i915 private structure
1210  * @pipe: pipe PLL to enable
1211  *
1212  * The PCH PLL needs to be enabled before the PCH transcoder, since it
1213  * drives the transcoder clock.
1214  */
1215 static void intel_enable_pch_pll(struct drm_i915_private *dev_priv,
1216                                  enum i915_pipe pipe)
1217 {
1218         int reg;
1219         u32 val;
1220
1221         if (pipe > 1)
1222                 return;
1223
1224         /* PCH only available on ILK+ */
1225         KASSERT(dev_priv->info->gen >= 5, ("Wrong device gen"));
1226
1227         /* PCH refclock must be enabled first */
1228         assert_pch_refclk_enabled(dev_priv);
1229
1230         reg = _PCH_DPLL(pipe);
1231         val = I915_READ(reg);
1232         val |= DPLL_VCO_ENABLE;
1233         I915_WRITE(reg, val);
1234         POSTING_READ(reg);
1235         DELAY(200);
1236 }
1237
1238 static void intel_disable_pch_pll(struct drm_i915_private *dev_priv,
1239                                   enum i915_pipe pipe)
1240 {
1241         int reg;
1242         u32 val, pll_mask = TRANSC_DPLL_ENABLE | TRANSC_DPLLB_SEL,
1243                 pll_sel = TRANSC_DPLL_ENABLE;
1244
1245         if (pipe > 1)
1246                 return;
1247
1248         /* PCH only available on ILK+ */
1249         KASSERT(dev_priv->info->gen >= 5, ("Wrong device gen"));
1250
1251         /* Make sure transcoder isn't still depending on us */
1252         assert_transcoder_disabled(dev_priv, pipe);
1253
1254         if (pipe == 0)
1255                 pll_sel |= TRANSC_DPLLA_SEL;
1256         else if (pipe == 1)
1257                 pll_sel |= TRANSC_DPLLB_SEL;
1258
1259
1260         if ((I915_READ(PCH_DPLL_SEL) & pll_mask) == pll_sel)
1261                 return;
1262
1263         reg = _PCH_DPLL(pipe);
1264         val = I915_READ(reg);
1265         val &= ~DPLL_VCO_ENABLE;
1266         I915_WRITE(reg, val);
1267         POSTING_READ(reg);
1268         DELAY(200);
1269 }
1270
1271 static void intel_enable_transcoder(struct drm_i915_private *dev_priv,
1272                                     enum i915_pipe pipe)
1273 {
1274         int reg;
1275         u32 val, pipeconf_val;
1276         struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1277
1278         /* PCH only available on ILK+ */
1279         KASSERT(dev_priv->info->gen >= 5, ("Wrong device gen"));
1280
1281         /* Make sure PCH DPLL is enabled */
1282         assert_pch_pll_enabled(dev_priv, pipe);
1283
1284         /* FDI must be feeding us bits for PCH ports */
1285         assert_fdi_tx_enabled(dev_priv, pipe);
1286         assert_fdi_rx_enabled(dev_priv, pipe);
1287
1288
1289         reg = TRANSCONF(pipe);
1290         val = I915_READ(reg);
1291         pipeconf_val = I915_READ(PIPECONF(pipe));
1292
1293         if (HAS_PCH_IBX(dev_priv->dev)) {
1294                 /*
1295                  * make the BPC in transcoder be consistent with
1296                  * that in pipeconf reg.
1297                  */
1298                 val &= ~PIPE_BPC_MASK;
1299                 val |= pipeconf_val & PIPE_BPC_MASK;
1300         }
1301
1302         val &= ~TRANS_INTERLACE_MASK;
1303         if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
1304                 if (HAS_PCH_IBX(dev_priv->dev) &&
1305                     intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
1306                         val |= TRANS_LEGACY_INTERLACED_ILK;
1307                 else
1308                         val |= TRANS_INTERLACED;
1309         else
1310                 val |= TRANS_PROGRESSIVE;
1311
1312         I915_WRITE(reg, val | TRANS_ENABLE);
1313         if (_intel_wait_for(dev_priv->dev, I915_READ(reg) & TRANS_STATE_ENABLE,
1314             100, 1, "915trc"))
1315                 DRM_ERROR("failed to enable transcoder %d\n", pipe);
1316 }
1317
1318 static void intel_disable_transcoder(struct drm_i915_private *dev_priv,
1319                                      enum i915_pipe pipe)
1320 {
1321         int reg;
1322         u32 val;
1323
1324         /* FDI relies on the transcoder */
1325         assert_fdi_tx_disabled(dev_priv, pipe);
1326         assert_fdi_rx_disabled(dev_priv, pipe);
1327
1328         /* Ports must be off as well */
1329         assert_pch_ports_disabled(dev_priv, pipe);
1330
1331         reg = TRANSCONF(pipe);
1332         val = I915_READ(reg);
1333         val &= ~TRANS_ENABLE;
1334         I915_WRITE(reg, val);
1335         /* wait for PCH transcoder off, transcoder state */
1336         if (_intel_wait_for(dev_priv->dev,
1337             (I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50,
1338             1, "915trd"))
1339                 DRM_ERROR("failed to disable transcoder %d\n", pipe);
1340 }
1341
1342 /**
1343  * intel_enable_pipe - enable a pipe, asserting requirements
1344  * @dev_priv: i915 private structure
1345  * @pipe: pipe to enable
1346  * @pch_port: on ILK+, is this pipe driving a PCH port or not
1347  *
1348  * Enable @pipe, making sure that various hardware specific requirements
1349  * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
1350  *
1351  * @pipe should be %PIPE_A or %PIPE_B.
1352  *
1353  * Will wait until the pipe is actually running (i.e. first vblank) before
1354  * returning.
1355  */
1356 static void intel_enable_pipe(struct drm_i915_private *dev_priv, enum i915_pipe pipe,
1357                               bool pch_port)
1358 {
1359         int reg;
1360         u32 val;
1361
1362         /*
1363          * A pipe without a PLL won't actually be able to drive bits from
1364          * a plane.  On ILK+ the pipe PLLs are integrated, so we don't
1365          * need the check.
1366          */
1367         if (!HAS_PCH_SPLIT(dev_priv->dev))
1368                 assert_pll_enabled(dev_priv, pipe);
1369         else {
1370                 if (pch_port) {
1371                         /* if driving the PCH, we need FDI enabled */
1372                         assert_fdi_rx_pll_enabled(dev_priv, pipe);
1373                         assert_fdi_tx_pll_enabled(dev_priv, pipe);
1374                 }
1375                 /* FIXME: assert CPU port conditions for SNB+ */
1376         }
1377
1378         reg = PIPECONF(pipe);
1379         val = I915_READ(reg);
1380         if (val & PIPECONF_ENABLE)
1381                 return;
1382
1383         I915_WRITE(reg, val | PIPECONF_ENABLE);
1384         intel_wait_for_vblank(dev_priv->dev, pipe);
1385 }
1386
1387 /**
1388  * intel_disable_pipe - disable a pipe, asserting requirements
1389  * @dev_priv: i915 private structure
1390  * @pipe: pipe to disable
1391  *
1392  * Disable @pipe, making sure that various hardware specific requirements
1393  * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
1394  *
1395  * @pipe should be %PIPE_A or %PIPE_B.
1396  *
1397  * Will wait until the pipe has shut down before returning.
1398  */
1399 static void intel_disable_pipe(struct drm_i915_private *dev_priv,
1400                                enum i915_pipe pipe)
1401 {
1402         int reg;
1403         u32 val;
1404
1405         /*
1406          * Make sure planes won't keep trying to pump pixels to us,
1407          * or we might hang the display.
1408          */
1409         assert_planes_disabled(dev_priv, pipe);
1410
1411         /* Don't disable pipe A or pipe A PLLs if needed */
1412         if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1413                 return;
1414
1415         reg = PIPECONF(pipe);
1416         val = I915_READ(reg);
1417         if ((val & PIPECONF_ENABLE) == 0)
1418                 return;
1419
1420         I915_WRITE(reg, val & ~PIPECONF_ENABLE);
1421         intel_wait_for_pipe_off(dev_priv->dev, pipe);
1422 }
1423
1424 /*
1425  * Plane regs are double buffered, going from enabled->disabled needs a
1426  * trigger in order to latch.  The display address reg provides this.
1427  */
1428 void intel_flush_display_plane(struct drm_i915_private *dev_priv,
1429                                       enum plane plane)
1430 {
1431         I915_WRITE(DSPADDR(plane), I915_READ(DSPADDR(plane)));
1432         I915_WRITE(DSPSURF(plane), I915_READ(DSPSURF(plane)));
1433 }
1434
1435 /**
1436  * intel_enable_plane - enable a display plane on a given pipe
1437  * @dev_priv: i915 private structure
1438  * @plane: plane to enable
1439  * @pipe: pipe being fed
1440  *
1441  * Enable @plane on @pipe, making sure that @pipe is running first.
1442  */
1443 static void intel_enable_plane(struct drm_i915_private *dev_priv,
1444                                enum plane plane, enum i915_pipe pipe)
1445 {
1446         int reg;
1447         u32 val;
1448
1449         /* If the pipe isn't enabled, we can't pump pixels and may hang */
1450         assert_pipe_enabled(dev_priv, pipe);
1451
1452         reg = DSPCNTR(plane);
1453         val = I915_READ(reg);
1454         if (val & DISPLAY_PLANE_ENABLE)
1455                 return;
1456
1457         I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
1458         intel_flush_display_plane(dev_priv, plane);
1459         intel_wait_for_vblank(dev_priv->dev, pipe);
1460 }
1461
1462 /**
1463  * intel_disable_plane - disable a display plane
1464  * @dev_priv: i915 private structure
1465  * @plane: plane to disable
1466  * @pipe: pipe consuming the data
1467  *
1468  * Disable @plane; should be an independent operation.
1469  */
1470 static void intel_disable_plane(struct drm_i915_private *dev_priv,
1471                                 enum plane plane, enum i915_pipe pipe)
1472 {
1473         int reg;
1474         u32 val;
1475
1476         reg = DSPCNTR(plane);
1477         val = I915_READ(reg);
1478         if ((val & DISPLAY_PLANE_ENABLE) == 0)
1479                 return;
1480
1481         I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
1482         intel_flush_display_plane(dev_priv, plane);
1483         intel_wait_for_vblank(dev_priv->dev, pipe);
1484 }
1485
1486 static void disable_pch_dp(struct drm_i915_private *dev_priv,
1487                            enum i915_pipe pipe, int reg, u32 port_sel)
1488 {
1489         u32 val = I915_READ(reg);
1490         if (dp_pipe_enabled(dev_priv, pipe, port_sel, val)) {
1491                 DRM_DEBUG_KMS("Disabling pch dp %x on pipe %d\n", reg, pipe);
1492                 I915_WRITE(reg, val & ~DP_PORT_EN);
1493         }
1494 }
1495
1496 static void disable_pch_hdmi(struct drm_i915_private *dev_priv,
1497                              enum i915_pipe pipe, int reg)
1498 {
1499         u32 val = I915_READ(reg);
1500         if (hdmi_pipe_enabled(dev_priv, val, pipe)) {
1501                 DRM_DEBUG_KMS("Disabling pch HDMI %x on pipe %d\n",
1502                               reg, pipe);
1503                 I915_WRITE(reg, val & ~PORT_ENABLE);
1504         }
1505 }
1506
1507 /* Disable any ports connected to this transcoder */
1508 static void intel_disable_pch_ports(struct drm_i915_private *dev_priv,
1509                                     enum i915_pipe pipe)
1510 {
1511         u32 reg, val;
1512
1513         val = I915_READ(PCH_PP_CONTROL);
1514         I915_WRITE(PCH_PP_CONTROL, val | PANEL_UNLOCK_REGS);
1515
1516         disable_pch_dp(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1517         disable_pch_dp(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1518         disable_pch_dp(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
1519
1520         reg = PCH_ADPA;
1521         val = I915_READ(reg);
1522         if (adpa_pipe_enabled(dev_priv, val, pipe))
1523                 I915_WRITE(reg, val & ~ADPA_DAC_ENABLE);
1524
1525         reg = PCH_LVDS;
1526         val = I915_READ(reg);
1527         if (lvds_pipe_enabled(dev_priv, val, pipe)) {
1528                 DRM_DEBUG_KMS("disable lvds on pipe %d val 0x%08x\n", pipe, val);
1529                 I915_WRITE(reg, val & ~LVDS_PORT_EN);
1530                 POSTING_READ(reg);
1531                 DELAY(100);
1532         }
1533
1534         disable_pch_hdmi(dev_priv, pipe, HDMIB);
1535         disable_pch_hdmi(dev_priv, pipe, HDMIC);
1536         disable_pch_hdmi(dev_priv, pipe, HDMID);
1537 }
1538
1539 int
1540 intel_pin_and_fence_fb_obj(struct drm_device *dev,
1541                            struct drm_i915_gem_object *obj,
1542                            struct intel_ring_buffer *pipelined)
1543 {
1544         struct drm_i915_private *dev_priv = dev->dev_private;
1545         u32 alignment;
1546         int ret;
1547
1548         alignment = 0; /* shut gcc */
1549         switch (obj->tiling_mode) {
1550         case I915_TILING_NONE:
1551                 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
1552                         alignment = 128 * 1024;
1553                 else if (INTEL_INFO(dev)->gen >= 4)
1554                         alignment = 4 * 1024;
1555                 else
1556                         alignment = 64 * 1024;
1557                 break;
1558         case I915_TILING_X:
1559                 /* pin() will align the object as required by fence */
1560                 alignment = 0;
1561                 break;
1562         case I915_TILING_Y:
1563                 /* FIXME: Is this true? */
1564                 DRM_ERROR("Y tiled not allowed for scan out buffers\n");
1565                 return -EINVAL;
1566         default:
1567                 KASSERT(0, ("Wrong tiling for fb obj"));
1568         }
1569
1570         dev_priv->mm.interruptible = false;
1571         ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
1572         if (ret)
1573                 goto err_interruptible;
1574
1575         /* Install a fence for tiled scan-out. Pre-i965 always needs a
1576          * fence, whereas 965+ only requires a fence if using
1577          * framebuffer compression.  For simplicity, we always install
1578          * a fence as the cost is not that onerous.
1579          */
1580         if (obj->tiling_mode != I915_TILING_NONE) {
1581                 ret = i915_gem_object_get_fence(obj, pipelined);
1582                 if (ret)
1583                         goto err_unpin;
1584
1585                 i915_gem_object_pin_fence(obj);
1586         }
1587
1588         dev_priv->mm.interruptible = true;
1589         return 0;
1590
1591 err_unpin:
1592         i915_gem_object_unpin(obj);
1593 err_interruptible:
1594         dev_priv->mm.interruptible = true;
1595         return ret;
1596 }
1597
1598 void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
1599 {
1600         i915_gem_object_unpin_fence(obj);
1601         i915_gem_object_unpin(obj);
1602 }
1603
1604 /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
1605  * is assumed to be a power-of-two. */
1606 unsigned long intel_gen4_compute_offset_xtiled(int *x, int *y,
1607                                                unsigned int bpp,
1608                                                unsigned int pitch)
1609 {
1610         int tile_rows, tiles;
1611
1612         tile_rows = *y / 8;
1613         *y %= 8;
1614         tiles = *x / (512/bpp);
1615         *x %= 512/bpp;
1616
1617         return tile_rows * pitch * 8 + tiles * 4096;
1618 }
1619
1620 static int i9xx_update_plane(struct drm_crtc *crtc, struct drm_framebuffer *fb,
1621                              int x, int y)
1622 {
1623         struct drm_device *dev = crtc->dev;
1624         struct drm_i915_private *dev_priv = dev->dev_private;
1625         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1626         struct intel_framebuffer *intel_fb;
1627         struct drm_i915_gem_object *obj;
1628         int plane = intel_crtc->plane;
1629         unsigned long Start, Offset;
1630         u32 dspcntr;
1631         u32 reg;
1632
1633         switch (plane) {
1634         case 0:
1635         case 1:
1636                 break;
1637         default:
1638                 DRM_ERROR("Can't update plane %d in SAREA\n", plane);
1639                 return -EINVAL;
1640         }
1641
1642         intel_fb = to_intel_framebuffer(fb);
1643         obj = intel_fb->obj;
1644
1645         reg = DSPCNTR(plane);
1646         dspcntr = I915_READ(reg);
1647         /* Mask out pixel format bits in case we change it */
1648         dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
1649         switch (fb->bits_per_pixel) {
1650         case 8:
1651                 dspcntr |= DISPPLANE_8BPP;
1652                 break;
1653         case 16:
1654                 if (fb->depth == 15)
1655                         dspcntr |= DISPPLANE_BGRX555;
1656                 else
1657                         dspcntr |= DISPPLANE_BGRX565;
1658                 break;
1659         case 24:
1660         case 32:
1661                 dspcntr |= DISPPLANE_BGRX888;
1662                 break;
1663         default:
1664                 DRM_ERROR("Unknown color depth %d\n", fb->bits_per_pixel);
1665                 return -EINVAL;
1666         }
1667         if (INTEL_INFO(dev)->gen >= 4) {
1668                 if (obj->tiling_mode != I915_TILING_NONE)
1669                         dspcntr |= DISPPLANE_TILED;
1670                 else
1671                         dspcntr &= ~DISPPLANE_TILED;
1672         }
1673
1674         I915_WRITE(reg, dspcntr);
1675
1676         Start = obj->gtt_offset;
1677         Offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
1678
1679         DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
1680                       Start, Offset, x, y, fb->pitches[0]);
1681         I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
1682         if (INTEL_INFO(dev)->gen >= 4) {
1683                 I915_WRITE(DSPSURF(plane), Start);
1684                 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
1685                 I915_WRITE(DSPADDR(plane), Offset);
1686         } else
1687                 I915_WRITE(DSPADDR(plane), Start + Offset);
1688         POSTING_READ(reg);
1689
1690         return (0);
1691 }
1692
1693 static int ironlake_update_plane(struct drm_crtc *crtc,
1694                                  struct drm_framebuffer *fb, int x, int y)
1695 {
1696         struct drm_device *dev = crtc->dev;
1697         struct drm_i915_private *dev_priv = dev->dev_private;
1698         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1699         struct intel_framebuffer *intel_fb;
1700         struct drm_i915_gem_object *obj;
1701         int plane = intel_crtc->plane;
1702         unsigned long Start, Offset;
1703         u32 dspcntr;
1704         u32 reg;
1705
1706         switch (plane) {
1707         case 0:
1708         case 1:
1709         case 2:
1710                 break;
1711         default:
1712                 DRM_ERROR("Can't update plane %d in SAREA\n", plane);
1713                 return -EINVAL;
1714         }
1715
1716         intel_fb = to_intel_framebuffer(fb);
1717         obj = intel_fb->obj;
1718
1719         reg = DSPCNTR(plane);
1720         dspcntr = I915_READ(reg);
1721         /* Mask out pixel format bits in case we change it */
1722         dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
1723         switch (fb->bits_per_pixel) {
1724         case 8:
1725                 dspcntr |= DISPPLANE_8BPP;
1726                 break;
1727         case 16:
1728                 if (fb->depth != 16) {
1729                         DRM_ERROR("bpp 16, depth %d\n", fb->depth);
1730                         return -EINVAL;
1731                 }
1732
1733                 dspcntr |= DISPPLANE_BGRX565;
1734                 break;
1735         case 24:
1736         case 32:
1737                 if (fb->depth == 24)
1738                         dspcntr |= DISPPLANE_BGRX888;
1739                 else if (fb->depth == 30)
1740                         dspcntr |= DISPPLANE_BGRX101010;
1741                 else {
1742                         DRM_ERROR("bpp %d depth %d\n", fb->bits_per_pixel,
1743                             fb->depth);
1744                         return -EINVAL;
1745                 }
1746                 break;
1747         default:
1748                 DRM_ERROR("Unknown color depth %d\n", fb->bits_per_pixel);
1749                 return -EINVAL;
1750         }
1751
1752         if (obj->tiling_mode != I915_TILING_NONE)
1753                 dspcntr |= DISPPLANE_TILED;
1754         else
1755                 dspcntr &= ~DISPPLANE_TILED;
1756
1757         /* must disable */
1758         dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
1759
1760         I915_WRITE(reg, dspcntr);
1761
1762         Start = obj->gtt_offset;
1763         Offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
1764
1765         DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
1766                       Start, Offset, x, y, fb->pitches[0]);
1767         I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
1768         I915_WRITE(DSPSURF(plane), Start);
1769         I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
1770         I915_WRITE(DSPADDR(plane), Offset);
1771         POSTING_READ(reg);
1772
1773         return 0;
1774 }
1775
1776 /* Assume fb object is pinned & idle & fenced and just update base pointers */
1777 static int
1778 intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
1779                            int x, int y, enum mode_set_atomic state)
1780 {
1781         struct drm_device *dev = crtc->dev;
1782         struct drm_i915_private *dev_priv = dev->dev_private;
1783
1784         if (dev_priv->display.disable_fbc)
1785                 dev_priv->display.disable_fbc(dev);
1786         intel_increase_pllclock(crtc);
1787
1788         return dev_priv->display.update_plane(crtc, fb, x, y);
1789 }
1790
1791 static int
1792 intel_finish_fb(struct drm_framebuffer *old_fb)
1793 {
1794         struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
1795         struct drm_device *dev = obj->base.dev;
1796         struct drm_i915_private *dev_priv = dev->dev_private;
1797         bool was_interruptible = dev_priv->mm.interruptible;
1798         int ret;
1799
1800 /* XXX */       lockmgr(&dev->event_lock, LK_EXCLUSIVE);
1801         while (!atomic_read(&dev_priv->mm.wedged) &&
1802                atomic_read(&obj->pending_flip) != 0) {
1803                 lksleep(&obj->pending_flip, &dev->event_lock,
1804                     0, "915flp", 0);
1805         }
1806 /* XXX */       lockmgr(&dev->event_lock, LK_RELEASE);
1807
1808         /* Big Hammer, we also need to ensure that any pending
1809          * MI_WAIT_FOR_EVENT inside a user batch buffer on the
1810          * current scanout is retired before unpinning the old
1811          * framebuffer.
1812          *
1813          * This should only fail upon a hung GPU, in which case we
1814          * can safely continue.
1815          */
1816         dev_priv->mm.interruptible = false;
1817         ret = i915_gem_object_finish_gpu(obj);
1818         dev_priv->mm.interruptible = was_interruptible;
1819         return ret;
1820 }
1821
1822 static int
1823 intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
1824                     struct drm_framebuffer *old_fb)
1825 {
1826         struct drm_device *dev = crtc->dev;
1827 #if 0
1828         struct drm_i915_master_private *master_priv;
1829 #else
1830         drm_i915_private_t *dev_priv = dev->dev_private;
1831 #endif
1832         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1833         int ret;
1834
1835         /* no fb bound */
1836         if (!crtc->fb) {
1837                 DRM_ERROR("No FB bound\n");
1838                 return 0;
1839         }
1840
1841         switch (intel_crtc->plane) {
1842         case 0:
1843         case 1:
1844                 break;
1845         case 2:
1846                 if (IS_IVYBRIDGE(dev))
1847                         break;
1848                 /* fall through otherwise */
1849         default:
1850                 DRM_ERROR("no plane for crtc\n");
1851                 return -EINVAL;
1852         }
1853
1854         DRM_LOCK(dev);
1855         ret = intel_pin_and_fence_fb_obj(dev,
1856                                          to_intel_framebuffer(crtc->fb)->obj,
1857                                          NULL);
1858         if (ret != 0) {
1859                 DRM_UNLOCK(dev);
1860                 DRM_ERROR("pin & fence failed\n");
1861                 return ret;
1862         }
1863
1864         if (old_fb)
1865                 intel_finish_fb(old_fb);
1866
1867         ret = intel_pipe_set_base_atomic(crtc, crtc->fb, x, y,
1868                                          LEAVE_ATOMIC_MODE_SET);
1869         if (ret) {
1870                 intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
1871                 DRM_UNLOCK(dev);
1872                 DRM_ERROR("failed to update base address\n");
1873                 return ret;
1874         }
1875
1876         if (old_fb) {
1877                 intel_wait_for_vblank(dev, intel_crtc->pipe);
1878                 intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
1879         }
1880
1881         DRM_UNLOCK(dev);
1882
1883 #if 0
1884         if (!dev->primary->master)
1885                 return 0;
1886
1887         master_priv = dev->primary->master->driver_priv;
1888         if (!master_priv->sarea_priv)
1889                 return 0;
1890
1891         if (intel_crtc->pipe) {
1892                 master_priv->sarea_priv->pipeB_x = x;
1893                 master_priv->sarea_priv->pipeB_y = y;
1894         } else {
1895                 master_priv->sarea_priv->pipeA_x = x;
1896                 master_priv->sarea_priv->pipeA_y = y;
1897         }
1898 #else
1899
1900         if (!dev_priv->sarea_priv)
1901                 return 0;
1902
1903         if (intel_crtc->pipe) {
1904                 dev_priv->sarea_priv->planeB_x = x;
1905                 dev_priv->sarea_priv->planeB_y = y;
1906         } else {
1907                 dev_priv->sarea_priv->planeA_x = x;
1908                 dev_priv->sarea_priv->planeA_y = y;
1909         }
1910 #endif
1911
1912         return 0;
1913 }
1914
1915 static void ironlake_set_pll_edp(struct drm_crtc *crtc, int clock)
1916 {
1917         struct drm_device *dev = crtc->dev;
1918         struct drm_i915_private *dev_priv = dev->dev_private;
1919         u32 dpa_ctl;
1920
1921         DRM_DEBUG_KMS("eDP PLL enable for clock %d\n", clock);
1922         dpa_ctl = I915_READ(DP_A);
1923         dpa_ctl &= ~DP_PLL_FREQ_MASK;
1924
1925         if (clock < 200000) {
1926                 u32 temp;
1927                 dpa_ctl |= DP_PLL_FREQ_160MHZ;
1928                 /* workaround for 160Mhz:
1929                    1) program 0x4600c bits 15:0 = 0x8124
1930                    2) program 0x46010 bit 0 = 1
1931                    3) program 0x46034 bit 24 = 1
1932                    4) program 0x64000 bit 14 = 1
1933                    */
1934                 temp = I915_READ(0x4600c);
1935                 temp &= 0xffff0000;
1936                 I915_WRITE(0x4600c, temp | 0x8124);
1937
1938                 temp = I915_READ(0x46010);
1939                 I915_WRITE(0x46010, temp | 1);
1940
1941                 temp = I915_READ(0x46034);
1942                 I915_WRITE(0x46034, temp | (1 << 24));
1943         } else {
1944                 dpa_ctl |= DP_PLL_FREQ_270MHZ;
1945         }
1946         I915_WRITE(DP_A, dpa_ctl);
1947
1948         POSTING_READ(DP_A);
1949         DELAY(500);
1950 }
1951
1952 static void intel_fdi_normal_train(struct drm_crtc *crtc)
1953 {
1954         struct drm_device *dev = crtc->dev;
1955         struct drm_i915_private *dev_priv = dev->dev_private;
1956         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1957         int pipe = intel_crtc->pipe;
1958         u32 reg, temp;
1959
1960         /* enable normal train */
1961         reg = FDI_TX_CTL(pipe);
1962         temp = I915_READ(reg);
1963         if (IS_IVYBRIDGE(dev)) {
1964                 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
1965                 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
1966         } else {
1967                 temp &= ~FDI_LINK_TRAIN_NONE;
1968                 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
1969         }
1970         I915_WRITE(reg, temp);
1971
1972         reg = FDI_RX_CTL(pipe);
1973         temp = I915_READ(reg);
1974         if (HAS_PCH_CPT(dev)) {
1975                 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
1976                 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
1977         } else {
1978                 temp &= ~FDI_LINK_TRAIN_NONE;
1979                 temp |= FDI_LINK_TRAIN_NONE;
1980         }
1981         I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
1982
1983         /* wait one idle pattern time */
1984         POSTING_READ(reg);
1985         DELAY(1000);
1986
1987         /* IVB wants error correction enabled */
1988         if (IS_IVYBRIDGE(dev))
1989                 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
1990                            FDI_FE_ERRC_ENABLE);
1991 }
1992
1993 static void cpt_phase_pointer_enable(struct drm_device *dev, int pipe)
1994 {
1995         struct drm_i915_private *dev_priv = dev->dev_private;
1996         u32 flags = I915_READ(SOUTH_CHICKEN1);
1997
1998         flags |= FDI_PHASE_SYNC_OVR(pipe);
1999         I915_WRITE(SOUTH_CHICKEN1, flags); /* once to unlock... */
2000         flags |= FDI_PHASE_SYNC_EN(pipe);
2001         I915_WRITE(SOUTH_CHICKEN1, flags); /* then again to enable */
2002         POSTING_READ(SOUTH_CHICKEN1);
2003 }
2004
2005 /* The FDI link training functions for ILK/Ibexpeak. */
2006 static void ironlake_fdi_link_train(struct drm_crtc *crtc)
2007 {
2008         struct drm_device *dev = crtc->dev;
2009         struct drm_i915_private *dev_priv = dev->dev_private;
2010         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2011         int pipe = intel_crtc->pipe;
2012         int plane = intel_crtc->plane;
2013         u32 reg, temp, tries;
2014
2015         /* FDI needs bits from pipe & plane first */
2016         assert_pipe_enabled(dev_priv, pipe);
2017         assert_plane_enabled(dev_priv, plane);
2018
2019         /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2020            for train result */
2021         reg = FDI_RX_IMR(pipe);
2022         temp = I915_READ(reg);
2023         temp &= ~FDI_RX_SYMBOL_LOCK;
2024         temp &= ~FDI_RX_BIT_LOCK;
2025         I915_WRITE(reg, temp);
2026         I915_READ(reg);
2027         DELAY(150);
2028
2029         /* enable CPU FDI TX and PCH FDI RX */
2030         reg = FDI_TX_CTL(pipe);
2031         temp = I915_READ(reg);
2032         temp &= ~(7 << 19);
2033         temp |= (intel_crtc->fdi_lanes - 1) << 19;
2034         temp &= ~FDI_LINK_TRAIN_NONE;
2035         temp |= FDI_LINK_TRAIN_PATTERN_1;
2036         I915_WRITE(reg, temp | FDI_TX_ENABLE);
2037
2038         reg = FDI_RX_CTL(pipe);
2039         temp = I915_READ(reg);
2040         temp &= ~FDI_LINK_TRAIN_NONE;
2041         temp |= FDI_LINK_TRAIN_PATTERN_1;
2042         I915_WRITE(reg, temp | FDI_RX_ENABLE);
2043
2044         POSTING_READ(reg);
2045         DELAY(150);
2046
2047         /* Ironlake workaround, enable clock pointer after FDI enable*/
2048         if (HAS_PCH_IBX(dev)) {
2049                 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2050                 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
2051                            FDI_RX_PHASE_SYNC_POINTER_EN);
2052         }
2053
2054         reg = FDI_RX_IIR(pipe);
2055         for (tries = 0; tries < 5; tries++) {
2056                 temp = I915_READ(reg);
2057                 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2058
2059                 if ((temp & FDI_RX_BIT_LOCK)) {
2060                         DRM_DEBUG_KMS("FDI train 1 done.\n");
2061                         I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2062                         break;
2063                 }
2064         }
2065         if (tries == 5)
2066                 DRM_ERROR("FDI train 1 fail!\n");
2067
2068         /* Train 2 */
2069         reg = FDI_TX_CTL(pipe);
2070         temp = I915_READ(reg);
2071         temp &= ~FDI_LINK_TRAIN_NONE;
2072         temp |= FDI_LINK_TRAIN_PATTERN_2;
2073         I915_WRITE(reg, temp);
2074
2075         reg = FDI_RX_CTL(pipe);
2076         temp = I915_READ(reg);
2077         temp &= ~FDI_LINK_TRAIN_NONE;
2078         temp |= FDI_LINK_TRAIN_PATTERN_2;
2079         I915_WRITE(reg, temp);
2080
2081         POSTING_READ(reg);
2082         DELAY(150);
2083
2084         reg = FDI_RX_IIR(pipe);
2085         for (tries = 0; tries < 5; tries++) {
2086                 temp = I915_READ(reg);
2087                 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2088
2089                 if (temp & FDI_RX_SYMBOL_LOCK) {
2090                         I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2091                         DRM_DEBUG_KMS("FDI train 2 done.\n");
2092                         break;
2093                 }
2094         }
2095         if (tries == 5)
2096                 DRM_ERROR("FDI train 2 fail!\n");
2097
2098         DRM_DEBUG_KMS("FDI train done\n");
2099
2100 }
2101
2102 static const int snb_b_fdi_train_param[] = {
2103         FDI_LINK_TRAIN_400MV_0DB_SNB_B,
2104         FDI_LINK_TRAIN_400MV_6DB_SNB_B,
2105         FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
2106         FDI_LINK_TRAIN_800MV_0DB_SNB_B,
2107 };
2108
2109 /* The FDI link training functions for SNB/Cougarpoint. */
2110 static void gen6_fdi_link_train(struct drm_crtc *crtc)
2111 {
2112         struct drm_device *dev = crtc->dev;
2113         struct drm_i915_private *dev_priv = dev->dev_private;
2114         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2115         int pipe = intel_crtc->pipe;
2116         u32 reg, temp, i;
2117
2118         /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2119            for train result */
2120         reg = FDI_RX_IMR(pipe);
2121         temp = I915_READ(reg);
2122         temp &= ~FDI_RX_SYMBOL_LOCK;
2123         temp &= ~FDI_RX_BIT_LOCK;
2124         I915_WRITE(reg, temp);
2125
2126         POSTING_READ(reg);
2127         DELAY(150);
2128
2129         /* enable CPU FDI TX and PCH FDI RX */
2130         reg = FDI_TX_CTL(pipe);
2131         temp = I915_READ(reg);
2132         temp &= ~(7 << 19);
2133         temp |= (intel_crtc->fdi_lanes - 1) << 19;
2134         temp &= ~FDI_LINK_TRAIN_NONE;
2135         temp |= FDI_LINK_TRAIN_PATTERN_1;
2136         temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2137         /* SNB-B */
2138         temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2139         I915_WRITE(reg, temp | FDI_TX_ENABLE);
2140
2141         reg = FDI_RX_CTL(pipe);
2142         temp = I915_READ(reg);
2143         if (HAS_PCH_CPT(dev)) {
2144                 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2145                 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2146         } else {
2147                 temp &= ~FDI_LINK_TRAIN_NONE;
2148                 temp |= FDI_LINK_TRAIN_PATTERN_1;
2149         }
2150         I915_WRITE(reg, temp | FDI_RX_ENABLE);
2151
2152         POSTING_READ(reg);
2153         DELAY(150);
2154
2155         if (HAS_PCH_CPT(dev))
2156                 cpt_phase_pointer_enable(dev, pipe);
2157
2158         for (i = 0; i < 4; i++) {
2159                 reg = FDI_TX_CTL(pipe);
2160                 temp = I915_READ(reg);
2161                 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2162                 temp |= snb_b_fdi_train_param[i];
2163                 I915_WRITE(reg, temp);
2164
2165                 POSTING_READ(reg);
2166                 DELAY(500);
2167
2168                 reg = FDI_RX_IIR(pipe);
2169                 temp = I915_READ(reg);
2170                 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2171
2172                 if (temp & FDI_RX_BIT_LOCK) {
2173                         I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2174                         DRM_DEBUG_KMS("FDI train 1 done.\n");
2175                         break;
2176                 }
2177         }
2178         if (i == 4)
2179                 DRM_ERROR("FDI train 1 fail!\n");
2180
2181         /* Train 2 */
2182         reg = FDI_TX_CTL(pipe);
2183         temp = I915_READ(reg);
2184         temp &= ~FDI_LINK_TRAIN_NONE;
2185         temp |= FDI_LINK_TRAIN_PATTERN_2;
2186         if (IS_GEN6(dev)) {
2187                 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2188                 /* SNB-B */
2189                 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2190         }
2191         I915_WRITE(reg, temp);
2192
2193         reg = FDI_RX_CTL(pipe);
2194         temp = I915_READ(reg);
2195         if (HAS_PCH_CPT(dev)) {
2196                 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2197                 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2198         } else {
2199                 temp &= ~FDI_LINK_TRAIN_NONE;
2200                 temp |= FDI_LINK_TRAIN_PATTERN_2;
2201         }
2202         I915_WRITE(reg, temp);
2203
2204         POSTING_READ(reg);
2205         DELAY(150);
2206
2207         for (i = 0; i < 4; i++) {
2208                 reg = FDI_TX_CTL(pipe);
2209                 temp = I915_READ(reg);
2210                 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2211                 temp |= snb_b_fdi_train_param[i];
2212                 I915_WRITE(reg, temp);
2213
2214                 POSTING_READ(reg);
2215                 DELAY(500);
2216
2217                 reg = FDI_RX_IIR(pipe);
2218                 temp = I915_READ(reg);
2219                 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2220
2221                 if (temp & FDI_RX_SYMBOL_LOCK) {
2222                         I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2223                         DRM_DEBUG_KMS("FDI train 2 done.\n");
2224                         break;
2225                 }
2226         }
2227         if (i == 4)
2228                 DRM_ERROR("FDI train 2 fail!\n");
2229
2230         DRM_DEBUG_KMS("FDI train done.\n");
2231 }
2232
2233 /* Manual link training for Ivy Bridge A0 parts */
2234 static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
2235 {
2236         struct drm_device *dev = crtc->dev;
2237         struct drm_i915_private *dev_priv = dev->dev_private;
2238         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2239         int pipe = intel_crtc->pipe;
2240         u32 reg, temp, i;
2241
2242         /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2243            for train result */
2244         reg = FDI_RX_IMR(pipe);
2245         temp = I915_READ(reg);
2246         temp &= ~FDI_RX_SYMBOL_LOCK;
2247         temp &= ~FDI_RX_BIT_LOCK;
2248         I915_WRITE(reg, temp);
2249
2250         POSTING_READ(reg);
2251         DELAY(150);
2252
2253         /* enable CPU FDI TX and PCH FDI RX */
2254         reg = FDI_TX_CTL(pipe);
2255         temp = I915_READ(reg);
2256         temp &= ~(7 << 19);
2257         temp |= (intel_crtc->fdi_lanes - 1) << 19;
2258         temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
2259         temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
2260         temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2261         temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2262         temp |= FDI_COMPOSITE_SYNC;
2263         I915_WRITE(reg, temp | FDI_TX_ENABLE);
2264
2265         reg = FDI_RX_CTL(pipe);
2266         temp = I915_READ(reg);
2267         temp &= ~FDI_LINK_TRAIN_AUTO;
2268         temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2269         temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2270         temp |= FDI_COMPOSITE_SYNC;
2271         I915_WRITE(reg, temp | FDI_RX_ENABLE);
2272
2273         POSTING_READ(reg);
2274         DELAY(150);
2275
2276         for (i = 0; i < 4; i++) {
2277                 reg = FDI_TX_CTL(pipe);
2278                 temp = I915_READ(reg);
2279                 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2280                 temp |= snb_b_fdi_train_param[i];
2281                 I915_WRITE(reg, temp);
2282
2283                 POSTING_READ(reg);
2284                 DELAY(500);
2285
2286                 reg = FDI_RX_IIR(pipe);
2287                 temp = I915_READ(reg);
2288                 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2289
2290                 if (temp & FDI_RX_BIT_LOCK ||
2291                     (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
2292                         I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2293                         DRM_DEBUG_KMS("FDI train 1 done.\n");
2294                         break;
2295                 }
2296         }
2297         if (i == 4)
2298                 DRM_ERROR("FDI train 1 fail!\n");
2299
2300         /* Train 2 */
2301         reg = FDI_TX_CTL(pipe);
2302         temp = I915_READ(reg);
2303         temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2304         temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
2305         temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2306         temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2307         I915_WRITE(reg, temp);
2308
2309         reg = FDI_RX_CTL(pipe);
2310         temp = I915_READ(reg);
2311         temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2312         temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2313         I915_WRITE(reg, temp);
2314
2315         POSTING_READ(reg);
2316         DELAY(150);
2317
2318         for (i = 0; i < 4; i++ ) {
2319                 reg = FDI_TX_CTL(pipe);
2320                 temp = I915_READ(reg);
2321                 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2322                 temp |= snb_b_fdi_train_param[i];
2323                 I915_WRITE(reg, temp);
2324
2325                 POSTING_READ(reg);
2326                 DELAY(500);
2327
2328                 reg = FDI_RX_IIR(pipe);
2329                 temp = I915_READ(reg);
2330                 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2331
2332                 if (temp & FDI_RX_SYMBOL_LOCK) {
2333                         I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2334                         DRM_DEBUG_KMS("FDI train 2 done.\n");
2335                         break;
2336                 }
2337         }
2338         if (i == 4)
2339                 DRM_ERROR("FDI train 2 fail!\n");
2340
2341         DRM_DEBUG_KMS("FDI train done.\n");
2342 }
2343
2344 static void ironlake_fdi_pll_enable(struct drm_crtc *crtc)
2345 {
2346         struct drm_device *dev = crtc->dev;
2347         struct drm_i915_private *dev_priv = dev->dev_private;
2348         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2349         int pipe = intel_crtc->pipe;
2350         u32 reg, temp;
2351
2352         /* Write the TU size bits so error detection works */
2353         I915_WRITE(FDI_RX_TUSIZE1(pipe),
2354                    I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
2355
2356         /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
2357         reg = FDI_RX_CTL(pipe);
2358         temp = I915_READ(reg);
2359         temp &= ~((0x7 << 19) | (0x7 << 16));
2360         temp |= (intel_crtc->fdi_lanes - 1) << 19;
2361         temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
2362         I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
2363
2364         POSTING_READ(reg);
2365         DELAY(200);
2366
2367         /* Switch from Rawclk to PCDclk */
2368         temp = I915_READ(reg);
2369         I915_WRITE(reg, temp | FDI_PCDCLK);
2370
2371         POSTING_READ(reg);
2372         DELAY(200);
2373
2374         /* Enable CPU FDI TX PLL, always on for Ironlake */
2375         reg = FDI_TX_CTL(pipe);
2376         temp = I915_READ(reg);
2377         if ((temp & FDI_TX_PLL_ENABLE) == 0) {
2378                 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
2379
2380                 POSTING_READ(reg);
2381                 DELAY(100);
2382         }
2383 }
2384
2385 static void cpt_phase_pointer_disable(struct drm_device *dev, int pipe)
2386 {
2387         struct drm_i915_private *dev_priv = dev->dev_private;
2388         u32 flags = I915_READ(SOUTH_CHICKEN1);
2389
2390         flags &= ~(FDI_PHASE_SYNC_EN(pipe));
2391         I915_WRITE(SOUTH_CHICKEN1, flags); /* once to disable... */
2392         flags &= ~(FDI_PHASE_SYNC_OVR(pipe));
2393         I915_WRITE(SOUTH_CHICKEN1, flags); /* then again to lock */
2394         POSTING_READ(SOUTH_CHICKEN1);
2395 }
2396
2397 static void ironlake_fdi_disable(struct drm_crtc *crtc)
2398 {
2399         struct drm_device *dev = crtc->dev;
2400         struct drm_i915_private *dev_priv = dev->dev_private;
2401         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2402         int pipe = intel_crtc->pipe;
2403         u32 reg, temp;
2404
2405         /* disable CPU FDI tx and PCH FDI rx */
2406         reg = FDI_TX_CTL(pipe);
2407         temp = I915_READ(reg);
2408         I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
2409         POSTING_READ(reg);
2410
2411         reg = FDI_RX_CTL(pipe);
2412         temp = I915_READ(reg);
2413         temp &= ~(0x7 << 16);
2414         temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
2415         I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
2416
2417         POSTING_READ(reg);
2418         DELAY(100);
2419
2420         /* Ironlake workaround, disable clock pointer after downing FDI */
2421         if (HAS_PCH_IBX(dev)) {
2422                 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2423                 I915_WRITE(FDI_RX_CHICKEN(pipe),
2424                            I915_READ(FDI_RX_CHICKEN(pipe) &
2425                                      ~FDI_RX_PHASE_SYNC_POINTER_EN));
2426         } else if (HAS_PCH_CPT(dev)) {
2427                 cpt_phase_pointer_disable(dev, pipe);
2428         }
2429
2430         /* still set train pattern 1 */
2431         reg = FDI_TX_CTL(pipe);
2432         temp = I915_READ(reg);
2433         temp &= ~FDI_LINK_TRAIN_NONE;
2434         temp |= FDI_LINK_TRAIN_PATTERN_1;
2435         I915_WRITE(reg, temp);
2436
2437         reg = FDI_RX_CTL(pipe);
2438         temp = I915_READ(reg);
2439         if (HAS_PCH_CPT(dev)) {
2440                 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2441                 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2442         } else {
2443                 temp &= ~FDI_LINK_TRAIN_NONE;
2444                 temp |= FDI_LINK_TRAIN_PATTERN_1;
2445         }
2446         /* BPC in FDI rx is consistent with that in PIPECONF */
2447         temp &= ~(0x07 << 16);
2448         temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
2449         I915_WRITE(reg, temp);
2450
2451         POSTING_READ(reg);
2452         DELAY(100);
2453 }
2454
2455 /*
2456  * When we disable a pipe, we need to clear any pending scanline wait events
2457  * to avoid hanging the ring, which we assume we are waiting on.
2458  */
2459 static void intel_clear_scanline_wait(struct drm_device *dev)
2460 {
2461         struct drm_i915_private *dev_priv = dev->dev_private;
2462         struct intel_ring_buffer *ring;
2463         u32 tmp;
2464
2465         if (IS_GEN2(dev))
2466                 /* Can't break the hang on i8xx */
2467                 return;
2468
2469         ring = LP_RING(dev_priv);
2470         tmp = I915_READ_CTL(ring);
2471         if (tmp & RING_WAIT)
2472                 I915_WRITE_CTL(ring, tmp);
2473 }
2474
2475 static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
2476 {
2477         struct drm_i915_gem_object *obj;
2478         struct drm_i915_private *dev_priv;
2479         struct drm_device *dev;
2480
2481         if (crtc->fb == NULL)
2482                 return;
2483
2484         obj = to_intel_framebuffer(crtc->fb)->obj;
2485         dev = crtc->dev;
2486         dev_priv = dev->dev_private;
2487         lockmgr(&dev->event_lock, LK_EXCLUSIVE);
2488         while (atomic_read(&obj->pending_flip) != 0)
2489                 lksleep(&obj->pending_flip, &dev->event_lock, 0, "915wfl", 0);
2490         lockmgr(&dev->event_lock, LK_RELEASE);
2491 }
2492
2493 static bool intel_crtc_driving_pch(struct drm_crtc *crtc)
2494 {
2495         struct drm_device *dev = crtc->dev;
2496         struct drm_mode_config *mode_config = &dev->mode_config;
2497         struct intel_encoder *encoder;
2498
2499         /*
2500          * If there's a non-PCH eDP on this crtc, it must be DP_A, and that
2501          * must be driven by its own crtc; no sharing is possible.
2502          */
2503         list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
2504                 if (encoder->base.crtc != crtc)
2505                         continue;
2506
2507                 switch (encoder->type) {
2508                 case INTEL_OUTPUT_EDP:
2509                         if (!intel_encoder_is_pch_edp(&encoder->base))
2510                                 return false;
2511                         continue;
2512                 }
2513         }
2514
2515         return true;
2516 }
2517
2518 /*
2519  * Enable PCH resources required for PCH ports:
2520  *   - PCH PLLs
2521  *   - FDI training & RX/TX
2522  *   - update transcoder timings
2523  *   - DP transcoding bits
2524  *   - transcoder
2525  */
2526 static void ironlake_pch_enable(struct drm_crtc *crtc)
2527 {
2528         struct drm_device *dev = crtc->dev;
2529         struct drm_i915_private *dev_priv = dev->dev_private;
2530         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2531         int pipe = intel_crtc->pipe;
2532         u32 reg, temp, transc_sel;
2533
2534         /* For PCH output, training FDI link */
2535         dev_priv->display.fdi_link_train(crtc);
2536
2537         intel_enable_pch_pll(dev_priv, pipe);
2538
2539         if (HAS_PCH_CPT(dev)) {
2540                 transc_sel = intel_crtc->use_pll_a ? TRANSC_DPLLA_SEL :
2541                         TRANSC_DPLLB_SEL;
2542
2543                 /* Be sure PCH DPLL SEL is set */
2544                 temp = I915_READ(PCH_DPLL_SEL);
2545                 if (pipe == 0) {
2546                         temp &= ~(TRANSA_DPLLB_SEL);
2547                         temp |= (TRANSA_DPLL_ENABLE | TRANSA_DPLLA_SEL);
2548                 } else if (pipe == 1) {
2549                         temp &= ~(TRANSB_DPLLB_SEL);
2550                         temp |= (TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
2551                 } else if (pipe == 2) {
2552                         temp &= ~(TRANSC_DPLLB_SEL);
2553                         temp |= (TRANSC_DPLL_ENABLE | transc_sel);
2554                 }
2555                 I915_WRITE(PCH_DPLL_SEL, temp);
2556         }
2557
2558         /* set transcoder timing, panel must allow it */
2559         assert_panel_unlocked(dev_priv, pipe);
2560         I915_WRITE(TRANS_HTOTAL(pipe), I915_READ(HTOTAL(pipe)));
2561         I915_WRITE(TRANS_HBLANK(pipe), I915_READ(HBLANK(pipe)));
2562         I915_WRITE(TRANS_HSYNC(pipe),  I915_READ(HSYNC(pipe)));
2563
2564         I915_WRITE(TRANS_VTOTAL(pipe), I915_READ(VTOTAL(pipe)));
2565         I915_WRITE(TRANS_VBLANK(pipe), I915_READ(VBLANK(pipe)));
2566         I915_WRITE(TRANS_VSYNC(pipe),  I915_READ(VSYNC(pipe)));
2567         I915_WRITE(TRANS_VSYNCSHIFT(pipe),  I915_READ(VSYNCSHIFT(pipe)));
2568
2569         intel_fdi_normal_train(crtc);
2570
2571         /* For PCH DP, enable TRANS_DP_CTL */
2572         if (HAS_PCH_CPT(dev) &&
2573             (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
2574              intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
2575                 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) >> 5;
2576                 reg = TRANS_DP_CTL(pipe);
2577                 temp = I915_READ(reg);
2578                 temp &= ~(TRANS_DP_PORT_SEL_MASK |
2579                           TRANS_DP_SYNC_MASK |
2580                           TRANS_DP_BPC_MASK);
2581                 temp |= (TRANS_DP_OUTPUT_ENABLE |
2582                          TRANS_DP_ENH_FRAMING);
2583                 temp |= bpc << 9; /* same format but at 11:9 */
2584
2585                 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
2586                         temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
2587                 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
2588                         temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
2589
2590                 switch (intel_trans_dp_port_sel(crtc)) {
2591                 case PCH_DP_B:
2592                         temp |= TRANS_DP_PORT_SEL_B;
2593                         break;
2594                 case PCH_DP_C:
2595                         temp |= TRANS_DP_PORT_SEL_C;
2596                         break;
2597                 case PCH_DP_D:
2598                         temp |= TRANS_DP_PORT_SEL_D;
2599                         break;
2600                 default:
2601                         DRM_DEBUG_KMS("Wrong PCH DP port return. Guess port B\n");
2602                         temp |= TRANS_DP_PORT_SEL_B;
2603                         break;
2604                 }
2605
2606                 I915_WRITE(reg, temp);
2607         }
2608
2609         intel_enable_transcoder(dev_priv, pipe);
2610 }
2611
2612 void intel_cpt_verify_modeset(struct drm_device *dev, int pipe)
2613 {
2614         struct drm_i915_private *dev_priv = dev->dev_private;
2615         int dslreg = PIPEDSL(pipe);
2616         u32 temp;
2617
2618         temp = I915_READ(dslreg);
2619         udelay(500);
2620         if (wait_for(I915_READ(dslreg) != temp, 5)) {
2621                 if (wait_for(I915_READ(dslreg) != temp, 5))
2622                         DRM_ERROR("mode set failed: pipe %d stuck\n", pipe);
2623         }
2624 }
2625
2626 static void ironlake_crtc_enable(struct drm_crtc *crtc)
2627 {
2628         struct drm_device *dev = crtc->dev;
2629         struct drm_i915_private *dev_priv = dev->dev_private;
2630         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2631         int pipe = intel_crtc->pipe;
2632         int plane = intel_crtc->plane;
2633         u32 temp;
2634         bool is_pch_port;
2635
2636         if (intel_crtc->active)
2637                 return;
2638
2639         intel_crtc->active = true;
2640         intel_update_watermarks(dev);
2641
2642         if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
2643                 temp = I915_READ(PCH_LVDS);
2644                 if ((temp & LVDS_PORT_EN) == 0)
2645                         I915_WRITE(PCH_LVDS, temp | LVDS_PORT_EN);
2646         }
2647
2648         is_pch_port = intel_crtc_driving_pch(crtc);
2649
2650         if (is_pch_port)
2651                 ironlake_fdi_pll_enable(crtc);
2652         else
2653                 ironlake_fdi_disable(crtc);
2654
2655         /* Enable panel fitting for LVDS */
2656         if (dev_priv->pch_pf_size &&
2657             (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) || HAS_eDP)) {
2658                 /* Force use of hard-coded filter coefficients
2659                  * as some pre-programmed values are broken,
2660                  * e.g. x201.
2661                  */
2662                 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
2663                 I915_WRITE(PF_WIN_POS(pipe), dev_priv->pch_pf_pos);
2664                 I915_WRITE(PF_WIN_SZ(pipe), dev_priv->pch_pf_size);
2665         }
2666
2667         intel_enable_pipe(dev_priv, pipe, is_pch_port);
2668         intel_enable_plane(dev_priv, plane, pipe);
2669
2670         if (is_pch_port)
2671                 ironlake_pch_enable(crtc);
2672
2673         intel_crtc_load_lut(crtc);
2674
2675         DRM_LOCK(dev);
2676         intel_update_fbc(dev);
2677         DRM_UNLOCK(dev);
2678
2679         intel_crtc_update_cursor(crtc, true);
2680 }
2681
2682 static void ironlake_crtc_disable(struct drm_crtc *crtc)
2683 {
2684         struct drm_device *dev = crtc->dev;
2685         struct drm_i915_private *dev_priv = dev->dev_private;
2686         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2687         int pipe = intel_crtc->pipe;
2688         int plane = intel_crtc->plane;
2689         u32 reg, temp;
2690
2691         if (!intel_crtc->active)
2692                 return;
2693
2694         intel_crtc_wait_for_pending_flips(crtc);
2695         drm_vblank_off(dev, pipe);
2696         intel_crtc_update_cursor(crtc, false);
2697
2698         intel_disable_plane(dev_priv, plane, pipe);
2699
2700         if (dev_priv->cfb_plane == plane)
2701                 intel_disable_fbc(dev);
2702
2703         intel_disable_pipe(dev_priv, pipe);
2704
2705         /* Disable PF */
2706         I915_WRITE(PF_CTL(pipe), 0);
2707         I915_WRITE(PF_WIN_SZ(pipe), 0);
2708
2709         ironlake_fdi_disable(crtc);
2710
2711         /* This is a horrible layering violation; we should be doing this in
2712          * the connector/encoder ->prepare instead, but we don't always have
2713          * enough information there about the config to know whether it will
2714          * actually be necessary or just cause undesired flicker.
2715          */
2716         intel_disable_pch_ports(dev_priv, pipe);
2717
2718         intel_disable_transcoder(dev_priv, pipe);
2719
2720         if (HAS_PCH_CPT(dev)) {
2721                 /* disable TRANS_DP_CTL */
2722                 reg = TRANS_DP_CTL(pipe);
2723                 temp = I915_READ(reg);
2724                 temp &= ~(TRANS_DP_OUTPUT_ENABLE | TRANS_DP_PORT_SEL_MASK);
2725                 temp |= TRANS_DP_PORT_SEL_NONE;
2726                 I915_WRITE(reg, temp);
2727
2728                 /* disable DPLL_SEL */
2729                 temp = I915_READ(PCH_DPLL_SEL);
2730                 switch (pipe) {
2731                 case 0:
2732                         temp &= ~(TRANSA_DPLL_ENABLE | TRANSA_DPLLB_SEL);
2733                         break;
2734                 case 1:
2735                         temp &= ~(TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
2736                         break;
2737                 case 2:
2738                         /* C shares PLL A or B */
2739                         temp &= ~(TRANSC_DPLL_ENABLE | TRANSC_DPLLB_SEL);
2740                         break;
2741                 default:
2742                         KASSERT(1, ("Wrong pipe %d", pipe)); /* wtf */
2743                 }
2744                 I915_WRITE(PCH_DPLL_SEL, temp);
2745         }
2746
2747         /* disable PCH DPLL */
2748         if (!intel_crtc->no_pll)
2749                 intel_disable_pch_pll(dev_priv, pipe);
2750
2751         /* Switch from PCDclk to Rawclk */
2752         reg = FDI_RX_CTL(pipe);
2753         temp = I915_READ(reg);
2754         I915_WRITE(reg, temp & ~FDI_PCDCLK);
2755
2756         /* Disable CPU FDI TX PLL */
2757         reg = FDI_TX_CTL(pipe);
2758         temp = I915_READ(reg);
2759         I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
2760
2761         POSTING_READ(reg);
2762         DELAY(100);
2763
2764         reg = FDI_RX_CTL(pipe);
2765         temp = I915_READ(reg);
2766         I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
2767
2768         /* Wait for the clocks to turn off. */
2769         POSTING_READ(reg);
2770         DELAY(100);
2771
2772         intel_crtc->active = false;
2773         intel_update_watermarks(dev);
2774
2775         DRM_LOCK(dev);
2776         intel_update_fbc(dev);
2777         intel_clear_scanline_wait(dev);
2778         DRM_UNLOCK(dev);
2779 }
2780
2781 static void ironlake_crtc_dpms(struct drm_crtc *crtc, int mode)
2782 {
2783         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2784         int pipe = intel_crtc->pipe;
2785         int plane = intel_crtc->plane;
2786
2787         /* XXX: When our outputs are all unaware of DPMS modes other than off
2788          * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
2789          */
2790         switch (mode) {
2791         case DRM_MODE_DPMS_ON:
2792         case DRM_MODE_DPMS_STANDBY:
2793         case DRM_MODE_DPMS_SUSPEND:
2794                 DRM_DEBUG_KMS("crtc %d/%d dpms on\n", pipe, plane);
2795                 ironlake_crtc_enable(crtc);
2796                 break;
2797
2798         case DRM_MODE_DPMS_OFF:
2799                 DRM_DEBUG_KMS("crtc %d/%d dpms off\n", pipe, plane);
2800                 ironlake_crtc_disable(crtc);
2801                 break;
2802         }
2803 }
2804
2805 static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
2806 {
2807         if (!enable && intel_crtc->overlay) {
2808                 struct drm_device *dev = intel_crtc->base.dev;
2809                 struct drm_i915_private *dev_priv = dev->dev_private;
2810
2811                 DRM_LOCK(dev);
2812                 dev_priv->mm.interruptible = false;
2813                 (void) intel_overlay_switch_off(intel_crtc->overlay);
2814                 dev_priv->mm.interruptible = true;
2815                 DRM_UNLOCK(dev);
2816         }
2817
2818         /* Let userspace switch the overlay on again. In most cases userspace
2819          * has to recompute where to put it anyway.
2820          */
2821 }
2822
2823 static void i9xx_crtc_enable(struct drm_crtc *crtc)
2824 {
2825         struct drm_device *dev = crtc->dev;
2826         struct drm_i915_private *dev_priv = dev->dev_private;
2827         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2828         int pipe = intel_crtc->pipe;
2829         int plane = intel_crtc->plane;
2830
2831         if (intel_crtc->active)
2832                 return;
2833
2834         intel_crtc->active = true;
2835         intel_update_watermarks(dev);
2836
2837         intel_enable_pll(dev_priv, pipe);
2838         intel_enable_pipe(dev_priv, pipe, false);
2839         intel_enable_plane(dev_priv, plane, pipe);
2840
2841         intel_crtc_load_lut(crtc);
2842         intel_update_fbc(dev);
2843
2844         /* Give the overlay scaler a chance to enable if it's on this pipe */
2845         intel_crtc_dpms_overlay(intel_crtc, true);
2846         intel_crtc_update_cursor(crtc, true);
2847 }
2848
2849 static void i9xx_crtc_disable(struct drm_crtc *crtc)
2850 {
2851         struct drm_device *dev = crtc->dev;
2852         struct drm_i915_private *dev_priv = dev->dev_private;
2853         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2854         int pipe = intel_crtc->pipe;
2855         int plane = intel_crtc->plane;
2856
2857         if (!intel_crtc->active)
2858                 return;
2859
2860         /* Give the overlay scaler a chance to disable if it's on this pipe */
2861         intel_crtc_wait_for_pending_flips(crtc);
2862         drm_vblank_off(dev, pipe);
2863         intel_crtc_dpms_overlay(intel_crtc, false);
2864         intel_crtc_update_cursor(crtc, false);
2865
2866         if (dev_priv->cfb_plane == plane)
2867                 intel_disable_fbc(dev);
2868
2869         intel_disable_plane(dev_priv, plane, pipe);
2870         intel_disable_pipe(dev_priv, pipe);
2871         intel_disable_pll(dev_priv, pipe);
2872
2873         intel_crtc->active = false;
2874         intel_update_fbc(dev);
2875         intel_update_watermarks(dev);
2876         intel_clear_scanline_wait(dev);
2877 }
2878
2879 static void i9xx_crtc_dpms(struct drm_crtc *crtc, int mode)
2880 {
2881         /* XXX: When our outputs are all unaware of DPMS modes other than off
2882          * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
2883          */
2884         switch (mode) {
2885         case DRM_MODE_DPMS_ON:
2886         case DRM_MODE_DPMS_STANDBY:
2887         case DRM_MODE_DPMS_SUSPEND:
2888                 i9xx_crtc_enable(crtc);
2889                 break;
2890         case DRM_MODE_DPMS_OFF:
2891                 i9xx_crtc_disable(crtc);
2892                 break;
2893         }
2894 }
2895
2896 /**
2897  * Sets the power management mode of the pipe and plane.
2898  */
2899 static void intel_crtc_dpms(struct drm_crtc *crtc, int mode)
2900 {
2901         struct drm_device *dev = crtc->dev;
2902         struct drm_i915_private *dev_priv = dev->dev_private;
2903 #if 0
2904         struct drm_i915_master_private *master_priv;
2905 #endif
2906         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2907         int pipe = intel_crtc->pipe;
2908         bool enabled;
2909
2910         if (intel_crtc->dpms_mode == mode)
2911                 return;
2912
2913         intel_crtc->dpms_mode = mode;
2914
2915         dev_priv->display.dpms(crtc, mode);
2916
2917 #if 0
2918         if (!dev->primary->master)
2919                 return;
2920
2921         master_priv = dev->primary->master->driver_priv;
2922         if (!master_priv->sarea_priv)
2923                 return;
2924 #else
2925         if (!dev_priv->sarea_priv)
2926                 return;
2927 #endif
2928
2929         enabled = crtc->enabled && mode != DRM_MODE_DPMS_OFF;
2930
2931         switch (pipe) {
2932         case 0:
2933 #if 0
2934                 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
2935                 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
2936 #else
2937                 dev_priv->sarea_priv->planeA_w = enabled ? crtc->mode.hdisplay : 0;
2938                 dev_priv->sarea_priv->planeA_h = enabled ? crtc->mode.vdisplay : 0;
2939 #endif
2940                 break;
2941         case 1:
2942 #if 0
2943                 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
2944                 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
2945 #else
2946                 dev_priv->sarea_priv->planeB_w = enabled ? crtc->mode.hdisplay : 0;
2947                 dev_priv->sarea_priv->planeB_h = enabled ? crtc->mode.vdisplay : 0;
2948 #endif
2949                 break;
2950         default:
2951                 DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
2952                 break;
2953         }
2954 }
2955
2956 static void intel_crtc_disable(struct drm_crtc *crtc)
2957 {
2958         struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
2959         struct drm_device *dev = crtc->dev;
2960
2961         /* Flush any pending WAITs before we disable the pipe. Note that
2962          * we need to drop the struct_mutex in order to acquire it again
2963          * during the lowlevel dpms routines around a couple of the
2964          * operations. It does not look trivial nor desirable to move
2965          * that locking higher. So instead we leave a window for the
2966          * submission of further commands on the fb before we can actually
2967          * disable it. This race with userspace exists anyway, and we can
2968          * only rely on the pipe being disabled by userspace after it
2969          * receives the hotplug notification and has flushed any pending
2970          * batches.
2971          */
2972         if (crtc->fb) {
2973                 DRM_LOCK(dev);
2974                 intel_finish_fb(crtc->fb);
2975                 DRM_UNLOCK(dev);
2976         }
2977
2978         crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
2979         assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
2980         assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
2981
2982         if (crtc->fb) {
2983                 DRM_LOCK(dev);
2984                 intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
2985                 DRM_UNLOCK(dev);
2986         }
2987 }
2988
2989 /* Prepare for a mode set.
2990  *
2991  * Note we could be a lot smarter here.  We need to figure out which outputs
2992  * will be enabled, which disabled (in short, how the config will changes)
2993  * and perform the minimum necessary steps to accomplish that, e.g. updating
2994  * watermarks, FBC configuration, making sure PLLs are programmed correctly,
2995  * panel fitting is in the proper state, etc.
2996  */
2997 static void i9xx_crtc_prepare(struct drm_crtc *crtc)
2998 {
2999         i9xx_crtc_disable(crtc);
3000 }
3001
3002 static void i9xx_crtc_commit(struct drm_crtc *crtc)
3003 {
3004         i9xx_crtc_enable(crtc);
3005 }
3006
3007 static void ironlake_crtc_prepare(struct drm_crtc *crtc)
3008 {
3009         ironlake_crtc_disable(crtc);
3010 }
3011
3012 static void ironlake_crtc_commit(struct drm_crtc *crtc)
3013 {
3014         ironlake_crtc_enable(crtc);
3015 }
3016
3017 void intel_encoder_prepare(struct drm_encoder *encoder)
3018 {
3019         struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
3020         /* lvds has its own version of prepare see intel_lvds_prepare */
3021         encoder_funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
3022 }
3023
3024 void intel_encoder_commit(struct drm_encoder *encoder)
3025 {
3026         struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
3027         struct drm_device *dev = encoder->dev;
3028         struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
3029         struct intel_crtc *intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
3030
3031         /* lvds has its own version of commit see intel_lvds_commit */
3032         encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
3033
3034         if (HAS_PCH_CPT(dev))
3035                 intel_cpt_verify_modeset(dev, intel_crtc->pipe);
3036 }
3037
3038 void intel_encoder_destroy(struct drm_encoder *encoder)
3039 {
3040         struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
3041
3042         drm_encoder_cleanup(encoder);
3043         drm_free(intel_encoder, DRM_MEM_KMS);
3044 }
3045
3046 static bool intel_crtc_mode_fixup(struct drm_crtc *crtc,
3047                                   const struct drm_display_mode *mode,
3048                                   struct drm_display_mode *adjusted_mode)
3049 {
3050         struct drm_device *dev = crtc->dev;
3051
3052         if (HAS_PCH_SPLIT(dev)) {
3053                 /* FDI link clock is fixed at 2.7G */
3054                 if (mode->clock * 3 > IRONLAKE_FDI_FREQ * 4)
3055                         return false;
3056         }
3057
3058         /* All interlaced capable intel hw wants timings in frames. Note though
3059          * that intel_lvds_mode_fixup does some funny tricks with the crtc
3060          * timings, so we need to be careful not to clobber these.*/
3061         if (!(adjusted_mode->private_flags & INTEL_MODE_CRTC_TIMINGS_SET))
3062                 drm_mode_set_crtcinfo(adjusted_mode, 0);
3063
3064         return true;
3065 }
3066
3067 static int valleyview_get_display_clock_speed(struct drm_device *dev)
3068 {
3069         return 400000; /* FIXME */
3070 }
3071
3072 static int i945_get_display_clock_speed(struct drm_device *dev)
3073 {
3074         return 400000;
3075 }
3076
3077 static int i915_get_display_clock_speed(struct drm_device *dev)
3078 {
3079         return 333000;
3080 }
3081
3082 static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
3083 {
3084         return 200000;
3085 }
3086
3087 static int i915gm_get_display_clock_speed(struct drm_device *dev)
3088 {
3089         u16 gcfgc = 0;
3090
3091         gcfgc = pci_read_config(dev->dev, GCFGC, 2);
3092
3093         if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
3094                 return 133000;
3095         else {
3096                 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
3097                 case GC_DISPLAY_CLOCK_333_MHZ:
3098                         return 333000;
3099                 default:
3100                 case GC_DISPLAY_CLOCK_190_200_MHZ:
3101                         return 190000;
3102                 }
3103         }
3104 }
3105
3106 static int i865_get_display_clock_speed(struct drm_device *dev)
3107 {
3108         return 266000;
3109 }
3110
3111 static int i855_get_display_clock_speed(struct drm_device *dev)
3112 {
3113         u16 hpllcc = 0;
3114         /* Assume that the hardware is in the high speed state.  This
3115          * should be the default.
3116          */
3117         switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
3118         case GC_CLOCK_133_200:
3119         case GC_CLOCK_100_200:
3120                 return 200000;
3121         case GC_CLOCK_166_250:
3122                 return 250000;
3123         case GC_CLOCK_100_133:
3124                 return 133000;
3125         }
3126
3127         /* Shouldn't happen */
3128         return 0;
3129 }
3130
3131 static int i830_get_display_clock_speed(struct drm_device *dev)
3132 {
3133         return 133000;
3134 }
3135
3136 struct fdi_m_n {
3137         u32        tu;
3138         u32        gmch_m;
3139         u32        gmch_n;
3140         u32        link_m;
3141         u32        link_n;
3142 };
3143
3144 static void
3145 fdi_reduce_ratio(u32 *num, u32 *den)
3146 {
3147         while (*num > 0xffffff || *den > 0xffffff) {
3148                 *num >>= 1;
3149                 *den >>= 1;
3150         }
3151 }
3152
3153 static void
3154 ironlake_compute_m_n(int bits_per_pixel, int nlanes, int pixel_clock,
3155                      int link_clock, struct fdi_m_n *m_n)
3156 {
3157         m_n->tu = 64; /* default size */
3158
3159         /* BUG_ON(pixel_clock > INT_MAX / 36); */
3160         m_n->gmch_m = bits_per_pixel * pixel_clock;
3161         m_n->gmch_n = link_clock * nlanes * 8;
3162         fdi_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
3163
3164         m_n->link_m = pixel_clock;
3165         m_n->link_n = link_clock;
3166         fdi_reduce_ratio(&m_n->link_m, &m_n->link_n);
3167 }
3168
3169 static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
3170 {
3171         if (i915_panel_use_ssc >= 0)
3172                 return i915_panel_use_ssc != 0;
3173         return dev_priv->lvds_use_ssc
3174                 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
3175 }
3176
3177 /**
3178  * intel_choose_pipe_bpp_dither - figure out what color depth the pipe should send
3179  * @crtc: CRTC structure
3180  * @mode: requested mode
3181  *
3182  * A pipe may be connected to one or more outputs.  Based on the depth of the
3183  * attached framebuffer, choose a good color depth to use on the pipe.
3184  *
3185  * If possible, match the pipe depth to the fb depth.  In some cases, this
3186  * isn't ideal, because the connected output supports a lesser or restricted
3187  * set of depths.  Resolve that here:
3188  *    LVDS typically supports only 6bpc, so clamp down in that case
3189  *    HDMI supports only 8bpc or 12bpc, so clamp to 8bpc with dither for 10bpc
3190  *    Displays may support a restricted set as well, check EDID and clamp as
3191  *      appropriate.
3192  *    DP may want to dither down to 6bpc to fit larger modes
3193  *
3194  * RETURNS:
3195  * Dithering requirement (i.e. false if display bpc and pipe bpc match,
3196  * true if they don't match).
3197  */
3198 static bool intel_choose_pipe_bpp_dither(struct drm_crtc *crtc,
3199                                          unsigned int *pipe_bpp,
3200                                          struct drm_display_mode *mode)
3201 {
3202         struct drm_device *dev = crtc->dev;
3203         struct drm_i915_private *dev_priv = dev->dev_private;
3204         struct drm_encoder *encoder;
3205         struct drm_connector *connector;
3206         unsigned int display_bpc = UINT_MAX, bpc;
3207
3208         /* Walk the encoders & connectors on this crtc, get min bpc */
3209         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
3210                 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
3211
3212                 if (encoder->crtc != crtc)
3213                         continue;
3214
3215                 if (intel_encoder->type == INTEL_OUTPUT_LVDS) {
3216                         unsigned int lvds_bpc;
3217
3218                         if ((I915_READ(PCH_LVDS) & LVDS_A3_POWER_MASK) ==
3219                             LVDS_A3_POWER_UP)
3220                                 lvds_bpc = 8;
3221                         else
3222                                 lvds_bpc = 6;
3223
3224                         if (lvds_bpc < display_bpc) {
3225                                 DRM_DEBUG_KMS("clamping display bpc (was %d) to LVDS (%d)\n", display_bpc, lvds_bpc);
3226                                 display_bpc = lvds_bpc;
3227                         }
3228                         continue;
3229                 }
3230
3231                 if (intel_encoder->type == INTEL_OUTPUT_EDP) {
3232                         /* Use VBT settings if we have an eDP panel */
3233                         unsigned int edp_bpc = dev_priv->edp.bpp / 3;
3234
3235                         if (edp_bpc < display_bpc) {
3236                                 DRM_DEBUG_KMS("clamping display bpc (was %d) to eDP (%d)\n", display_bpc, edp_bpc);
3237                                 display_bpc = edp_bpc;
3238                         }
3239                         continue;
3240                 }
3241
3242                 /* Not one of the known troublemakers, check the EDID */
3243                 list_for_each_entry(connector, &dev->mode_config.connector_list,
3244                                     head) {
3245                         if (connector->encoder != encoder)
3246                                 continue;
3247
3248                         /* Don't use an invalid EDID bpc value */
3249                         if (connector->display_info.bpc &&
3250                             connector->display_info.bpc < display_bpc) {
3251                                 DRM_DEBUG_KMS("clamping display bpc (was %d) to EDID reported max of %d\n", display_bpc, connector->display_info.bpc);
3252                                 display_bpc = connector->display_info.bpc;
3253                         }
3254                 }
3255
3256                 /*
3257                  * HDMI is either 12 or 8, so if the display lets 10bpc sneak
3258                  * through, clamp it down.  (Note: >12bpc will be caught below.)
3259                  */
3260                 if (intel_encoder->type == INTEL_OUTPUT_HDMI) {
3261                         if (display_bpc > 8 && display_bpc < 12) {
3262                                 DRM_DEBUG_KMS("forcing bpc to 12 for HDMI\n");
3263                                 display_bpc = 12;
3264                         } else {
3265                                 DRM_DEBUG_KMS("forcing bpc to 8 for HDMI\n");
3266                                 display_bpc = 8;
3267                         }
3268                 }
3269         }
3270
3271         if (mode->private_flags & INTEL_MODE_DP_FORCE_6BPC) {
3272                 DRM_DEBUG_KMS("Dithering DP to 6bpc\n");
3273                 display_bpc = 6;
3274         }
3275
3276         /*
3277          * We could just drive the pipe at the highest bpc all the time and
3278          * enable dithering as needed, but that costs bandwidth.  So choose
3279          * the minimum value that expresses the full color range of the fb but
3280          * also stays within the max display bpc discovered above.
3281          */
3282
3283         switch (crtc->fb->depth) {
3284         case 8:
3285                 bpc = 8; /* since we go through a colormap */
3286                 break;
3287         case 15:
3288         case 16:
3289                 bpc = 6; /* min is 18bpp */
3290                 break;
3291         case 24:
3292                 bpc = 8;
3293                 break;
3294         case 30:
3295                 bpc = 10;
3296                 break;
3297         case 48:
3298                 bpc = 12;
3299                 break;
3300         default:
3301                 DRM_DEBUG("unsupported depth, assuming 24 bits\n");
3302                 bpc = min((unsigned int)8, display_bpc);
3303                 break;
3304         }
3305
3306         display_bpc = min(display_bpc, bpc);
3307
3308         DRM_DEBUG_KMS("setting pipe bpc to %d (max display bpc %d)\n",
3309                          bpc, display_bpc);
3310
3311         *pipe_bpp = display_bpc * 3;
3312
3313         return display_bpc != bpc;
3314 }
3315
3316 static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
3317 {
3318         struct drm_device *dev = crtc->dev;
3319         struct drm_i915_private *dev_priv = dev->dev_private;
3320         int refclk;
3321
3322         if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
3323             intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
3324                 refclk = dev_priv->lvds_ssc_freq * 1000;
3325                 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
3326                               refclk / 1000);
3327         } else if (!IS_GEN2(dev)) {
3328                 refclk = 96000;
3329         } else {
3330                 refclk = 48000;
3331         }
3332
3333         return refclk;
3334 }
3335
3336 static void i9xx_adjust_sdvo_tv_clock(struct drm_display_mode *adjusted_mode,
3337                                       intel_clock_t *clock)
3338 {
3339         /* SDVO TV has fixed PLL values depend on its clock range,
3340            this mirrors vbios setting. */
3341         if (adjusted_mode->clock >= 100000
3342             && adjusted_mode->clock < 140500) {
3343                 clock->p1 = 2;
3344                 clock->p2 = 10;
3345                 clock->n = 3;
3346                 clock->m1 = 16;
3347                 clock->m2 = 8;
3348         } else if (adjusted_mode->clock >= 140500
3349                    && adjusted_mode->clock <= 200000) {
3350                 clock->p1 = 1;
3351                 clock->p2 = 10;
3352                 clock->n = 6;
3353                 clock->m1 = 12;
3354                 clock->m2 = 8;
3355         }
3356 }
3357
3358 static void i9xx_update_pll_dividers(struct drm_crtc *crtc,
3359                                      intel_clock_t *clock,
3360                                      intel_clock_t *reduced_clock)
3361 {
3362         struct drm_device *dev = crtc->dev;
3363         struct drm_i915_private *dev_priv = dev->dev_private;
3364         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3365         int pipe = intel_crtc->pipe;
3366         u32 fp, fp2 = 0;
3367
3368         if (IS_PINEVIEW(dev)) {
3369                 fp = (1 << clock->n) << 16 | clock->m1 << 8 | clock->m2;
3370                 if (reduced_clock)
3371                         fp2 = (1 << reduced_clock->n) << 16 |
3372                                 reduced_clock->m1 << 8 | reduced_clock->m2;
3373         } else {
3374                 fp = clock->n << 16 | clock->m1 << 8 | clock->m2;
3375                 if (reduced_clock)
3376                         fp2 = reduced_clock->n << 16 | reduced_clock->m1 << 8 |
3377                                 reduced_clock->m2;
3378         }
3379
3380         I915_WRITE(FP0(pipe), fp);
3381
3382         intel_crtc->lowfreq_avail = false;
3383         if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
3384             reduced_clock && i915_powersave) {
3385                 I915_WRITE(FP1(pipe), fp2);
3386                 intel_crtc->lowfreq_avail = true;
3387         } else {
3388                 I915_WRITE(FP1(pipe), fp);
3389         }
3390 }
3391
3392 static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
3393                               struct drm_display_mode *mode,
3394                               struct drm_display_mode *adjusted_mode,
3395                               int x, int y,
3396                               struct drm_framebuffer *old_fb)
3397 {
3398         struct drm_device *dev = crtc->dev;
3399         struct drm_i915_private *dev_priv = dev->dev_private;
3400         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3401         int pipe = intel_crtc->pipe;
3402         int plane = intel_crtc->plane;
3403         int refclk, num_connectors = 0;
3404         intel_clock_t clock, reduced_clock;
3405         u32 dpll, dspcntr, pipeconf, vsyncshift;
3406         bool ok, has_reduced_clock = false, is_sdvo = false, is_dvo = false;
3407         bool is_crt = false, is_lvds = false, is_tv = false, is_dp = false;
3408         struct drm_mode_config *mode_config = &dev->mode_config;
3409         struct intel_encoder *encoder;
3410         const intel_limit_t *limit;
3411         int ret;
3412         u32 temp;
3413         u32 lvds_sync = 0;
3414
3415         list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
3416                 if (encoder->base.crtc != crtc)
3417                         continue;
3418
3419                 switch (encoder->type) {
3420                 case INTEL_OUTPUT_LVDS:
3421                         is_lvds = true;
3422                         break;
3423                 case INTEL_OUTPUT_SDVO:
3424                 case INTEL_OUTPUT_HDMI:
3425                         is_sdvo = true;
3426                         if (encoder->needs_tv_clock)
3427                                 is_tv = true;
3428                         break;
3429                 case INTEL_OUTPUT_DVO:
3430                         is_dvo = true;
3431                         break;
3432                 case INTEL_OUTPUT_TVOUT:
3433                         is_tv = true;
3434                         break;
3435                 case INTEL_OUTPUT_ANALOG:
3436                         is_crt = true;
3437                         break;
3438                 case INTEL_OUTPUT_DISPLAYPORT:
3439                         is_dp = true;
3440                         break;
3441                 }
3442
3443                 num_connectors++;
3444         }
3445
3446         refclk = i9xx_get_refclk(crtc, num_connectors);
3447
3448         /*
3449          * Returns a set of divisors for the desired target clock with the given
3450          * refclk, or false.  The returned values represent the clock equation:
3451          * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
3452          */
3453         limit = intel_limit(crtc, refclk);
3454         ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, NULL,
3455                              &clock);
3456         if (!ok) {
3457                 DRM_ERROR("Couldn't find PLL settings for mode!\n");
3458                 return -EINVAL;
3459         }
3460
3461         /* Ensure that the cursor is valid for the new mode before changing... */
3462         intel_crtc_update_cursor(crtc, true);
3463
3464         if (is_lvds && dev_priv->lvds_downclock_avail) {
3465                 /*
3466                  * Ensure we match the reduced clock's P to the target clock.
3467                  * If the clocks don't match, we can't switch the display clock
3468                  * by using the FP0/FP1. In such case we will disable the LVDS
3469                  * downclock feature.
3470                 */
3471                 has_reduced_clock = limit->find_pll(limit, crtc,
3472                                                     dev_priv->lvds_downclock,
3473                                                     refclk,
3474                                                     &clock,
3475                                                     &reduced_clock);
3476         }
3477
3478         if (is_sdvo && is_tv)
3479                 i9xx_adjust_sdvo_tv_clock(adjusted_mode, &clock);
3480
3481         i9xx_update_pll_dividers(crtc, &clock, has_reduced_clock ?
3482                                  &reduced_clock : NULL);
3483
3484         dpll = DPLL_VGA_MODE_DIS;
3485
3486         if (!IS_GEN2(dev)) {
3487                 if (is_lvds)
3488                         dpll |= DPLLB_MODE_LVDS;
3489                 else
3490                         dpll |= DPLLB_MODE_DAC_SERIAL;
3491                 if (is_sdvo) {
3492                         int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
3493                         if (pixel_multiplier > 1) {
3494                                 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
3495                                         dpll |= (pixel_multiplier - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
3496                         }
3497                         dpll |= DPLL_DVO_HIGH_SPEED;
3498                 }
3499                 if (is_dp)
3500                         dpll |= DPLL_DVO_HIGH_SPEED;
3501
3502                 /* compute bitmask from p1 value */
3503                 if (IS_PINEVIEW(dev))
3504                         dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
3505                 else {
3506                         dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
3507                         if (IS_G4X(dev) && has_reduced_clock)
3508                                 dpll |= (1 << (reduced_clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
3509                 }
3510                 switch (clock.p2) {
3511                 case 5:
3512                         dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
3513                         break;
3514                 case 7:
3515                         dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
3516                         break;
3517                 case 10:
3518                         dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
3519                         break;
3520                 case 14:
3521                         dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
3522                         break;
3523                 }
3524                 if (INTEL_INFO(dev)->gen >= 4)
3525                         dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
3526         } else {
3527                 if (is_lvds) {
3528                         dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
3529                 } else {
3530                         if (clock.p1 == 2)
3531                                 dpll |= PLL_P1_DIVIDE_BY_TWO;
3532                         else
3533                                 dpll |= (clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
3534                         if (clock.p2 == 4)
3535                                 dpll |= PLL_P2_DIVIDE_BY_4;
3536                 }
3537         }
3538
3539         if (is_sdvo && is_tv)
3540                 dpll |= PLL_REF_INPUT_TVCLKINBC;
3541         else if (is_tv)
3542                 /* XXX: just matching BIOS for now */
3543                 /*      dpll |= PLL_REF_INPUT_TVCLKINBC; */
3544                 dpll |= 3;
3545         else if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
3546                 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
3547         else
3548                 dpll |= PLL_REF_INPUT_DREFCLK;
3549
3550         /* setup pipeconf */
3551         pipeconf = I915_READ(PIPECONF(pipe));
3552
3553         /* Set up the display plane register */
3554         dspcntr = DISPPLANE_GAMMA_ENABLE;
3555
3556         if (pipe == 0)
3557                 dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
3558         else
3559                 dspcntr |= DISPPLANE_SEL_PIPE_B;
3560
3561         if (pipe == 0 && INTEL_INFO(dev)->gen < 4) {
3562                 /* Enable pixel doubling when the dot clock is > 90% of the (display)
3563                  * core speed.
3564                  *
3565                  * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
3566                  * pipe == 0 check?
3567                  */
3568                 if (mode->clock >
3569                     dev_priv->display.get_display_clock_speed(dev) * 9 / 10)
3570                         pipeconf |= PIPECONF_DOUBLE_WIDE;
3571                 else
3572                         pipeconf &= ~PIPECONF_DOUBLE_WIDE;
3573         }
3574
3575         /* default to 8bpc */
3576         pipeconf &= ~(PIPECONF_BPP_MASK | PIPECONF_DITHER_EN);
3577         if (is_dp) {
3578                 if (mode->private_flags & INTEL_MODE_DP_FORCE_6BPC) {
3579                         pipeconf |= PIPECONF_BPP_6 |
3580                                     PIPECONF_DITHER_EN |
3581                                     PIPECONF_DITHER_TYPE_SP;
3582                 }
3583         }
3584
3585         dpll |= DPLL_VCO_ENABLE;
3586
3587         DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
3588         drm_mode_debug_printmodeline(mode);
3589
3590         I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
3591
3592         POSTING_READ(DPLL(pipe));
3593         DELAY(150);
3594
3595         /* The LVDS pin pair needs to be on before the DPLLs are enabled.
3596          * This is an exception to the general rule that mode_set doesn't turn
3597          * things on.
3598          */
3599         if (is_lvds) {
3600                 temp = I915_READ(LVDS);
3601                 temp |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
3602                 if (pipe == 1) {
3603                         temp |= LVDS_PIPEB_SELECT;
3604                 } else {
3605                         temp &= ~LVDS_PIPEB_SELECT;
3606                 }
3607                 /* set the corresponsding LVDS_BORDER bit */
3608                 temp |= dev_priv->lvds_border_bits;
3609                 /* Set the B0-B3 data pairs corresponding to whether we're going to
3610                  * set the DPLLs for dual-channel mode or not.
3611                  */
3612                 if (clock.p2 == 7)
3613                         temp |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
3614                 else
3615                         temp &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
3616
3617                 /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
3618                  * appropriately here, but we need to look more thoroughly into how
3619                  * panels behave in the two modes.
3620                  */
3621                 /* set the dithering flag on LVDS as needed */
3622                 if (INTEL_INFO(dev)->gen >= 4) {
3623                         if (dev_priv->lvds_dither)
3624                                 temp |= LVDS_ENABLE_DITHER;
3625                         else
3626                                 temp &= ~LVDS_ENABLE_DITHER;
3627                 }
3628                 if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
3629                         lvds_sync |= LVDS_HSYNC_POLARITY;
3630                 if (adjusted_mode->flags & DRM_MODE_FLAG_NVSYNC)
3631                         lvds_sync |= LVDS_VSYNC_POLARITY;
3632                 if ((temp & (LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY))
3633                     != lvds_sync) {
3634                         char flags[2] = "-+";
3635                         DRM_INFO("Changing LVDS panel from "
3636                                  "(%chsync, %cvsync) to (%chsync, %cvsync)\n",
3637                                  flags[!(temp & LVDS_HSYNC_POLARITY)],
3638                                  flags[!(temp & LVDS_VSYNC_POLARITY)],
3639                                  flags[!(lvds_sync & LVDS_HSYNC_POLARITY)],
3640                                  flags[!(lvds_sync & LVDS_VSYNC_POLARITY)]);
3641                         temp &= ~(LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY);
3642                         temp |= lvds_sync;
3643                 }
3644                 I915_WRITE(LVDS, temp);
3645         }
3646
3647         if (is_dp) {
3648                 intel_dp_set_m_n(crtc, mode, adjusted_mode);
3649         }
3650
3651         I915_WRITE(DPLL(pipe), dpll);
3652
3653         /* Wait for the clocks to stabilize. */
3654         POSTING_READ(DPLL(pipe));
3655         DELAY(150);
3656
3657         if (INTEL_INFO(dev)->gen >= 4) {
3658                 temp = 0;
3659                 if (is_sdvo) {
3660                         temp = intel_mode_get_pixel_multiplier(adjusted_mode);
3661                         if (temp > 1)
3662                                 temp = (temp - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
3663                         else
3664                                 temp = 0;
3665                 }
3666                 I915_WRITE(DPLL_MD(pipe), temp);
3667         } else {
3668                 /* The pixel multiplier can only be updated once the
3669                  * DPLL is enabled and the clocks are stable.
3670                  *
3671                  * So write it again.
3672                  */
3673                 I915_WRITE(DPLL(pipe), dpll);
3674         }
3675
3676         if (HAS_PIPE_CXSR(dev)) {
3677                 if (intel_crtc->lowfreq_avail) {
3678                         DRM_DEBUG_KMS("enabling CxSR downclocking\n");
3679                         pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
3680                 } else {
3681                         DRM_DEBUG_KMS("disabling CxSR downclocking\n");
3682                         pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
3683                 }
3684         }
3685
3686         pipeconf &= ~PIPECONF_INTERLACE_MASK;
3687         if (!IS_GEN2(dev) &&
3688             adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
3689                 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
3690                 /* the chip adds 2 halflines automatically */
3691                 adjusted_mode->crtc_vtotal -= 1;
3692                 adjusted_mode->crtc_vblank_end -= 1;
3693                 vsyncshift = adjusted_mode->crtc_hsync_start
3694                              - adjusted_mode->crtc_htotal/2;
3695         } else {
3696                 pipeconf |= PIPECONF_PROGRESSIVE;
3697                 vsyncshift = 0;
3698         }
3699
3700         if (!IS_GEN3(dev))
3701                 I915_WRITE(VSYNCSHIFT(pipe), vsyncshift);
3702
3703         I915_WRITE(HTOTAL(pipe),
3704                    (adjusted_mode->crtc_hdisplay - 1) |
3705                    ((adjusted_mode->crtc_htotal - 1) << 16));
3706         I915_WRITE(HBLANK(pipe),
3707                    (adjusted_mode->crtc_hblank_start - 1) |
3708                    ((adjusted_mode->crtc_hblank_end - 1) << 16));
3709         I915_WRITE(HSYNC(pipe),
3710                    (adjusted_mode->crtc_hsync_start - 1) |
3711                    ((adjusted_mode->crtc_hsync_end - 1) << 16));
3712
3713         I915_WRITE(VTOTAL(pipe),
3714                    (adjusted_mode->crtc_vdisplay - 1) |
3715                    ((adjusted_mode->crtc_vtotal - 1) << 16));
3716         I915_WRITE(VBLANK(pipe),
3717                    (adjusted_mode->crtc_vblank_start - 1) |
3718                    ((adjusted_mode->crtc_vblank_end - 1) << 16));
3719         I915_WRITE(VSYNC(pipe),
3720                    (adjusted_mode->crtc_vsync_start - 1) |
3721                    ((adjusted_mode->crtc_vsync_end - 1) << 16));
3722
3723         /* pipesrc and dspsize control the size that is scaled from,
3724          * which should always be the user's requested size.
3725          */
3726         I915_WRITE(DSPSIZE(plane),
3727                    ((mode->vdisplay - 1) << 16) |
3728                    (mode->hdisplay - 1));
3729         I915_WRITE(DSPPOS(plane), 0);
3730         I915_WRITE(PIPESRC(pipe),
3731                    ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
3732
3733         I915_WRITE(PIPECONF(pipe), pipeconf);
3734         POSTING_READ(PIPECONF(pipe));
3735         intel_enable_pipe(dev_priv, pipe, false);
3736
3737         intel_wait_for_vblank(dev, pipe);
3738
3739         I915_WRITE(DSPCNTR(plane), dspcntr);
3740         POSTING_READ(DSPCNTR(plane));
3741         intel_enable_plane(dev_priv, plane, pipe);
3742
3743         ret = intel_pipe_set_base(crtc, x, y, old_fb);
3744
3745         intel_update_watermarks(dev);
3746
3747         return ret;
3748 }
3749
3750 /*
3751  * Initialize reference clocks when the driver loads
3752  */
3753 void ironlake_init_pch_refclk(struct drm_device *dev)
3754 {
3755         struct drm_i915_private *dev_priv = dev->dev_private;
3756         struct drm_mode_config *mode_config = &dev->mode_config;
3757         struct intel_encoder *encoder;
3758         u32 temp;
3759         bool has_lvds = false;
3760         bool has_cpu_edp = false;
3761         bool has_pch_edp = false;
3762         bool has_panel = false;
3763         bool has_ck505 = false;
3764         bool can_ssc = false;
3765
3766         /* We need to take the global config into account */
3767         list_for_each_entry(encoder, &mode_config->encoder_list,
3768                             base.head) {
3769                 switch (encoder->type) {
3770                 case INTEL_OUTPUT_LVDS:
3771                         has_panel = true;
3772                         has_lvds = true;
3773                         break;
3774                 case INTEL_OUTPUT_EDP:
3775                         has_panel = true;
3776                         if (intel_encoder_is_pch_edp(&encoder->base))
3777                                 has_pch_edp = true;
3778                         else
3779                                 has_cpu_edp = true;
3780                         break;
3781                 }
3782         }
3783
3784         if (HAS_PCH_IBX(dev)) {
3785                 has_ck505 = dev_priv->display_clock_mode;
3786                 can_ssc = has_ck505;
3787         } else {
3788                 has_ck505 = false;
3789                 can_ssc = true;
3790         }
3791
3792         DRM_DEBUG_KMS("has_panel %d has_lvds %d has_pch_edp %d has_cpu_edp %d has_ck505 %d\n",
3793                       has_panel, has_lvds, has_pch_edp, has_cpu_edp,
3794                       has_ck505);
3795
3796         /* Ironlake: try to setup display ref clock before DPLL
3797          * enabling. This is only under driver's control after
3798          * PCH B stepping, previous chipset stepping should be
3799          * ignoring this setting.
3800          */
3801         temp = I915_READ(PCH_DREF_CONTROL);
3802         /* Always enable nonspread source */
3803         temp &= ~DREF_NONSPREAD_SOURCE_MASK;
3804
3805         if (has_ck505)
3806                 temp |= DREF_NONSPREAD_CK505_ENABLE;
3807         else
3808                 temp |= DREF_NONSPREAD_SOURCE_ENABLE;
3809
3810         if (has_panel) {
3811                 temp &= ~DREF_SSC_SOURCE_MASK;
3812                 temp |= DREF_SSC_SOURCE_ENABLE;
3813
3814                 /* SSC must be turned on before enabling the CPU output  */
3815                 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
3816                         DRM_DEBUG_KMS("Using SSC on panel\n");
3817                         temp |= DREF_SSC1_ENABLE;
3818                 } else
3819                         temp &= ~DREF_SSC1_ENABLE;
3820
3821                 /* Get SSC going before enabling the outputs */
3822                 I915_WRITE(PCH_DREF_CONTROL, temp);
3823                 POSTING_READ(PCH_DREF_CONTROL);
3824                 DELAY(200);
3825
3826                 temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
3827
3828                 /* Enable CPU source on CPU attached eDP */
3829                 if (has_cpu_edp) {
3830                         if (intel_panel_use_ssc(dev_priv) && can_ssc) {
3831                                 DRM_DEBUG_KMS("Using SSC on eDP\n");
3832                                 temp |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
3833                         }
3834                         else
3835                                 temp |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
3836                 } else
3837                         temp |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
3838
3839                 I915_WRITE(PCH_DREF_CONTROL, temp);
3840                 POSTING_READ(PCH_DREF_CONTROL);
3841                 DELAY(200);
3842         } else {
3843                 DRM_DEBUG_KMS("Disabling SSC entirely\n");
3844
3845                 temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
3846
3847                 /* Turn off CPU output */
3848                 temp |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
3849
3850                 I915_WRITE(PCH_DREF_CONTROL, temp);
3851                 POSTING_READ(PCH_DREF_CONTROL);
3852                 DELAY(200);
3853
3854                 /* Turn off the SSC source */
3855                 temp &= ~DREF_SSC_SOURCE_MASK;
3856                 temp |= DREF_SSC_SOURCE_DISABLE;
3857
3858                 /* Turn off SSC1 */
3859                 temp &= ~ DREF_SSC1_ENABLE;
3860
3861                 I915_WRITE(PCH_DREF_CONTROL, temp);
3862                 POSTING_READ(PCH_DREF_CONTROL);
3863                 DELAY(200);
3864         }
3865 }
3866
3867 static int ironlake_get_refclk(struct drm_crtc *crtc)
3868 {
3869         struct drm_device *dev = crtc->dev;
3870         struct drm_i915_private *dev_priv = dev->dev_private;
3871         struct intel_encoder *encoder;
3872         struct drm_mode_config *mode_config = &dev->mode_config;
3873         struct intel_encoder *edp_encoder = NULL;
3874         int num_connectors = 0;
3875         bool is_lvds = false;
3876
3877         list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
3878                 if (encoder->base.crtc != crtc)
3879                         continue;
3880
3881                 switch (encoder->type) {
3882                 case INTEL_OUTPUT_LVDS:
3883                         is_lvds = true;
3884                         break;
3885                 case INTEL_OUTPUT_EDP:
3886                         edp_encoder = encoder;
3887                         break;
3888                 }
3889                 num_connectors++;
3890         }
3891
3892         if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
3893                 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
3894                               dev_priv->lvds_ssc_freq);
3895                 return dev_priv->lvds_ssc_freq * 1000;
3896         }
3897
3898         return 120000;
3899 }
3900
3901 static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
3902                                   struct drm_display_mode *mode,
3903                                   struct drm_display_mode *adjusted_mode,
3904                                   int x, int y,
3905                                   struct drm_framebuffer *old_fb)
3906 {
3907         struct drm_device *dev = crtc->dev;
3908         struct drm_i915_private *dev_priv = dev->dev_private;
3909         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3910         int pipe = intel_crtc->pipe;
3911         int plane = intel_crtc->plane;
3912         int refclk, num_connectors = 0;
3913         intel_clock_t clock, reduced_clock;
3914         u32 dpll, fp = 0, fp2 = 0, dspcntr, pipeconf;
3915         bool ok, has_reduced_clock = false, is_sdvo = false;
3916         bool is_crt = false, is_lvds = false, is_tv = false, is_dp = false;
3917         struct intel_encoder *has_edp_encoder = NULL;
3918         struct drm_mode_config *mode_config = &dev->mode_config;
3919         struct intel_encoder *encoder;
3920         const intel_limit_t *limit;
3921         int ret;
3922         struct fdi_m_n m_n = {0};
3923         u32 temp;
3924         u32 lvds_sync = 0;
3925         int target_clock, pixel_multiplier, lane, link_bw, factor;
3926         unsigned int pipe_bpp;
3927         bool dither;
3928
3929         list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
3930                 if (encoder->base.crtc != crtc)
3931                         continue;
3932
3933                 switch (encoder->type) {
3934                 case INTEL_OUTPUT_LVDS:
3935                         is_lvds = true;
3936                         break;
3937                 case INTEL_OUTPUT_SDVO:
3938                 case INTEL_OUTPUT_HDMI:
3939                         is_sdvo = true;
3940                         if (encoder->needs_tv_clock)
3941                                 is_tv = true;
3942                         break;
3943                 case INTEL_OUTPUT_TVOUT:
3944                         is_tv = true;
3945                         break;
3946                 case INTEL_OUTPUT_ANALOG:
3947                         is_crt = true;
3948                         break;
3949                 case INTEL_OUTPUT_DISPLAYPORT:
3950                         is_dp = true;
3951                         break;
3952                 case INTEL_OUTPUT_EDP:
3953                         has_edp_encoder = encoder;
3954                         break;
3955                 }
3956
3957                 num_connectors++;
3958         }
3959
3960         refclk = ironlake_get_refclk(crtc);
3961
3962         /*
3963          * Returns a set of divisors for the desired target clock with the given
3964          * refclk, or false.  The returned values represent the clock equation:
3965          * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
3966          */
3967         limit = intel_limit(crtc, refclk);
3968         ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, NULL,
3969                              &clock);
3970         if (!ok) {
3971                 DRM_ERROR("Couldn't find PLL settings for mode!\n");
3972                 return -EINVAL;
3973         }
3974
3975         /* Ensure that the cursor is valid for the new mode before changing... */
3976         intel_crtc_update_cursor(crtc, true);
3977
3978         if (is_lvds && dev_priv->lvds_downclock_avail) {
3979                 /*
3980                  * Ensure we match the reduced clock's P to the target clock.
3981                  * If the clocks don't match, we can't switch the display clock
3982                  * by using the FP0/FP1. In such case we will disable the LVDS
3983                  * downclock feature.
3984                 */
3985                 has_reduced_clock = limit->find_pll(limit, crtc,
3986                                                     dev_priv->lvds_downclock,
3987                                                     refclk,
3988                                                     &clock,
3989                                                     &reduced_clock);
3990         }
3991         /* SDVO TV has fixed PLL values depend on its clock range,
3992            this mirrors vbios setting. */
3993         if (is_sdvo && is_tv) {
3994                 if (adjusted_mode->clock >= 100000
3995                     && adjusted_mode->clock < 140500) {
3996                         clock.p1 = 2;
3997                         clock.p2 = 10;
3998                         clock.n = 3;
3999                         clock.m1 = 16;
4000                         clock.m2 = 8;
4001                 } else if (adjusted_mode->clock >= 140500
4002                            && adjusted_mode->clock <= 200000) {
4003                         clock.p1 = 1;
4004                         clock.p2 = 10;
4005                         clock.n = 6;
4006                         clock.m1 = 12;
4007                         clock.m2 = 8;
4008                 }
4009         }
4010
4011         /* FDI link */
4012         pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
4013         lane = 0;
4014         /* CPU eDP doesn't require FDI link, so just set DP M/N
4015            according to current link config */
4016         if (has_edp_encoder &&
4017             !intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
4018                 target_clock = mode->clock;
4019                 intel_edp_link_config(has_edp_encoder,
4020                                       &lane, &link_bw);
4021         } else {
4022                 /* [e]DP over FDI requires target mode clock
4023                    instead of link clock */
4024                 if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base))
4025                         target_clock = mode->clock;
4026                 else
4027                         target_clock = adjusted_mode->clock;
4028
4029                 /* FDI is a binary signal running at ~2.7GHz, encoding
4030                  * each output octet as 10 bits. The actual frequency
4031                  * is stored as a divider into a 100MHz clock, and the
4032                  * mode pixel clock is stored in units of 1KHz.
4033                  * Hence the bw of each lane in terms of the mode signal
4034                  * is:
4035                  */
4036                 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
4037         }
4038
4039         /* determine panel color depth */
4040         temp = I915_READ(PIPECONF(pipe));
4041         temp &= ~PIPE_BPC_MASK;
4042         dither = intel_choose_pipe_bpp_dither(crtc, &pipe_bpp, mode);
4043         switch (pipe_bpp) {
4044         case 18:
4045                 temp |= PIPE_6BPC;
4046                 break;
4047         case 24:
4048                 temp |= PIPE_8BPC;
4049                 break;
4050         case 30:
4051                 temp |= PIPE_10BPC;
4052                 break;
4053         case 36:
4054                 temp |= PIPE_12BPC;
4055                 break;
4056         default:
4057                 kprintf("intel_choose_pipe_bpp returned invalid value %d\n",
4058                         pipe_bpp);
4059                 temp |= PIPE_8BPC;
4060                 pipe_bpp = 24;
4061                 break;
4062         }
4063
4064         intel_crtc->bpp = pipe_bpp;
4065         I915_WRITE(PIPECONF(pipe), temp);
4066
4067         if (!lane) {
4068                 /*
4069                  * Account for spread spectrum to avoid
4070                  * oversubscribing the link. Max center spread
4071                  * is 2.5%; use 5% for safety's sake.
4072                  */
4073                 u32 bps = target_clock * intel_crtc->bpp * 21 / 20;
4074                 lane = bps / (link_bw * 8) + 1;
4075         }
4076
4077         intel_crtc->fdi_lanes = lane;
4078
4079         if (pixel_multiplier > 1)
4080                 link_bw *= pixel_multiplier;
4081         ironlake_compute_m_n(intel_crtc->bpp, lane, target_clock, link_bw,
4082                              &m_n);
4083
4084         fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
4085         if (has_reduced_clock)
4086                 fp2 = reduced_clock.n << 16 | reduced_clock.m1 << 8 |
4087                         reduced_clock.m2;
4088
4089         /* Enable autotuning of the PLL clock (if permissible) */
4090         factor = 21;
4091         if (is_lvds) {
4092                 if ((intel_panel_use_ssc(dev_priv) &&
4093                      dev_priv->lvds_ssc_freq == 100) ||
4094                     (I915_READ(PCH_LVDS) & LVDS_CLKB_POWER_MASK) == LVDS_CLKB_POWER_UP)
4095                         factor = 25;
4096         } else if (is_sdvo && is_tv)
4097                 factor = 20;
4098
4099         if (clock.m < factor * clock.n)
4100                 fp |= FP_CB_TUNE;
4101
4102         dpll = 0;
4103
4104         if (is_lvds)
4105                 dpll |= DPLLB_MODE_LVDS;
4106         else
4107                 dpll |= DPLLB_MODE_DAC_SERIAL;
4108         if (is_sdvo) {
4109                 int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
4110                 if (pixel_multiplier > 1) {
4111                         dpll |= (pixel_multiplier - 1) << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
4112                 }
4113                 dpll |= DPLL_DVO_HIGH_SPEED;
4114         }
4115         if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base))
4116                 dpll |= DPLL_DVO_HIGH_SPEED;
4117
4118         /* compute bitmask from p1 value */
4119         dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4120         /* also FPA1 */
4121         dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
4122
4123         switch (clock.p2) {
4124         case 5:
4125                 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
4126                 break;
4127         case 7:
4128                 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
4129                 break;
4130         case 10:
4131                 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
4132                 break;
4133         case 14:
4134                 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
4135                 break;
4136         }
4137
4138         if (is_sdvo && is_tv)
4139                 dpll |= PLL_REF_INPUT_TVCLKINBC;
4140         else if (is_tv)
4141                 /* XXX: just matching BIOS for now */
4142                 /*      dpll |= PLL_REF_INPUT_TVCLKINBC; */
4143                 dpll |= 3;
4144         else if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
4145                 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4146         else
4147                 dpll |= PLL_REF_INPUT_DREFCLK;
4148
4149         /* setup pipeconf */
4150         pipeconf = I915_READ(PIPECONF(pipe));
4151
4152         /* Set up the display plane register */
4153         dspcntr = DISPPLANE_GAMMA_ENABLE;
4154
4155         DRM_DEBUG_KMS("Mode for pipe %d:\n", pipe);
4156         drm_mode_debug_printmodeline(mode);
4157
4158         /* PCH eDP needs FDI, but CPU eDP does not */
4159         if (!intel_crtc->no_pll) {
4160                 if (!has_edp_encoder ||
4161                     intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
4162                         I915_WRITE(_PCH_FP0(pipe), fp);
4163                         I915_WRITE(_PCH_DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
4164
4165                         POSTING_READ(_PCH_DPLL(pipe));
4166                         DELAY(150);
4167                 }
4168         } else {
4169                 if (dpll == (I915_READ(_PCH_DPLL(0)) & 0x7fffffff) &&
4170                     fp == I915_READ(_PCH_FP0(0))) {
4171                         intel_crtc->use_pll_a = true;
4172                         DRM_DEBUG_KMS("using pipe a dpll\n");
4173                 } else if (dpll == (I915_READ(_PCH_DPLL(1)) & 0x7fffffff) &&
4174                            fp == I915_READ(_PCH_FP0(1))) {
4175                         intel_crtc->use_pll_a = false;
4176                         DRM_DEBUG_KMS("using pipe b dpll\n");
4177                 } else {
4178                         DRM_DEBUG_KMS("no matching PLL configuration for pipe 2\n");
4179                         return -EINVAL;
4180                 }
4181         }
4182
4183         /* The LVDS pin pair needs to be on before the DPLLs are enabled.
4184          * This is an exception to the general rule that mode_set doesn't turn
4185          * things on.
4186          */
4187         if (is_lvds) {
4188                 temp = I915_READ(PCH_LVDS);
4189                 temp |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
4190                 if (HAS_PCH_CPT(dev)) {
4191                         temp &= ~PORT_TRANS_SEL_MASK;
4192                         temp |= PORT_TRANS_SEL_CPT(pipe);
4193                 } else {
4194                         if (pipe == 1)
4195                                 temp |= LVDS_PIPEB_SELECT;
4196                         else
4197                                 temp &= ~LVDS_PIPEB_SELECT;
4198                 }
4199
4200                 /* set the corresponsding LVDS_BORDER bit */
4201                 temp |= dev_priv->lvds_border_bits;
4202                 /* Set the B0-B3 data pairs corresponding to whether we're going to
4203                  * set the DPLLs for dual-channel mode or not.
4204                  */
4205                 if (clock.p2 == 7)
4206                         temp |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
4207                 else
4208                         temp &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
4209
4210                 /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
4211                  * appropriately here, but we need to look more thoroughly into how
4212                  * panels behave in the two modes.
4213                  */
4214                 if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
4215                         lvds_sync |= LVDS_HSYNC_POLARITY;
4216                 if (adjusted_mode->flags & DRM_MODE_FLAG_NVSYNC)
4217                         lvds_sync |= LVDS_VSYNC_POLARITY;
4218                 if ((temp & (LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY))
4219                     != lvds_sync) {
4220                         char flags[2] = "-+";
4221                         DRM_INFO("Changing LVDS panel from "
4222                                  "(%chsync, %cvsync) to (%chsync, %cvsync)\n",
4223                                  flags[!(temp & LVDS_HSYNC_POLARITY)],
4224                                  flags[!(temp & LVDS_VSYNC_POLARITY)],
4225                                  flags[!(lvds_sync & LVDS_HSYNC_POLARITY)],
4226                                  flags[!(lvds_sync & LVDS_VSYNC_POLARITY)]);
4227                         temp &= ~(LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY);
4228                         temp |= lvds_sync;
4229                 }
4230                 I915_WRITE(PCH_LVDS, temp);
4231         }
4232
4233         pipeconf &= ~PIPECONF_DITHER_EN;
4234         pipeconf &= ~PIPECONF_DITHER_TYPE_MASK;
4235         if ((is_lvds && dev_priv->lvds_dither) || dither) {
4236                 pipeconf |= PIPECONF_DITHER_EN;
4237                 pipeconf |= PIPECONF_DITHER_TYPE_SP;
4238         }
4239         if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
4240                 intel_dp_set_m_n(crtc, mode, adjusted_mode);
4241         } else {
4242                 /* For non-DP output, clear any trans DP clock recovery setting.*/
4243                 I915_WRITE(TRANSDATA_M1(pipe), 0);
4244                 I915_WRITE(TRANSDATA_N1(pipe), 0);
4245                 I915_WRITE(TRANSDPLINK_M1(pipe), 0);
4246                 I915_WRITE(TRANSDPLINK_N1(pipe), 0);
4247         }
4248
4249         if (!intel_crtc->no_pll &&
4250             (!has_edp_encoder ||
4251              intel_encoder_is_pch_edp(&has_edp_encoder->base))) {
4252                 I915_WRITE(_PCH_DPLL(pipe), dpll);
4253
4254                 /* Wait for the clocks to stabilize. */
4255                 POSTING_READ(_PCH_DPLL(pipe));
4256                 DELAY(150);
4257
4258                 /* The pixel multiplier can only be updated once the
4259                  * DPLL is enabled and the clocks are stable.
4260                  *
4261                  * So write it again.
4262                  */
4263                 I915_WRITE(_PCH_DPLL(pipe), dpll);
4264         }
4265
4266         intel_crtc->lowfreq_avail = false;
4267         if (!intel_crtc->no_pll) {
4268                 if (is_lvds && has_reduced_clock && i915_powersave) {
4269                         I915_WRITE(_PCH_FP1(pipe), fp2);
4270                         intel_crtc->lowfreq_avail = true;
4271                         if (HAS_PIPE_CXSR(dev)) {
4272                                 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
4273                                 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
4274                         }
4275                 } else {
4276                         I915_WRITE(_PCH_FP1(pipe), fp);
4277                         if (HAS_PIPE_CXSR(dev)) {
4278                                 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
4279                                 pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
4280                         }
4281                 }
4282         }
4283
4284         pipeconf &= ~PIPECONF_INTERLACE_MASK;
4285         if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
4286                 pipeconf |= PIPECONF_INTERLACED_ILK;
4287                 /* the chip adds 2 halflines automatically */
4288                 adjusted_mode->crtc_vtotal -= 1;
4289                 adjusted_mode->crtc_vblank_end -= 1;
4290                 I915_WRITE(VSYNCSHIFT(pipe),
4291                            adjusted_mode->crtc_hsync_start
4292                            - adjusted_mode->crtc_htotal/2);
4293         } else {
4294                 pipeconf |= PIPECONF_PROGRESSIVE;
4295                 I915_WRITE(VSYNCSHIFT(pipe), 0);
4296         }
4297
4298         I915_WRITE(HTOTAL(pipe),
4299                    (adjusted_mode->crtc_hdisplay - 1) |
4300                    ((adjusted_mode->crtc_htotal - 1) << 16));
4301         I915_WRITE(HBLANK(pipe),
4302                    (adjusted_mode->crtc_hblank_start - 1) |
4303                    ((adjusted_mode->crtc_hblank_end - 1) << 16));
4304         I915_WRITE(HSYNC(pipe),
4305                    (adjusted_mode->crtc_hsync_start - 1) |
4306                    ((adjusted_mode->crtc_hsync_end - 1) << 16));
4307
4308         I915_WRITE(VTOTAL(pipe),
4309                    (adjusted_mode->crtc_vdisplay - 1) |
4310                    ((adjusted_mode->crtc_vtotal - 1) << 16));
4311         I915_WRITE(VBLANK(pipe),
4312                    (adjusted_mode->crtc_vblank_start - 1) |
4313                    ((adjusted_mode->crtc_vblank_end - 1) << 16));
4314         I915_WRITE(VSYNC(pipe),
4315                    (adjusted_mode->crtc_vsync_start - 1) |
4316                    ((adjusted_mode->crtc_vsync_end - 1) << 16));
4317
4318         /* pipesrc controls the size that is scaled from, which should
4319          * always be the user's requested size.
4320          */
4321         I915_WRITE(PIPESRC(pipe),
4322                    ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
4323
4324         I915_WRITE(PIPE_DATA_M1(pipe), TU_SIZE(m_n.tu) | m_n.gmch_m);
4325         I915_WRITE(PIPE_DATA_N1(pipe), m_n.gmch_n);
4326         I915_WRITE(PIPE_LINK_M1(pipe), m_n.link_m);
4327         I915_WRITE(PIPE_LINK_N1(pipe), m_n.link_n);
4328
4329         if (has_edp_encoder &&
4330             !intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
4331                 ironlake_set_pll_edp(crtc, adjusted_mode->clock);
4332         }
4333
4334         I915_WRITE(PIPECONF(pipe), pipeconf);
4335         POSTING_READ(PIPECONF(pipe));
4336
4337         intel_wait_for_vblank(dev, pipe);
4338
4339         I915_WRITE(DSPCNTR(plane), dspcntr);
4340         POSTING_READ(DSPCNTR(plane));
4341
4342         ret = intel_pipe_set_base(crtc, x, y, old_fb);
4343
4344         intel_update_watermarks(dev);
4345
4346         return ret;
4347 }
4348
4349 static int intel_crtc_mode_set(struct drm_crtc *crtc,
4350                                struct drm_display_mode *mode,
4351                                struct drm_display_mode *adjusted_mode,
4352                                int x, int y,
4353                                struct drm_framebuffer *old_fb)
4354 {
4355         struct drm_device *dev = crtc->dev;
4356         struct drm_i915_private *dev_priv = dev->dev_private;
4357         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4358         int pipe = intel_crtc->pipe;
4359         int ret;
4360
4361         drm_vblank_pre_modeset(dev, pipe);
4362
4363         ret = dev_priv->display.crtc_mode_set(crtc, mode, adjusted_mode,
4364                                               x, y, old_fb);
4365         drm_vblank_post_modeset(dev, pipe);
4366
4367         if (ret)
4368                 intel_crtc->dpms_mode = DRM_MODE_DPMS_OFF;
4369         else
4370                 intel_crtc->dpms_mode = DRM_MODE_DPMS_ON;
4371
4372         return ret;
4373 }
4374
4375 static bool intel_eld_uptodate(struct drm_connector *connector,
4376                                int reg_eldv, uint32_t bits_eldv,
4377                                int reg_elda, uint32_t bits_elda,
4378                                int reg_edid)
4379 {
4380         struct drm_i915_private *dev_priv = connector->dev->dev_private;
4381         uint8_t *eld = connector->eld;
4382         uint32_t i;
4383
4384         i = I915_READ(reg_eldv);
4385         i &= bits_eldv;
4386
4387         if (!eld[0])
4388                 return !i;
4389
4390         if (!i)
4391                 return false;
4392
4393         i = I915_READ(reg_elda);
4394         i &= ~bits_elda;
4395         I915_WRITE(reg_elda, i);
4396
4397         for (i = 0; i < eld[2]; i++)
4398                 if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
4399                         return false;
4400
4401         return true;
4402 }
4403
4404 static void g4x_write_eld(struct drm_connector *connector,
4405                           struct drm_crtc *crtc)
4406 {
4407         struct drm_i915_private *dev_priv = connector->dev->dev_private;
4408         uint8_t *eld = connector->eld;
4409         uint32_t eldv;
4410         uint32_t len;
4411         uint32_t i;
4412
4413         i = I915_READ(G4X_AUD_VID_DID);
4414
4415         if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
4416                 eldv = G4X_ELDV_DEVCL_DEVBLC;
4417         else
4418                 eldv = G4X_ELDV_DEVCTG;
4419
4420         if (intel_eld_uptodate(connector,
4421                                G4X_AUD_CNTL_ST, eldv,
4422                                G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
4423                                G4X_HDMIW_HDMIEDID))
4424                 return;
4425
4426         i = I915_READ(G4X_AUD_CNTL_ST);
4427         i &= ~(eldv | G4X_ELD_ADDR);
4428         len = (i >> 9) & 0x1f;          /* ELD buffer size */
4429         I915_WRITE(G4X_AUD_CNTL_ST, i);
4430
4431         if (!eld[0])
4432                 return;
4433
4434         if (eld[2] < (uint8_t)len)
4435                 len = eld[2];
4436         DRM_DEBUG_KMS("ELD size %d\n", len);
4437         for (i = 0; i < len; i++)
4438                 I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
4439
4440         i = I915_READ(G4X_AUD_CNTL_ST);
4441         i |= eldv;
4442         I915_WRITE(G4X_AUD_CNTL_ST, i);
4443 }
4444
4445 static void ironlake_write_eld(struct drm_connector *connector,
4446                                      struct drm_crtc *crtc)
4447 {
4448         struct drm_i915_private *dev_priv = connector->dev->dev_private;
4449         uint8_t *eld = connector->eld;
4450         uint32_t eldv;
4451         uint32_t i;
4452         int len;
4453         int hdmiw_hdmiedid;
4454         int aud_config;
4455         int aud_cntl_st;
4456         int aud_cntrl_st2;
4457
4458         if (HAS_PCH_IBX(connector->dev)) {
4459                 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID_A;
4460                 aud_config = IBX_AUD_CONFIG_A;
4461                 aud_cntl_st = IBX_AUD_CNTL_ST_A;
4462                 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
4463         } else {
4464                 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID_A;
4465                 aud_config = CPT_AUD_CONFIG_A;
4466                 aud_cntl_st = CPT_AUD_CNTL_ST_A;
4467                 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
4468         }
4469
4470         i = to_intel_crtc(crtc)->pipe;
4471         hdmiw_hdmiedid += i * 0x100;
4472         aud_cntl_st += i * 0x100;
4473         aud_config += i * 0x100;
4474
4475         DRM_DEBUG_KMS("ELD on pipe %c\n", pipe_name(i));
4476
4477         i = I915_READ(aud_cntl_st);
4478         i = (i >> 29) & 0x3;            /* DIP_Port_Select, 0x1 = PortB */
4479         if (!i) {
4480                 DRM_DEBUG_KMS("Audio directed to unknown port\n");
4481                 /* operate blindly on all ports */
4482                 eldv = IBX_ELD_VALIDB;
4483                 eldv |= IBX_ELD_VALIDB << 4;
4484                 eldv |= IBX_ELD_VALIDB << 8;
4485         } else {
4486                 DRM_DEBUG_KMS("ELD on port %c\n", 'A' + i);
4487                 eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
4488         }
4489
4490         if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
4491                 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
4492                 eld[5] |= (1 << 2);     /* Conn_Type, 0x1 = DisplayPort */
4493                 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
4494         } else
4495                 I915_WRITE(aud_config, 0);
4496
4497         if (intel_eld_uptodate(connector,
4498                                aud_cntrl_st2, eldv,
4499                                aud_cntl_st, IBX_ELD_ADDRESS,
4500                                hdmiw_hdmiedid))
4501                 return;
4502
4503         i = I915_READ(aud_cntrl_st2);
4504         i &= ~eldv;
4505         I915_WRITE(aud_cntrl_st2, i);
4506
4507         if (!eld[0])
4508                 return;
4509
4510         i = I915_READ(aud_cntl_st);
4511         i &= ~IBX_ELD_ADDRESS;
4512         I915_WRITE(aud_cntl_st, i);
4513
4514         /* 84 bytes of hw ELD buffer */
4515         len = 21;
4516         if (eld[2] < (uint8_t)len)
4517                 len = eld[2];
4518         DRM_DEBUG_KMS("ELD size %d\n", len);
4519         for (i = 0; i < len; i++)
4520                 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
4521
4522         i = I915_READ(aud_cntrl_st2);
4523         i |= eldv;
4524         I915_WRITE(aud_cntrl_st2, i);
4525 }
4526
4527 void intel_write_eld(struct drm_encoder *encoder,
4528                      struct drm_display_mode *mode)
4529 {
4530         struct drm_crtc *crtc = encoder->crtc;
4531         struct drm_connector *connector;
4532         struct drm_device *dev = encoder->dev;
4533         struct drm_i915_private *dev_priv = dev->dev_private;
4534
4535         connector = drm_select_eld(encoder, mode);
4536         if (!connector)
4537                 return;
4538
4539         DRM_DEBUG_KMS("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
4540                          connector->base.id,
4541                          drm_get_connector_name(connector),
4542                          connector->encoder->base.id,
4543                          drm_get_encoder_name(connector->encoder));
4544
4545         connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
4546
4547         if (dev_priv->display.write_eld)
4548                 dev_priv->display.write_eld(connector, crtc);
4549 }
4550
4551 /** Loads the palette/gamma unit for the CRTC with the prepared values */
4552 void intel_crtc_load_lut(struct drm_crtc *crtc)
4553 {
4554         struct drm_device *dev = crtc->dev;
4555         struct drm_i915_private *dev_priv = dev->dev_private;
4556         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4557         int palreg = PALETTE(intel_crtc->pipe);
4558         int i;
4559
4560         /* The clocks have to be on to load the palette. */
4561         if (!crtc->enabled || !intel_crtc->active)
4562                 return;
4563
4564         /* use legacy palette for Ironlake */
4565         if (HAS_PCH_SPLIT(dev))
4566                 palreg = LGC_PALETTE(intel_crtc->pipe);
4567
4568         for (i = 0; i < 256; i++) {
4569                 I915_WRITE(palreg + 4 * i,
4570                            (intel_crtc->lut_r[i] << 16) |
4571                            (intel_crtc->lut_g[i] << 8) |
4572                            intel_crtc->lut_b[i]);
4573         }
4574 }
4575
4576 static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
4577 {
4578         struct drm_device *dev = crtc->dev;
4579         struct drm_i915_private *dev_priv = dev->dev_private;
4580         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4581         bool visible = base != 0;
4582         u32 cntl;
4583
4584         if (intel_crtc->cursor_visible == visible)
4585                 return;
4586
4587         cntl = I915_READ(_CURACNTR);
4588         if (visible) {
4589                 /* On these chipsets we can only modify the base whilst
4590                  * the cursor is disabled.
4591                  */
4592                 I915_WRITE(_CURABASE, base);
4593
4594                 cntl &= ~(CURSOR_FORMAT_MASK);
4595                 /* XXX width must be 64, stride 256 => 0x00 << 28 */
4596                 cntl |= CURSOR_ENABLE |
4597                         CURSOR_GAMMA_ENABLE |
4598                         CURSOR_FORMAT_ARGB;
4599         } else
4600                 cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
4601         I915_WRITE(_CURACNTR, cntl);
4602
4603         intel_crtc->cursor_visible = visible;
4604 }
4605
4606 static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
4607 {
4608         struct drm_device *dev = crtc->dev;
4609         struct drm_i915_private *dev_priv = dev->dev_private;
4610         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4611         int pipe = intel_crtc->pipe;
4612         bool visible = base != 0;
4613
4614         if (intel_crtc->cursor_visible != visible) {
4615                 uint32_t cntl = I915_READ(CURCNTR(pipe));
4616                 if (base) {
4617                         cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
4618                         cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
4619                         cntl |= pipe << 28; /* Connect to correct pipe */
4620                 } else {
4621                         cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
4622                         cntl |= CURSOR_MODE_DISABLE;
4623                 }
4624                 I915_WRITE(CURCNTR(pipe), cntl);
4625
4626                 intel_crtc->cursor_visible = visible;
4627         }
4628         /* and commit changes on next vblank */
4629         I915_WRITE(CURBASE(pipe), base);
4630 }
4631
4632 static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
4633 {
4634         struct drm_device *dev = crtc->dev;
4635         struct drm_i915_private *dev_priv = dev->dev_private;
4636         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4637         int pipe = intel_crtc->pipe;
4638         bool visible = base != 0;
4639
4640         if (intel_crtc->cursor_visible != visible) {
4641                 uint32_t cntl = I915_READ(CURCNTR_IVB(pipe));
4642                 if (base) {
4643                         cntl &= ~CURSOR_MODE;
4644                         cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
4645                 } else {
4646                         cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
4647                         cntl |= CURSOR_MODE_DISABLE;
4648                 }
4649                 I915_WRITE(CURCNTR_IVB(pipe), cntl);
4650
4651                 intel_crtc->cursor_visible = visible;
4652         }
4653         /* and commit changes on next vblank */
4654         I915_WRITE(CURBASE_IVB(pipe), base);
4655 }
4656
4657 /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
4658 static void intel_crtc_update_cursor(struct drm_crtc *crtc,
4659                                      bool on)
4660 {
4661         struct drm_device *dev = crtc->dev;
4662         struct drm_i915_private *dev_priv = dev->dev_private;
4663         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4664         int pipe = intel_crtc->pipe;
4665         int x = intel_crtc->cursor_x;
4666         int y = intel_crtc->cursor_y;
4667         u32 base, pos;
4668         bool visible;
4669
4670         pos = 0;
4671
4672         if (on && crtc->enabled && crtc->fb) {
4673                 base = intel_crtc->cursor_addr;
4674                 if (x > (int) crtc->fb->width)
4675                         base = 0;
4676
4677                 if (y > (int) crtc->fb->height)
4678                         base = 0;
4679         } else
4680                 base = 0;
4681
4682         if (x < 0) {
4683                 if (x + intel_crtc->cursor_width < 0)
4684                         base = 0;
4685
4686                 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
4687                 x = -x;
4688         }
4689         pos |= x << CURSOR_X_SHIFT;
4690
4691         if (y < 0) {
4692                 if (y + intel_crtc->cursor_height < 0)
4693                         base = 0;
4694
4695                 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
4696                 y = -y;
4697         }
4698         pos |= y << CURSOR_Y_SHIFT;
4699
4700         visible = base != 0;
4701         if (!visible && !intel_crtc->cursor_visible)
4702                 return;
4703
4704         if (IS_IVYBRIDGE(dev)) {
4705                 I915_WRITE(CURPOS_IVB(pipe), pos);
4706                 ivb_update_cursor(crtc, base);
4707         } else {
4708                 I915_WRITE(CURPOS(pipe), pos);
4709                 if (IS_845G(dev) || IS_I865G(dev))
4710                         i845_update_cursor(crtc, base);
4711                 else
4712                         i9xx_update_cursor(crtc, base);
4713         }
4714 }
4715
4716 static int intel_crtc_cursor_set(struct drm_crtc *crtc,
4717                                  struct drm_file *file,
4718                                  uint32_t handle,
4719                                  uint32_t width, uint32_t height)
4720 {
4721         struct drm_device *dev = crtc->dev;
4722         struct drm_i915_private *dev_priv = dev->dev_private;
4723         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4724         struct drm_i915_gem_object *obj;
4725         uint32_t addr;
4726         int ret;
4727
4728         DRM_DEBUG_KMS("\n");
4729
4730         /* if we want to turn off the cursor ignore width and height */
4731         if (!handle) {
4732                 DRM_DEBUG_KMS("cursor off\n");
4733                 addr = 0;
4734                 obj = NULL;
4735                 DRM_LOCK(dev);
4736                 goto finish;
4737         }
4738
4739         /* Currently we only support 64x64 cursors */
4740         if (width != 64 || height != 64) {
4741                 DRM_ERROR("we currently only support 64x64 cursors\n");
4742                 return -EINVAL;
4743         }
4744
4745         obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
4746         if (&obj->base == NULL)
4747                 return -ENOENT;
4748
4749         if (obj->base.size < width * height * 4) {
4750                 DRM_ERROR("buffer is to small\n");
4751                 ret = -ENOMEM;
4752                 goto fail;
4753         }
4754
4755         /* we only need to pin inside GTT if cursor is non-phy */
4756         DRM_LOCK(dev);
4757         if (!dev_priv->info->cursor_needs_physical) {
4758                 if (obj->tiling_mode) {
4759                         DRM_ERROR("cursor cannot be tiled\n");
4760                         ret = -EINVAL;
4761                         goto fail_locked;
4762                 }
4763
4764                 ret = i915_gem_object_pin_to_display_plane(obj, 0, NULL);
4765                 if (ret) {
4766                         DRM_ERROR("failed to move cursor bo into the GTT\n");
4767                         goto fail_locked;
4768                 }
4769
4770                 ret = i915_gem_object_put_fence(obj);
4771                 if (ret) {
4772                         DRM_ERROR("failed to release fence for cursor\n");
4773                         goto fail_unpin;
4774                 }
4775
4776                 addr = obj->gtt_offset;
4777         } else {
4778                 int align = IS_I830(dev) ? 16 * 1024 : 256;
4779                 ret = i915_gem_attach_phys_object(dev, obj,
4780                                                   (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
4781                                                   align);
4782                 if (ret) {
4783                         DRM_ERROR("failed to attach phys object\n");
4784                         goto fail_locked;
4785                 }
4786                 addr = obj->phys_obj->handle->busaddr;
4787         }
4788
4789         if (IS_GEN2(dev))
4790                 I915_WRITE(CURSIZE, (height << 12) | width);
4791
4792  finish:
4793         if (intel_crtc->cursor_bo) {
4794                 if (dev_priv->info->cursor_needs_physical) {
4795                         if (intel_crtc->cursor_bo != obj)
4796                                 i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
4797                 } else
4798                         i915_gem_object_unpin(intel_crtc->cursor_bo);
4799                 drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
4800         }
4801
4802         DRM_UNLOCK(dev);
4803
4804         intel_crtc->cursor_addr = addr;
4805         intel_crtc->cursor_bo = obj;
4806         intel_crtc->cursor_width = width;
4807         intel_crtc->cursor_height = height;
4808
4809         intel_crtc_update_cursor(crtc, true);
4810
4811         return 0;
4812 fail_unpin:
4813         i915_gem_object_unpin(obj);
4814 fail_locked:
4815         DRM_UNLOCK(dev);
4816 fail:
4817         drm_gem_object_unreference_unlocked(&obj->base);
4818         return ret;
4819 }
4820
4821 static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
4822 {
4823         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4824
4825         intel_crtc->cursor_x = x;
4826         intel_crtc->cursor_y = y;
4827
4828         intel_crtc_update_cursor(crtc, true);
4829
4830         return 0;
4831 }
4832
4833 /** Sets the color ramps on behalf of RandR */
4834 void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
4835                                  u16 blue, int regno)
4836 {
4837         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4838
4839         intel_crtc->lut_r[regno] = red >> 8;
4840         intel_crtc->lut_g[regno] = green >> 8;
4841         intel_crtc->lut_b[regno] = blue >> 8;
4842 }
4843
4844 void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
4845                              u16 *blue, int regno)
4846 {
4847         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4848
4849         *red = intel_crtc->lut_r[regno] << 8;
4850         *green = intel_crtc->lut_g[regno] << 8;
4851         *blue = intel_crtc->lut_b[regno] << 8;
4852 }
4853
4854 static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
4855                                  u16 *blue, uint32_t start, uint32_t size)
4856 {
4857         int end = (start + size > 256) ? 256 : start + size, i;
4858         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4859
4860         for (i = start; i < end; i++) {
4861                 intel_crtc->lut_r[i] = red[i] >> 8;
4862                 intel_crtc->lut_g[i] = green[i] >> 8;
4863                 intel_crtc->lut_b[i] = blue[i] >> 8;
4864         }
4865
4866         intel_crtc_load_lut(crtc);
4867 }
4868
4869 /**
4870  * Get a pipe with a simple mode set on it for doing load-based monitor
4871  * detection.
4872  *
4873  * It will be up to the load-detect code to adjust the pipe as appropriate for
4874  * its requirements.  The pipe will be connected to no other encoders.
4875  *
4876  * Currently this code will only succeed if there is a pipe with no encoders
4877  * configured for it.  In the future, it could choose to temporarily disable
4878  * some outputs to free up a pipe for its use.
4879  *
4880  * \return crtc, or NULL if no pipes are available.
4881  */
4882
4883 /* VESA 640x480x72Hz mode to set on the pipe */
4884 static struct drm_display_mode load_detect_mode = {
4885         DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
4886                  704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
4887 };
4888
4889 static struct drm_framebuffer *
4890 intel_framebuffer_create(struct drm_device *dev,
4891                          struct drm_mode_fb_cmd2 *mode_cmd,
4892                          struct drm_i915_gem_object *obj)
4893 {
4894         struct intel_framebuffer *intel_fb;
4895         int ret;
4896
4897         intel_fb = kmalloc(sizeof(*intel_fb), DRM_MEM_KMS, M_WAITOK | M_ZERO);
4898         if (!intel_fb) {
4899                 drm_gem_object_unreference_unlocked(&obj->base);
4900                 return ERR_PTR(-ENOMEM);
4901         }
4902
4903         ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
4904         if (ret) {
4905                 drm_gem_object_unreference_unlocked(&obj->base);
4906                 kfree(intel_fb, DRM_MEM_KMS);
4907                 return ERR_PTR(ret);
4908         }
4909
4910         return &intel_fb->base;
4911 }
4912
4913 static u32
4914 intel_framebuffer_pitch_for_width(int width, int bpp)
4915 {
4916         u32 pitch = howmany(width * bpp, 8);
4917         return roundup2(pitch, 64);
4918 }
4919
4920 static u32
4921 intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
4922 {
4923         u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
4924         return roundup2(pitch * mode->vdisplay, PAGE_SIZE);
4925 }
4926
4927 static struct drm_framebuffer *
4928 intel_framebuffer_create_for_mode(struct drm_device *dev,
4929                                   struct drm_display_mode *mode,
4930                                   int depth, int bpp)
4931 {
4932         struct drm_i915_gem_object *obj;
4933         struct drm_mode_fb_cmd2 mode_cmd = { 0 };
4934
4935         obj = i915_gem_alloc_object(dev,
4936                                     intel_framebuffer_size_for_mode(mode, bpp));
4937         if (obj == NULL)
4938                 return ERR_PTR(-ENOMEM);
4939
4940         mode_cmd.width = mode->hdisplay;
4941         mode_cmd.height = mode->vdisplay;
4942         mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
4943                                                                 bpp);
4944         mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
4945
4946         return intel_framebuffer_create(dev, &mode_cmd, obj);
4947 }
4948
4949 static int
4950 mode_fits_in_fbdev(struct drm_device *dev,
4951     struct drm_display_mode *mode, struct drm_framebuffer **res)
4952 {
4953         struct drm_i915_private *dev_priv = dev->dev_private;
4954         struct drm_i915_gem_object *obj;
4955         struct drm_framebuffer *fb;
4956
4957         if (dev_priv->fbdev == NULL) {
4958                 *res = NULL;
4959                 return (0);
4960         }
4961
4962         obj = dev_priv->fbdev->ifb.obj;
4963         if (obj == NULL) {
4964                 *res = NULL;
4965                 return (0);
4966         }
4967
4968         fb = &dev_priv->fbdev->ifb.base;
4969         if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
4970             fb->bits_per_pixel)) {
4971                 *res = NULL;
4972                 return (0);
4973         }
4974
4975         if (obj->base.size < mode->vdisplay * fb->pitches[0]) {
4976                 *res = NULL;
4977                 return (0);
4978         }
4979
4980         *res = fb;
4981         return (0);
4982 }
4983
4984 bool intel_get_load_detect_pipe(struct intel_encoder *intel_encoder,
4985                                 struct drm_connector *connector,
4986                                 struct drm_display_mode *mode,
4987                                 struct intel_load_detect_pipe *old)
4988 {
4989         struct intel_crtc *intel_crtc;
4990         struct drm_crtc *possible_crtc;
4991         struct drm_encoder *encoder = &intel_encoder->base;
4992         struct drm_crtc *crtc = NULL;
4993         struct drm_device *dev = encoder->dev;
4994         struct drm_framebuffer *old_fb;
4995         int i = -1, r;
4996
4997         DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
4998                       connector->base.id, drm_get_connector_name(connector),
4999                       encoder->base.id, drm_get_encoder_name(encoder));
5000
5001         /*
5002          * Algorithm gets a little messy:
5003          *
5004          *   - if the connector already has an assigned crtc, use it (but make
5005          *     sure it's on first)
5006          *
5007          *   - try to find the first unused crtc that can drive this connector,
5008          *     and use that if we find one
5009          */
5010
5011         /* See if we already have a CRTC for this connector */
5012         if (encoder->crtc) {
5013                 crtc = encoder->crtc;
5014
5015                 intel_crtc = to_intel_crtc(crtc);
5016                 old->dpms_mode = intel_crtc->dpms_mode;
5017                 old->load_detect_temp = false;
5018
5019                 /* Make sure the crtc and connector are running */
5020                 if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
5021                         struct drm_encoder_helper_funcs *encoder_funcs;
5022                         struct drm_crtc_helper_funcs *crtc_funcs;
5023
5024                         crtc_funcs = crtc->helper_private;
5025                         crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
5026
5027                         encoder_funcs = encoder->helper_private;
5028                         encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
5029                 }
5030
5031                 return true;
5032         }
5033
5034         /* Find an unused one (if possible) */
5035         list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
5036                 i++;
5037                 if (!(encoder->possible_crtcs & (1 << i)))
5038                         continue;
5039                 if (!possible_crtc->enabled) {
5040                         crtc = possible_crtc;
5041                         break;
5042                 }
5043         }
5044
5045         /*
5046          * If we didn't find an unused CRTC, don't use any.
5047          */
5048         if (!crtc) {
5049                 DRM_DEBUG_KMS("no pipe available for load-detect\n");
5050                 return false;
5051         }
5052
5053         encoder->crtc = crtc;
5054         connector->encoder = encoder;
5055
5056         intel_crtc = to_intel_crtc(crtc);
5057         old->dpms_mode = intel_crtc->dpms_mode;
5058         old->load_detect_temp = true;
5059         old->release_fb = NULL;
5060
5061         if (!mode)
5062                 mode = &load_detect_mode;
5063
5064         old_fb = crtc->fb;
5065
5066         /* We need a framebuffer large enough to accommodate all accesses
5067          * that the plane may generate whilst we perform load detection.
5068          * We can not rely on the fbcon either being present (we get called
5069          * during its initialisation to detect all boot displays, or it may
5070          * not even exist) or that it is large enough to satisfy the
5071          * requested mode.
5072          */
5073         r = mode_fits_in_fbdev(dev, mode, &crtc->fb);
5074         if (crtc->fb == NULL) {
5075                 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
5076                 crtc->fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
5077                 old->release_fb = crtc->fb;
5078         } else
5079                 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
5080         if (IS_ERR(crtc->fb)) {
5081                 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
5082                 return false;
5083         }
5084
5085         if (!drm_crtc_helper_set_mode(crtc, mode, 0, 0, old_fb)) {
5086                 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
5087                 if (old->release_fb)
5088                         old->release_fb->funcs->destroy(old->release_fb);
5089                 crtc->fb = old_fb;
5090                 return false;
5091         }
5092
5093         /* let the connector get through one full cycle before testing */
5094         intel_wait_for_vblank(dev, intel_crtc->pipe);
5095
5096         return true;
5097 }
5098
5099 void intel_release_load_detect_pipe(struct intel_encoder *intel_encoder,
5100                                     struct drm_connector *connector,
5101                                     struct intel_load_detect_pipe *old)
5102 {
5103         struct drm_encoder *encoder = &intel_encoder->base;
5104         struct drm_device *dev = encoder->dev;
5105         struct drm_crtc *crtc = encoder->crtc;
5106         struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
5107         struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
5108
5109         DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
5110                       connector->base.id, drm_get_connector_name(connector),
5111                       encoder->base.id, drm_get_encoder_name(encoder));
5112
5113         if (old->load_detect_temp) {
5114                 connector->encoder = NULL;
5115                 drm_helper_disable_unused_functions(dev);
5116
5117                 if (old->release_fb)
5118                         old->release_fb->funcs->destroy(old->release_fb);
5119
5120                 return;
5121         }
5122
5123         /* Switch crtc and encoder back off if necessary */
5124         if (old->dpms_mode != DRM_MODE_DPMS_ON) {
5125                 encoder_funcs->dpms(encoder, old->dpms_mode);
5126                 crtc_funcs->dpms(crtc, old->dpms_mode);
5127         }
5128 }
5129
5130 /* Returns the clock of the currently programmed mode of the given pipe. */
5131 static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
5132 {
5133         struct drm_i915_private *dev_priv = dev->dev_private;
5134         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5135         int pipe = intel_crtc->pipe;
5136         u32 dpll = I915_READ(DPLL(pipe));
5137         u32 fp;
5138         intel_clock_t clock;
5139
5140         if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
5141                 fp = I915_READ(FP0(pipe));
5142         else
5143                 fp = I915_READ(FP1(pipe));
5144
5145         clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
5146         if (IS_PINEVIEW(dev)) {
5147                 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
5148                 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
5149         } else {
5150                 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
5151                 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
5152         }
5153
5154         if (!IS_GEN2(dev)) {
5155                 if (IS_PINEVIEW(dev))
5156                         clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
5157                                 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
5158                 else
5159                         clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
5160                                DPLL_FPA01_P1_POST_DIV_SHIFT);
5161
5162                 switch (dpll & DPLL_MODE_MASK) {
5163                 case DPLLB_MODE_DAC_SERIAL:
5164                         clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
5165                                 5 : 10;
5166                         break;
5167                 case DPLLB_MODE_LVDS:
5168                         clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
5169                                 7 : 14;
5170                         break;
5171                 default:
5172                         DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
5173                                   "mode\n", (int)(dpll & DPLL_MODE_MASK));
5174                         return 0;
5175                 }
5176
5177                 /* XXX: Handle the 100Mhz refclk */
5178                 intel_clock(dev, 96000, &clock);
5179         } else {
5180                 bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
5181
5182                 if (is_lvds) {
5183                         clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
5184                                        DPLL_FPA01_P1_POST_DIV_SHIFT);
5185                         clock.p2 = 14;
5186
5187                         if ((dpll & PLL_REF_INPUT_MASK) ==
5188                             PLLB_REF_INPUT_SPREADSPECTRUMIN) {
5189                                 /* XXX: might not be 66MHz */
5190                                 intel_clock(dev, 66000, &clock);
5191                         } else
5192                                 intel_clock(dev, 48000, &clock);
5193                 } else {
5194                         if (dpll & PLL_P1_DIVIDE_BY_TWO)
5195                                 clock.p1 = 2;
5196                         else {
5197                                 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
5198                                             DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
5199                         }
5200                         if (dpll & PLL_P2_DIVIDE_BY_4)
5201                                 clock.p2 = 4;
5202                         else
5203                                 clock.p2 = 2;
5204
5205                         intel_clock(dev, 48000, &clock);
5206                 }
5207         }
5208
5209         /* XXX: It would be nice to validate the clocks, but we can't reuse
5210          * i830PllIsValid() because it relies on the xf86_config connector
5211          * configuration being accurate, which it isn't necessarily.
5212          */
5213
5214         return clock.dot;
5215 }
5216
5217 /** Returns the currently programmed mode of the given pipe. */
5218 struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
5219                                              struct drm_crtc *crtc)
5220 {
5221         struct drm_i915_private *dev_priv = dev->dev_private;
5222         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5223         int pipe = intel_crtc->pipe;
5224         struct drm_display_mode *mode;
5225         int htot = I915_READ(HTOTAL(pipe));
5226         int hsync = I915_READ(HSYNC(pipe));
5227         int vtot = I915_READ(VTOTAL(pipe));
5228         int vsync = I915_READ(VSYNC(pipe));
5229
5230         mode = kmalloc(sizeof(*mode), DRM_MEM_KMS, M_WAITOK | M_ZERO);
5231
5232         mode->clock = intel_crtc_clock_get(dev, crtc);
5233         mode->hdisplay = (htot & 0xffff) + 1;
5234         mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
5235         mode->hsync_start = (hsync & 0xffff) + 1;
5236         mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
5237         mode->vdisplay = (vtot & 0xffff) + 1;
5238         mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
5239         mode->vsync_start = (vsync & 0xffff) + 1;
5240         mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
5241
5242         drm_mode_set_name(mode);
5243         drm_mode_set_crtcinfo(mode, 0);
5244
5245         return mode;
5246 }
5247
5248 static void intel_increase_pllclock(struct drm_crtc *crtc)
5249 {
5250         struct drm_device *dev = crtc->dev;
5251         drm_i915_private_t *dev_priv = dev->dev_private;
5252         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5253         int pipe = intel_crtc->pipe;
5254         int dpll_reg = DPLL(pipe);
5255         int dpll;
5256
5257         if (HAS_PCH_SPLIT(dev))
5258                 return;
5259
5260         if (!dev_priv->lvds_downclock_avail)
5261                 return;
5262
5263         dpll = I915_READ(dpll_reg);
5264         if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
5265                 DRM_DEBUG_DRIVER("upclocking LVDS\n");
5266
5267                 assert_panel_unlocked(dev_priv, pipe);
5268
5269                 dpll &= ~DISPLAY_RATE_SELECT_FPA1;
5270                 I915_WRITE(dpll_reg, dpll);
5271                 intel_wait_for_vblank(dev, pipe);
5272
5273                 dpll = I915_READ(dpll_reg);
5274                 if (dpll & DISPLAY_RATE_SELECT_FPA1)
5275                         DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
5276         }
5277 }
5278
5279 static void intel_decrease_pllclock(struct drm_crtc *crtc)
5280 {
5281         struct drm_device *dev = crtc->dev;
5282         drm_i915_private_t *dev_priv = dev->dev_private;
5283         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5284
5285         if (HAS_PCH_SPLIT(dev))
5286                 return;
5287
5288         if (!dev_priv->lvds_downclock_avail)
5289                 return;
5290
5291         /*
5292          * Since this is called by a timer, we should never get here in
5293          * the manual case.
5294          */
5295         if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
5296                 int pipe = intel_crtc->pipe;
5297                 int dpll_reg = DPLL(pipe);
5298                 u32 dpll;
5299
5300                 DRM_DEBUG_DRIVER("downclocking LVDS\n");
5301
5302                 assert_panel_unlocked(dev_priv, pipe);
5303
5304                 dpll = I915_READ(dpll_reg);
5305                 dpll |= DISPLAY_RATE_SELECT_FPA1;
5306                 I915_WRITE(dpll_reg, dpll);
5307                 intel_wait_for_vblank(dev, pipe);
5308                 dpll = I915_READ(dpll_reg);
5309                 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
5310                         DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
5311         }
5312 }
5313
5314 void intel_mark_busy(struct drm_device *dev)
5315 {
5316         i915_update_gfx_val(dev->dev_private);
5317 }
5318
5319 void intel_mark_idle(struct drm_device *dev)
5320 {
5321         struct drm_crtc *crtc;
5322
5323         if (!i915_powersave)
5324                 return;
5325
5326         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
5327                 if (!crtc->fb)
5328                         continue;
5329
5330                 intel_decrease_pllclock(crtc);
5331         }
5332 }
5333
5334 static void intel_crtc_destroy(struct drm_crtc *crtc)
5335 {
5336         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5337         struct drm_device *dev = crtc->dev;
5338         struct intel_unpin_work *work;
5339
5340         lockmgr(&dev->event_lock, LK_EXCLUSIVE);
5341         work = intel_crtc->unpin_work;
5342         intel_crtc->unpin_work = NULL;
5343         lockmgr(&dev->event_lock, LK_RELEASE);
5344
5345         if (work) {
5346                 cancel_work_sync(&work->work);
5347                 kfree(work, DRM_MEM_KMS);
5348         }
5349
5350         drm_crtc_cleanup(crtc);
5351
5352         drm_free(intel_crtc, DRM_MEM_KMS);
5353 }
5354
5355 static void intel_unpin_work_fn(struct work_struct *__work)
5356 {
5357         struct intel_unpin_work *work =
5358                                 container_of(__work, struct intel_unpin_work, work);
5359         struct drm_device *dev;
5360
5361         dev = work->dev;
5362         DRM_LOCK(dev);
5363         intel_unpin_fb_obj(work->old_fb_obj);
5364         drm_gem_object_unreference(&work->pending_flip_obj->base);
5365         drm_gem_object_unreference(&work->old_fb_obj->base);
5366
5367         intel_update_fbc(work->dev);
5368         DRM_UNLOCK(dev);
5369         drm_free(work, DRM_MEM_KMS);
5370 }
5371
5372 static void do_intel_finish_page_flip(struct drm_device *dev,
5373                                       struct drm_crtc *crtc)
5374 {
5375         drm_i915_private_t *dev_priv = dev->dev_private;
5376         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5377         struct intel_unpin_work *work;
5378         struct drm_i915_gem_object *obj;
5379
5380         /* Ignore early vblank irqs */
5381         if (intel_crtc == NULL)
5382                 return;
5383
5384         lockmgr(&dev->event_lock, LK_EXCLUSIVE);
5385         work = intel_crtc->unpin_work;
5386         if (work == NULL || !atomic_read(&work->pending)) {
5387                 lockmgr(&dev->event_lock, LK_RELEASE);
5388                 return;
5389         }
5390
5391         intel_crtc->unpin_work = NULL;
5392
5393         if (work->event)
5394                 drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
5395
5396         drm_vblank_put(dev, intel_crtc->pipe);
5397
5398         lockmgr(&dev->event_lock, LK_RELEASE);
5399
5400         obj = work->old_fb_obj;
5401
5402         atomic_clear_mask(1 << intel_crtc->plane,
5403                           &obj->pending_flip.counter);
5404         wakeup(&obj->pending_flip);
5405
5406         queue_work(dev_priv->wq, &work->work);
5407 }
5408
5409 void intel_finish_page_flip(struct drm_device *dev, int pipe)
5410 {
5411         drm_i915_private_t *dev_priv = dev->dev_private;
5412         struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
5413
5414         do_intel_finish_page_flip(dev, crtc);
5415 }
5416
5417 void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
5418 {
5419         drm_i915_private_t *dev_priv = dev->dev_private;
5420         struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
5421
5422         do_intel_finish_page_flip(dev, crtc);
5423 }
5424
5425 void intel_prepare_page_flip(struct drm_device *dev, int plane)
5426 {
5427         drm_i915_private_t *dev_priv = dev->dev_private;
5428         struct intel_crtc *intel_crtc =
5429                 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
5430
5431         lockmgr(&dev->event_lock, LK_EXCLUSIVE);
5432         if (intel_crtc->unpin_work)
5433                 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
5434         lockmgr(&dev->event_lock, LK_RELEASE);
5435 }
5436
5437 static int intel_gen2_queue_flip(struct drm_device *dev,
5438                                  struct drm_crtc *crtc,
5439                                  struct drm_framebuffer *fb,
5440                                  struct drm_i915_gem_object *obj)
5441 {
5442         struct drm_i915_private *dev_priv = dev->dev_private;
5443         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5444         unsigned long offset;
5445         u32 flip_mask;
5446         int ret;
5447
5448         ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));
5449         if (ret)
5450                 goto out;
5451
5452         /* Offset into the new buffer for cases of shared fbs between CRTCs */
5453         offset = crtc->y * fb->pitches[0] + crtc->x * fb->bits_per_pixel/8;
5454
5455         ret = BEGIN_LP_RING(6);
5456         if (ret)
5457                 goto out;
5458
5459         /* Can't queue multiple flips, so wait for the previous
5460          * one to finish before executing the next.
5461          */
5462         if (intel_crtc->plane)
5463                 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
5464         else
5465                 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
5466         OUT_RING(MI_WAIT_FOR_EVENT | flip_mask);
5467         OUT_RING(MI_NOOP);
5468         OUT_RING(MI_DISPLAY_FLIP |
5469                  MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
5470         OUT_RING(fb->pitches[0]);
5471         OUT_RING(obj->gtt_offset + offset);
5472         OUT_RING(0); /* aux display base address, unused */
5473         ADVANCE_LP_RING();
5474 out:
5475         return ret;
5476 }
5477
5478 static int intel_gen3_queue_flip(struct drm_device *dev,
5479                                  struct drm_crtc *crtc,
5480                                  struct drm_framebuffer *fb,
5481                                  struct drm_i915_gem_object *obj)
5482 {
5483         struct drm_i915_private *dev_priv = dev->dev_private;
5484         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5485         unsigned long offset;
5486         u32 flip_mask;
5487         int ret;
5488
5489         ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));
5490         if (ret)
5491                 goto out;
5492
5493         /* Offset into the new buffer for cases of shared fbs between CRTCs */
5494         offset = crtc->y * fb->pitches[0] + crtc->x * fb->bits_per_pixel/8;
5495
5496         ret = BEGIN_LP_RING(6);
5497         if (ret)
5498                 goto out;
5499
5500         if (intel_crtc->plane)
5501                 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
5502         else
5503                 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
5504         OUT_RING(MI_WAIT_FOR_EVENT | flip_mask);
5505         OUT_RING(MI_NOOP);
5506         OUT_RING(MI_DISPLAY_FLIP_I915 |
5507                  MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
5508         OUT_RING(fb->pitches[0]);
5509         OUT_RING(obj->gtt_offset + offset);
5510         OUT_RING(MI_NOOP);
5511
5512         ADVANCE_LP_RING();
5513 out:
5514         return ret;
5515 }
5516
5517 static int intel_gen4_queue_flip(struct drm_device *dev,
5518                                  struct drm_crtc *crtc,
5519                                  struct drm_framebuffer *fb,
5520                                  struct drm_i915_gem_object *obj)
5521 {
5522         struct drm_i915_private *dev_priv = dev->dev_private;
5523         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5524         uint32_t pf, pipesrc;
5525         int ret;
5526
5527         ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));
5528         if (ret)
5529                 goto out;
5530
5531         ret = BEGIN_LP_RING(4);
5532         if (ret)
5533                 goto out;
5534
5535         /* i965+ uses the linear or tiled offsets from the
5536          * Display Registers (which do not change across a page-flip)
5537          * so we need only reprogram the base address.
5538          */
5539         OUT_RING(MI_DISPLAY_FLIP |
5540                  MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
5541         OUT_RING(fb->pitches[0]);
5542         OUT_RING(obj->gtt_offset | obj->tiling_mode);
5543
5544         /* XXX Enabling the panel-fitter across page-flip is so far
5545          * untested on non-native modes, so ignore it for now.
5546          * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
5547          */
5548         pf = 0;
5549         pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
5550         OUT_RING(pf | pipesrc);
5551         ADVANCE_LP_RING();
5552 out:
5553         return ret;
5554 }
5555
5556 static int intel_gen6_queue_flip(struct drm_device *dev,
5557                                  struct drm_crtc *crtc,
5558                                  struct drm_framebuffer *fb,
5559                                  struct drm_i915_gem_object *obj)
5560 {
5561         struct drm_i915_private *dev_priv = dev->dev_private;
5562         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5563         uint32_t pf, pipesrc;
5564         int ret;
5565
5566         ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));
5567         if (ret)
5568                 goto out;
5569
5570         ret = BEGIN_LP_RING(4);
5571         if (ret)
5572                 goto out;
5573
5574         OUT_RING(MI_DISPLAY_FLIP |
5575                  MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
5576         OUT_RING(fb->pitches[0] | obj->tiling_mode);
5577         OUT_RING(obj->gtt_offset);
5578
5579         /* Contrary to the suggestions in the documentation,
5580          * "Enable Panel Fitter" does not seem to be required when page
5581          * flipping with a non-native mode, and worse causes a normal
5582          * modeset to fail.
5583          * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
5584          */
5585         pf = 0;
5586         pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
5587         OUT_RING(pf | pipesrc);
5588         ADVANCE_LP_RING();
5589 out:
5590         return ret;
5591 }
5592
5593 /*
5594  * On gen7 we currently use the blit ring because (in early silicon at least)
5595  * the render ring doesn't give us interrpts for page flip completion, which
5596  * means clients will hang after the first flip is queued.  Fortunately the
5597  * blit ring generates interrupts properly, so use it instead.
5598  */
5599 static int intel_gen7_queue_flip(struct drm_device *dev,
5600                                  struct drm_crtc *crtc,
5601                                  struct drm_framebuffer *fb,
5602                                  struct drm_i915_gem_object *obj)
5603 {
5604         struct drm_i915_private *dev_priv = dev->dev_private;
5605         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5606         struct intel_ring_buffer *ring = &dev_priv->ring[BCS];
5607         int ret;
5608
5609         ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
5610         if (ret)
5611                 goto out;
5612
5613         ret = intel_ring_begin(ring, 4);
5614         if (ret)
5615                 goto out;
5616
5617         intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | (intel_crtc->plane << 19));
5618         intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
5619         intel_ring_emit(ring, (obj->gtt_offset));
5620         intel_ring_emit(ring, (MI_NOOP));
5621         intel_ring_advance(ring);
5622 out:
5623         return ret;
5624 }
5625
5626 static int intel_default_queue_flip(struct drm_device *dev,
5627                                     struct drm_crtc *crtc,
5628                                     struct drm_framebuffer *fb,
5629                                     struct drm_i915_gem_object *obj)
5630 {
5631         return -ENODEV;
5632 }
5633
5634 static int intel_crtc_page_flip(struct drm_crtc *crtc,
5635                                 struct drm_framebuffer *fb,
5636                                 struct drm_pending_vblank_event *event)
5637 {
5638         struct drm_device *dev = crtc->dev;
5639         struct drm_i915_private *dev_priv = dev->dev_private;
5640         struct intel_framebuffer *intel_fb;
5641         struct drm_i915_gem_object *obj;
5642         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5643         struct intel_unpin_work *work;
5644         int ret;
5645
5646         work = kmalloc(sizeof *work, DRM_MEM_KMS, M_WAITOK | M_ZERO);
5647
5648         work->event = event;
5649         work->dev = crtc->dev;
5650         intel_fb = to_intel_framebuffer(crtc->fb);
5651         work->old_fb_obj = intel_fb->obj;
5652         INIT_WORK(&work->work, intel_unpin_work_fn);
5653
5654         ret = drm_vblank_get(dev, intel_crtc->pipe);
5655         if (ret)
5656                 goto free_work;
5657
5658         /* We borrow the event spin lock for protecting unpin_work */
5659         lockmgr(&dev->event_lock, LK_EXCLUSIVE);
5660         if (intel_crtc->unpin_work) {
5661                 lockmgr(&dev->event_lock, LK_RELEASE);
5662                 drm_free(work, DRM_MEM_KMS);
5663                 drm_vblank_put(dev, intel_crtc->pipe);
5664
5665                 DRM_DEBUG("flip queue: crtc already busy\n");
5666                 return -EBUSY;
5667         }
5668         intel_crtc->unpin_work = work;
5669         lockmgr(&dev->event_lock, LK_RELEASE);
5670
5671         intel_fb = to_intel_framebuffer(fb);
5672         obj = intel_fb->obj;
5673
5674         DRM_LOCK(dev);
5675
5676         /* Reference the objects for the scheduled work. */
5677         drm_gem_object_reference(&work->old_fb_obj->base);
5678         drm_gem_object_reference(&obj->base);
5679
5680         crtc->fb = fb;
5681
5682         work->pending_flip_obj = obj;
5683
5684         work->enable_stall_check = true;
5685
5686         /* Block clients from rendering to the new back buffer until
5687          * the flip occurs and the object is no longer visible.
5688          */
5689         atomic_add(1 << intel_crtc->plane, &work->old_fb_obj->pending_flip);
5690
5691         ret = dev_priv->display.queue_flip(dev, crtc, fb, obj);
5692         if (ret)
5693                 goto cleanup_pending;
5694         intel_disable_fbc(dev);
5695         DRM_UNLOCK(dev);
5696
5697         return 0;
5698
5699 cleanup_pending:
5700         atomic_sub(1 << intel_crtc->plane, &work->old_fb_obj->pending_flip);
5701         drm_gem_object_unreference(&work->old_fb_obj->base);
5702         drm_gem_object_unreference(&obj->base);
5703         DRM_UNLOCK(dev);
5704
5705         lockmgr(&dev->event_lock, LK_EXCLUSIVE);
5706         intel_crtc->unpin_work = NULL;
5707         lockmgr(&dev->event_lock, LK_RELEASE);
5708
5709         drm_vblank_put(dev, intel_crtc->pipe);
5710 free_work:
5711         drm_free(work, DRM_MEM_KMS);
5712
5713         return ret;
5714 }
5715
5716 static void intel_sanitize_modesetting(struct drm_device *dev,
5717                                        int pipe, int plane)
5718 {
5719         struct drm_i915_private *dev_priv = dev->dev_private;
5720         u32 reg, val;
5721
5722         /* Clear any frame start delays used for debugging left by the BIOS */
5723         for_each_pipe(pipe) {
5724                 reg = PIPECONF(pipe);
5725                 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
5726         }
5727
5728         if (HAS_PCH_SPLIT(dev))
5729                 return;
5730
5731         /* Who knows what state these registers were left in by the BIOS or
5732          * grub?
5733          *
5734          * If we leave the registers in a conflicting state (e.g. with the
5735          * display plane reading from the other pipe than the one we intend
5736          * to use) then when we attempt to teardown the active mode, we will
5737          * not disable the pipes and planes in the correct order -- leaving
5738          * a plane reading from a disabled pipe and possibly leading to
5739          * undefined behaviour.
5740          */
5741
5742         reg = DSPCNTR(plane);
5743         val = I915_READ(reg);
5744
5745         if ((val & DISPLAY_PLANE_ENABLE) == 0)
5746                 return;
5747         if (!!(val & DISPPLANE_SEL_PIPE_MASK) == pipe)
5748                 return;
5749
5750         /* This display plane is active and attached to the other CPU pipe. */
5751         pipe = !pipe;
5752
5753         /* Disable the plane and wait for it to stop reading from the pipe. */
5754         intel_disable_plane(dev_priv, plane, pipe);
5755         intel_disable_pipe(dev_priv, pipe);
5756 }
5757
5758 static void intel_crtc_reset(struct drm_crtc *crtc)
5759 {
5760         struct drm_device *dev = crtc->dev;
5761         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5762
5763         /* Reset flags back to the 'unknown' status so that they
5764          * will be correctly set on the initial modeset.
5765          */
5766         intel_crtc->dpms_mode = -1;
5767
5768         /* We need to fix up any BIOS configuration that conflicts with
5769          * our expectations.
5770          */
5771         intel_sanitize_modesetting(dev, intel_crtc->pipe, intel_crtc->plane);
5772 }
5773
5774 static struct drm_crtc_helper_funcs intel_helper_funcs = {
5775         .dpms = intel_crtc_dpms,
5776         .mode_fixup = intel_crtc_mode_fixup,
5777         .mode_set = intel_crtc_mode_set,
5778         .mode_set_base = intel_pipe_set_base,
5779         .mode_set_base_atomic = intel_pipe_set_base_atomic,
5780         .load_lut = intel_crtc_load_lut,
5781         .disable = intel_crtc_disable,
5782 };
5783
5784 static const struct drm_crtc_funcs intel_crtc_funcs = {
5785         .reset = intel_crtc_reset,
5786         .cursor_set = intel_crtc_cursor_set,
5787         .cursor_move = intel_crtc_cursor_move,
5788         .gamma_set = intel_crtc_gamma_set,
5789         .set_config = drm_crtc_helper_set_config,
5790         .destroy = intel_crtc_destroy,
5791         .page_flip = intel_crtc_page_flip,
5792 };
5793
5794 static void intel_crtc_init(struct drm_device *dev, int pipe)
5795 {
5796         drm_i915_private_t *dev_priv = dev->dev_private;
5797         struct intel_crtc *intel_crtc;
5798         int i;
5799
5800         intel_crtc = kmalloc(sizeof(struct intel_crtc) +
5801             (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)),
5802             DRM_MEM_KMS, M_WAITOK | M_ZERO);
5803
5804         drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
5805
5806         drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
5807         for (i = 0; i < 256; i++) {
5808                 intel_crtc->lut_r[i] = i;
5809                 intel_crtc->lut_g[i] = i;
5810                 intel_crtc->lut_b[i] = i;
5811         }
5812
5813         /* Swap pipes & planes for FBC on pre-965 */
5814         intel_crtc->pipe = pipe;
5815         intel_crtc->plane = pipe;
5816         if (IS_MOBILE(dev) && IS_GEN3(dev)) {
5817                 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
5818                 intel_crtc->plane = !pipe;
5819         }
5820
5821         KASSERT(pipe < DRM_ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) &&
5822             dev_priv->plane_to_crtc_mapping[intel_crtc->plane] == NULL,
5823             ("plane_to_crtc is already initialized"));
5824         dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
5825         dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
5826
5827         intel_crtc_reset(&intel_crtc->base);
5828         intel_crtc->active = true; /* force the pipe off on setup_init_config */
5829         intel_crtc->bpp = 24; /* default for pre-Ironlake */
5830
5831         if (HAS_PCH_SPLIT(dev)) {
5832                 if (pipe == 2 && IS_IVYBRIDGE(dev))
5833                         intel_crtc->no_pll = true;
5834                 intel_helper_funcs.prepare = ironlake_crtc_prepare;
5835                 intel_helper_funcs.commit = ironlake_crtc_commit;
5836         } else {
5837                 intel_helper_funcs.prepare = i9xx_crtc_prepare;
5838                 intel_helper_funcs.commit = i9xx_crtc_commit;
5839         }
5840
5841         drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
5842
5843         intel_crtc->busy = false;
5844
5845         callout_init_mp(&intel_crtc->idle_callout);
5846 }
5847
5848 int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
5849                                 struct drm_file *file)
5850 {
5851         drm_i915_private_t *dev_priv = dev->dev_private;
5852         struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
5853         struct drm_mode_object *drmmode_obj;
5854         struct intel_crtc *crtc;
5855
5856         if (!dev_priv) {
5857                 DRM_ERROR("called with no initialization\n");
5858                 return -EINVAL;
5859         }
5860
5861         drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
5862                         DRM_MODE_OBJECT_CRTC);
5863
5864         if (!drmmode_obj) {
5865                 DRM_ERROR("no such CRTC id\n");
5866                 return -EINVAL;
5867         }
5868
5869         crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
5870         pipe_from_crtc_id->pipe = crtc->pipe;
5871
5872         return 0;
5873 }
5874
5875 static int intel_encoder_clones(struct drm_device *dev, int type_mask)
5876 {
5877         struct intel_encoder *encoder;
5878         int index_mask = 0;
5879         int entry = 0;
5880
5881         list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
5882                 if (type_mask & encoder->clone_mask)
5883                         index_mask |= (1 << entry);
5884                 entry++;
5885         }
5886
5887         return index_mask;
5888 }
5889
5890 static bool has_edp_a(struct drm_device *dev)
5891 {
5892         struct drm_i915_private *dev_priv = dev->dev_private;
5893
5894         if (!IS_MOBILE(dev))
5895                 return false;
5896
5897         if ((I915_READ(DP_A) & DP_DETECTED) == 0)
5898                 return false;
5899
5900         if (IS_GEN5(dev) &&
5901             (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
5902                 return false;
5903
5904         return true;
5905 }
5906
5907 static void intel_setup_outputs(struct drm_device *dev)
5908 {
5909         struct drm_i915_private *dev_priv = dev->dev_private;
5910         struct intel_encoder *encoder;
5911         bool dpd_is_edp = false;
5912         bool has_lvds;
5913
5914         has_lvds = intel_lvds_init(dev);
5915         if (!has_lvds && !HAS_PCH_SPLIT(dev)) {
5916                 /* disable the panel fitter on everything but LVDS */
5917                 I915_WRITE(PFIT_CONTROL, 0);
5918         }
5919
5920         if (HAS_PCH_SPLIT(dev)) {
5921                 dpd_is_edp = intel_dpd_is_edp(dev);
5922
5923                 if (has_edp_a(dev))
5924                         intel_dp_init(dev, DP_A);
5925
5926                 if (dpd_is_edp && (I915_READ(PCH_DP_D) & DP_DETECTED))
5927                         intel_dp_init(dev, PCH_DP_D);
5928         }
5929
5930         intel_crt_init(dev);
5931
5932         if (HAS_PCH_SPLIT(dev)) {
5933                 int found;
5934
5935                 DRM_DEBUG_KMS(
5936 "HDMIB %d PCH_DP_B %d HDMIC %d HDMID %d PCH_DP_C %d PCH_DP_D %d LVDS %d\n",
5937                     (I915_READ(HDMIB) & PORT_DETECTED) != 0,
5938                     (I915_READ(PCH_DP_B) & DP_DETECTED) != 0,
5939                     (I915_READ(HDMIC) & PORT_DETECTED) != 0,
5940                     (I915_READ(HDMID) & PORT_DETECTED) != 0,
5941                     (I915_READ(PCH_DP_C) & DP_DETECTED) != 0,
5942                     (I915_READ(PCH_DP_D) & DP_DETECTED) != 0,
5943                     (I915_READ(PCH_LVDS) & LVDS_DETECTED) != 0);
5944
5945                 if (I915_READ(HDMIB) & PORT_DETECTED) {
5946                         /* PCH SDVOB multiplex with HDMIB */
5947                         found = intel_sdvo_init(dev, PCH_SDVOB);
5948                         if (!found)
5949                                 intel_hdmi_init(dev, HDMIB);
5950                         if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
5951                                 intel_dp_init(dev, PCH_DP_B);
5952                 }
5953
5954                 if (I915_READ(HDMIC) & PORT_DETECTED)
5955                         intel_hdmi_init(dev, HDMIC);
5956
5957                 if (I915_READ(HDMID) & PORT_DETECTED)
5958                         intel_hdmi_init(dev, HDMID);
5959
5960                 if (I915_READ(PCH_DP_C) & DP_DETECTED)
5961                         intel_dp_init(dev, PCH_DP_C);
5962
5963                 if (!dpd_is_edp && (I915_READ(PCH_DP_D) & DP_DETECTED))
5964                         intel_dp_init(dev, PCH_DP_D);
5965
5966         } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
5967                 bool found = false;
5968
5969                 if (I915_READ(SDVOB) & SDVO_DETECTED) {
5970                         DRM_DEBUG_KMS("probing SDVOB\n");
5971                         found = intel_sdvo_init(dev, SDVOB);
5972                         if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
5973                                 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
5974                                 intel_hdmi_init(dev, SDVOB);
5975                         }
5976
5977                         if (!found && SUPPORTS_INTEGRATED_DP(dev)) {
5978                                 DRM_DEBUG_KMS("probing DP_B\n");
5979                                 intel_dp_init(dev, DP_B);
5980                         }
5981                 }
5982
5983                 /* Before G4X SDVOC doesn't have its own detect register */
5984
5985                 if (I915_READ(SDVOB) & SDVO_DETECTED) {
5986                         DRM_DEBUG_KMS("probing SDVOC\n");
5987                         found = intel_sdvo_init(dev, SDVOC);
5988                 }
5989
5990                 if (!found && (I915_READ(SDVOC) & SDVO_DETECTED)) {
5991
5992                         if (SUPPORTS_INTEGRATED_HDMI(dev)) {
5993                                 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
5994                                 intel_hdmi_init(dev, SDVOC);
5995                         }
5996                         if (SUPPORTS_INTEGRATED_DP(dev)) {
5997                                 DRM_DEBUG_KMS("probing DP_C\n");
5998                                 intel_dp_init(dev, DP_C);
5999                         }
6000                 }
6001
6002                 if (SUPPORTS_INTEGRATED_DP(dev) &&
6003                     (I915_READ(DP_D) & DP_DETECTED)) {
6004                         DRM_DEBUG_KMS("probing DP_D\n");
6005                         intel_dp_init(dev, DP_D);
6006                 }
6007         } else if (IS_GEN2(dev)) {
6008 #if 1
6009                 KIB_NOTYET();
6010 #else
6011                 intel_dvo_init(dev);
6012 #endif
6013         }
6014
6015         if (SUPPORTS_TV(dev))
6016                 intel_tv_init(dev);
6017
6018         list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
6019                 encoder->base.possible_crtcs = encoder->crtc_mask;
6020                 encoder->base.possible_clones =
6021                         intel_encoder_clones(dev, encoder->clone_mask);
6022         }
6023
6024         /* disable all the possible outputs/crtcs before entering KMS mode */
6025         drm_helper_disable_unused_functions(dev);
6026
6027         if (HAS_PCH_SPLIT(dev))
6028                 ironlake_init_pch_refclk(dev);
6029 }
6030
6031 static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
6032 {
6033         struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
6034
6035         drm_framebuffer_cleanup(fb);
6036         drm_gem_object_unreference_unlocked(&intel_fb->obj->base);
6037
6038         drm_free(intel_fb, DRM_MEM_KMS);
6039 }
6040
6041 static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
6042                                                 struct drm_file *file,
6043                                                 unsigned int *handle)
6044 {
6045         struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
6046         struct drm_i915_gem_object *obj = intel_fb->obj;
6047
6048         return drm_gem_handle_create(file, &obj->base, handle);
6049 }
6050
6051 static const struct drm_framebuffer_funcs intel_fb_funcs = {
6052         .destroy = intel_user_framebuffer_destroy,
6053         .create_handle = intel_user_framebuffer_create_handle,
6054 };
6055
6056 int intel_framebuffer_init(struct drm_device *dev,
6057                            struct intel_framebuffer *intel_fb,
6058                            struct drm_mode_fb_cmd2 *mode_cmd,
6059                            struct drm_i915_gem_object *obj)
6060 {
6061         int ret;
6062
6063         if (obj->tiling_mode == I915_TILING_Y)
6064                 return -EINVAL;
6065
6066         if (mode_cmd->pitches[0] & 63)
6067                 return -EINVAL;
6068
6069         switch (mode_cmd->pixel_format) {
6070         case DRM_FORMAT_RGB332:
6071         case DRM_FORMAT_RGB565:
6072         case DRM_FORMAT_XRGB8888:
6073         case DRM_FORMAT_XBGR8888:
6074         case DRM_FORMAT_ARGB8888:
6075         case DRM_FORMAT_XRGB2101010:
6076         case DRM_FORMAT_ARGB2101010:
6077                 /* RGB formats are common across chipsets */
6078                 break;
6079         case DRM_FORMAT_YUYV:
6080         case DRM_FORMAT_UYVY:
6081         case DRM_FORMAT_YVYU:
6082         case DRM_FORMAT_VYUY:
6083                 break;
6084         default:
6085                 DRM_DEBUG_KMS("unsupported pixel format %u\n",
6086                                 mode_cmd->pixel_format);
6087                 return -EINVAL;
6088         }
6089
6090         ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
6091         if (ret) {
6092                 DRM_ERROR("framebuffer init failed %d\n", ret);
6093                 return ret;
6094         }
6095
6096         drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
6097         intel_fb->obj = obj;
6098         return 0;
6099 }
6100
6101 static struct drm_framebuffer *
6102 intel_user_framebuffer_create(struct drm_device *dev,
6103                               struct drm_file *filp,
6104                               struct drm_mode_fb_cmd2 *mode_cmd)
6105 {
6106         struct drm_i915_gem_object *obj;
6107
6108         obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
6109                                                 mode_cmd->handles[0]));
6110         if (&obj->base == NULL)
6111                 return ERR_PTR(-ENOENT);
6112
6113         return intel_framebuffer_create(dev, mode_cmd, obj);
6114 }
6115
6116 static const struct drm_mode_config_funcs intel_mode_funcs = {
6117         .fb_create = intel_user_framebuffer_create,
6118         .output_poll_changed = intel_fb_output_poll_changed,
6119 };
6120
6121 /* Set up chip specific display functions */
6122 static void intel_init_display(struct drm_device *dev)
6123 {
6124         struct drm_i915_private *dev_priv = dev->dev_private;
6125
6126         /* We always want a DPMS function */
6127         if (HAS_PCH_SPLIT(dev)) {
6128                 dev_priv->display.dpms = ironlake_crtc_dpms;
6129                 dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
6130                 dev_priv->display.update_plane = ironlake_update_plane;
6131         } else {
6132                 dev_priv->display.dpms = i9xx_crtc_dpms;
6133                 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
6134                 dev_priv->display.update_plane = i9xx_update_plane;
6135         }
6136
6137         /* Returns the core display clock speed */
6138         if (IS_VALLEYVIEW(dev))
6139                 dev_priv->display.get_display_clock_speed =
6140                         valleyview_get_display_clock_speed;
6141         else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
6142                 dev_priv->display.get_display_clock_speed =
6143                         i945_get_display_clock_speed;
6144         else if (IS_I915G(dev))
6145                 dev_priv->display.get_display_clock_speed =
6146                         i915_get_display_clock_speed;
6147         else if (IS_I945GM(dev) || IS_845G(dev) || IS_PINEVIEW_M(dev))
6148                 dev_priv->display.get_display_clock_speed =
6149                         i9xx_misc_get_display_clock_speed;
6150         else if (IS_I915GM(dev))
6151                 dev_priv->display.get_display_clock_speed =
6152                         i915gm_get_display_clock_speed;
6153         else if (IS_I865G(dev))
6154                 dev_priv->display.get_display_clock_speed =
6155                         i865_get_display_clock_speed;
6156         else if (IS_I85X(dev))
6157                 dev_priv->display.get_display_clock_speed =
6158                         i855_get_display_clock_speed;
6159         else /* 852, 830 */
6160                 dev_priv->display.get_display_clock_speed =
6161                         i830_get_display_clock_speed;
6162
6163         if (HAS_PCH_SPLIT(dev)) {
6164                 if (IS_GEN5(dev)) {
6165                         dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
6166                         dev_priv->display.write_eld = ironlake_write_eld;
6167                 } else if (IS_GEN6(dev)) {
6168                         dev_priv->display.fdi_link_train = gen6_fdi_link_train;
6169                         dev_priv->display.write_eld = ironlake_write_eld;
6170                 } else if (IS_IVYBRIDGE(dev)) {
6171                         /* FIXME: detect B0+ stepping and use auto training */
6172                         dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
6173                         dev_priv->display.write_eld = ironlake_write_eld;
6174                 } else
6175                         dev_priv->display.update_wm = NULL;
6176         } else if (IS_G4X(dev)) {
6177                 dev_priv->display.write_eld = g4x_write_eld;
6178         }
6179
6180         /* Default just returns -ENODEV to indicate unsupported */
6181         dev_priv->display.queue_flip = intel_default_queue_flip;
6182
6183         switch (INTEL_INFO(dev)->gen) {
6184         case 2:
6185                 dev_priv->display.queue_flip = intel_gen2_queue_flip;
6186                 break;
6187
6188         case 3:
6189                 dev_priv->display.queue_flip = intel_gen3_queue_flip;
6190                 break;
6191
6192         case 4:
6193         case 5:
6194                 dev_priv->display.queue_flip = intel_gen4_queue_flip;
6195                 break;
6196
6197         case 6:
6198                 dev_priv->display.queue_flip = intel_gen6_queue_flip;
6199                 break;
6200         case 7:
6201                 dev_priv->display.queue_flip = intel_gen7_queue_flip;
6202                 break;
6203         }
6204 }
6205
6206 /*
6207  * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
6208  * resume, or other times.  This quirk makes sure that's the case for
6209  * affected systems.
6210  */
6211 static void quirk_pipea_force(struct drm_device *dev)
6212 {
6213         struct drm_i915_private *dev_priv = dev->dev_private;
6214
6215         dev_priv->quirks |= QUIRK_PIPEA_FORCE;
6216         DRM_DEBUG("applying pipe a force quirk\n");
6217 }
6218
6219 /*
6220  * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
6221  */
6222 static void quirk_ssc_force_disable(struct drm_device *dev)
6223 {
6224         struct drm_i915_private *dev_priv = dev->dev_private;
6225         dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
6226 }
6227
6228 struct intel_quirk {
6229         int device;
6230         int subsystem_vendor;
6231         int subsystem_device;
6232         void (*hook)(struct drm_device *dev);
6233 };
6234
6235 #define PCI_ANY_ID      (~0u)
6236
6237 struct intel_quirk intel_quirks[] = {
6238         /* HP Mini needs pipe A force quirk (LP: #322104) */
6239         { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
6240
6241         /* Thinkpad R31 needs pipe A force quirk */
6242         { 0x3577, 0x1014, 0x0505, quirk_pipea_force },
6243         /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
6244         { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
6245
6246         /* ThinkPad X30 needs pipe A force quirk (LP: #304614) */
6247         { 0x3577,  0x1014, 0x0513, quirk_pipea_force },
6248         /* ThinkPad X40 needs pipe A force quirk */
6249
6250         /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
6251         { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
6252
6253         /* 855 & before need to leave pipe A & dpll A up */
6254         { 0x3582, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
6255         { 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
6256
6257         /* Lenovo U160 cannot use SSC on LVDS */
6258         { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
6259
6260         /* Sony Vaio Y cannot use SSC on LVDS */
6261         { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
6262 };
6263
6264 static void intel_init_quirks(struct drm_device *dev)
6265 {
6266         struct intel_quirk *q;
6267         device_t d;
6268         int i;
6269
6270         d = dev->dev;
6271         for (i = 0; i < DRM_ARRAY_SIZE(intel_quirks); i++) {
6272                 q = &intel_quirks[i];
6273                 if (pci_get_device(d) == q->device &&
6274                     (pci_get_subvendor(d) == q->subsystem_vendor ||
6275                      q->subsystem_vendor == PCI_ANY_ID) &&
6276                     (pci_get_subdevice(d) == q->subsystem_device ||
6277                      q->subsystem_device == PCI_ANY_ID))
6278                         q->hook(dev);
6279         }
6280 }
6281
6282 /* Disable the VGA plane that we never use */
6283 static void i915_disable_vga(struct drm_device *dev)
6284 {
6285         struct drm_i915_private *dev_priv = dev->dev_private;
6286         u8 sr1;
6287         u32 vga_reg;
6288
6289         if (HAS_PCH_SPLIT(dev))
6290                 vga_reg = CPU_VGACNTRL;
6291         else
6292                 vga_reg = VGACNTRL;
6293
6294 #if 0
6295         vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
6296 #endif
6297         outb(VGA_SR_INDEX, 1);
6298         sr1 = inb(VGA_SR_DATA);
6299         outb(VGA_SR_DATA, sr1 | 1 << 5);
6300 #if 0
6301         vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
6302 #endif
6303         DELAY(300);
6304
6305         I915_WRITE(vga_reg, VGA_DISP_DISABLE);
6306         POSTING_READ(vga_reg);
6307 }
6308
6309 void intel_modeset_init(struct drm_device *dev)
6310 {
6311         struct drm_i915_private *dev_priv = dev->dev_private;
6312         int i, ret;
6313
6314         drm_mode_config_init(dev);
6315
6316         dev->mode_config.min_width = 0;
6317         dev->mode_config.min_height = 0;
6318
6319         dev->mode_config.preferred_depth = 24;
6320         dev->mode_config.prefer_shadow = 1;
6321
6322         dev->mode_config.funcs = __DECONST(struct drm_mode_config_funcs *,
6323             &intel_mode_funcs);
6324
6325         intel_init_quirks(dev);
6326
6327         intel_init_pm(dev);
6328
6329         intel_init_display(dev);
6330
6331         if (IS_GEN2(dev)) {
6332                 dev->mode_config.max_width = 2048;
6333                 dev->mode_config.max_height = 2048;
6334         } else if (IS_GEN3(dev)) {
6335                 dev->mode_config.max_width = 4096;
6336                 dev->mode_config.max_height = 4096;
6337         } else {
6338                 dev->mode_config.max_width = 8192;
6339                 dev->mode_config.max_height = 8192;
6340         }
6341         dev->mode_config.fb_base = dev->agp->base;
6342
6343         DRM_DEBUG_KMS("%d display pipe%s available.\n",
6344                       dev_priv->num_pipe, dev_priv->num_pipe > 1 ? "s" : "");
6345
6346         for (i = 0; i < dev_priv->num_pipe; i++) {
6347                 intel_crtc_init(dev, i);
6348                 ret = intel_plane_init(dev, i);
6349                 if (ret)
6350                         DRM_DEBUG_KMS("plane %d init failed: %d\n", i, ret);
6351         }
6352
6353         /* Just disable it once at startup */
6354         i915_disable_vga(dev);
6355         intel_setup_outputs(dev);
6356
6357         intel_init_clock_gating(dev);
6358
6359         if (IS_IRONLAKE_M(dev)) {
6360                 ironlake_enable_drps(dev);
6361                 intel_init_emon(dev);
6362         }
6363
6364         if (IS_GEN6(dev)) {
6365                 gen6_enable_rps(dev_priv);
6366                 gen6_update_ring_freq(dev_priv);
6367         }
6368
6369         callout_init_mp(&dev_priv->idle_callout);
6370 }
6371
6372 void intel_modeset_gem_init(struct drm_device *dev)
6373 {
6374         if (IS_IRONLAKE_M(dev))
6375                 ironlake_enable_rc6(dev);
6376
6377         intel_setup_overlay(dev);
6378 }
6379
6380 void intel_modeset_cleanup(struct drm_device *dev)
6381 {
6382         struct drm_i915_private *dev_priv = dev->dev_private;
6383         struct drm_crtc *crtc;
6384         struct intel_crtc *intel_crtc;
6385
6386         drm_kms_helper_poll_fini(dev);
6387         DRM_LOCK(dev);
6388
6389 #if 0
6390         intel_unregister_dsm_handler();
6391 #endif
6392
6393         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
6394                 /* Skip inactive CRTCs */
6395                 if (!crtc->fb)
6396                         continue;
6397
6398                 intel_crtc = to_intel_crtc(crtc);
6399                 intel_increase_pllclock(crtc);
6400         }
6401
6402         intel_disable_fbc(dev);
6403
6404         if (IS_IRONLAKE_M(dev))
6405                 ironlake_disable_drps(dev);
6406         if (IS_GEN6(dev))
6407                 gen6_disable_rps(dev);
6408
6409         if (IS_IRONLAKE_M(dev))
6410                 ironlake_disable_rc6(dev);
6411
6412         DRM_UNLOCK(dev);
6413
6414         /* Disable the irq before mode object teardown, for the irq might
6415          * enqueue unpin/hotplug work. */
6416         drm_irq_uninstall(dev);
6417         cancel_work_sync(&dev_priv->hotplug_work);
6418         cancel_work_sync(&dev_priv->rps.work);
6419
6420         /* flush any delayed tasks or pending work */
6421         flush_scheduled_work();
6422
6423         drm_mode_config_cleanup(dev);
6424 }
6425
6426 /*
6427  * Return which encoder is currently attached for connector.
6428  */
6429 struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
6430 {
6431         return &intel_attached_encoder(connector)->base;
6432 }
6433
6434 void intel_connector_attach_encoder(struct intel_connector *connector,
6435                                     struct intel_encoder *encoder)
6436 {
6437         connector->encoder = encoder;
6438         drm_mode_connector_attach_encoder(&connector->base,
6439                                           &encoder->base);
6440 }
6441
6442 /*
6443  * set vga decode state - true == enable VGA decode
6444  */
6445 int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
6446 {
6447         struct drm_i915_private *dev_priv;
6448         device_t bridge_dev;
6449         u16 gmch_ctrl;
6450
6451         dev_priv = dev->dev_private;
6452         bridge_dev = intel_gtt_get_bridge_device();
6453         gmch_ctrl = pci_read_config(bridge_dev, INTEL_GMCH_CTRL, 2);
6454         if (state)
6455                 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
6456         else
6457                 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
6458         pci_write_config(bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl, 2);
6459         return (0);
6460 }
6461
6462 struct intel_display_error_state {
6463         struct intel_cursor_error_state {
6464                 u32 control;
6465                 u32 position;
6466                 u32 base;
6467                 u32 size;
6468         } cursor[2];
6469
6470         struct intel_pipe_error_state {
6471                 u32 conf;
6472                 u32 source;
6473
6474                 u32 htotal;
6475                 u32 hblank;
6476                 u32 hsync;
6477                 u32 vtotal;
6478                 u32 vblank;
6479                 u32 vsync;
6480         } pipe[2];
6481
6482         struct intel_plane_error_state {
6483                 u32 control;
6484                 u32 stride;
6485                 u32 size;
6486                 u32 pos;
6487                 u32 addr;
6488                 u32 surface;
6489                 u32 tile_offset;
6490         } plane[2];
6491 };
6492
6493 struct intel_display_error_state *
6494 intel_display_capture_error_state(struct drm_device *dev)
6495 {
6496         drm_i915_private_t *dev_priv = dev->dev_private;
6497         struct intel_display_error_state *error;
6498         int i;
6499
6500         error = kmalloc(sizeof(*error), DRM_MEM_KMS, M_NOWAIT);
6501         if (error == NULL)
6502                 return NULL;
6503
6504         for (i = 0; i < 2; i++) {
6505                 error->cursor[i].control = I915_READ(CURCNTR(i));
6506                 error->cursor[i].position = I915_READ(CURPOS(i));
6507                 error->cursor[i].base = I915_READ(CURBASE(i));
6508
6509                 error->plane[i].control = I915_READ(DSPCNTR(i));
6510                 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
6511                 error->plane[i].size = I915_READ(DSPSIZE(i));
6512                 error->plane[i].pos = I915_READ(DSPPOS(i));
6513                 error->plane[i].addr = I915_READ(DSPADDR(i));
6514                 if (INTEL_INFO(dev)->gen >= 4) {
6515                         error->plane[i].surface = I915_READ(DSPSURF(i));
6516                         error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
6517                 }
6518
6519                 error->pipe[i].conf = I915_READ(PIPECONF(i));
6520                 error->pipe[i].source = I915_READ(PIPESRC(i));
6521                 error->pipe[i].htotal = I915_READ(HTOTAL(i));
6522                 error->pipe[i].hblank = I915_READ(HBLANK(i));
6523                 error->pipe[i].hsync = I915_READ(HSYNC(i));
6524                 error->pipe[i].vtotal = I915_READ(VTOTAL(i));
6525                 error->pipe[i].vblank = I915_READ(VBLANK(i));
6526                 error->pipe[i].vsync = I915_READ(VSYNC(i));
6527         }
6528
6529         return error;
6530 }
6531
6532 void
6533 intel_display_print_error_state(struct sbuf *m,
6534                                 struct drm_device *dev,
6535                                 struct intel_display_error_state *error)
6536 {
6537         int i;
6538
6539         for (i = 0; i < 2; i++) {
6540                 sbuf_printf(m, "Pipe [%d]:\n", i);
6541                 sbuf_printf(m, "  CONF: %08x\n", error->pipe[i].conf);
6542                 sbuf_printf(m, "  SRC: %08x\n", error->pipe[i].source);
6543                 sbuf_printf(m, "  HTOTAL: %08x\n", error->pipe[i].htotal);
6544                 sbuf_printf(m, "  HBLANK: %08x\n", error->pipe[i].hblank);
6545                 sbuf_printf(m, "  HSYNC: %08x\n", error->pipe[i].hsync);
6546                 sbuf_printf(m, "  VTOTAL: %08x\n", error->pipe[i].vtotal);
6547                 sbuf_printf(m, "  VBLANK: %08x\n", error->pipe[i].vblank);
6548                 sbuf_printf(m, "  VSYNC: %08x\n", error->pipe[i].vsync);
6549
6550                 sbuf_printf(m, "Plane [%d]:\n", i);
6551                 sbuf_printf(m, "  CNTR: %08x\n", error->plane[i].control);
6552                 sbuf_printf(m, "  STRIDE: %08x\n", error->plane[i].stride);
6553                 sbuf_printf(m, "  SIZE: %08x\n", error->plane[i].size);
6554                 sbuf_printf(m, "  POS: %08x\n", error->plane[i].pos);
6555                 sbuf_printf(m, "  ADDR: %08x\n", error->plane[i].addr);
6556                 if (INTEL_INFO(dev)->gen >= 4) {
6557                         sbuf_printf(m, "  SURF: %08x\n", error->plane[i].surface);
6558                         sbuf_printf(m, "  TILEOFF: %08x\n", error->plane[i].tile_offset);
6559                 }
6560
6561                 sbuf_printf(m, "Cursor [%d]:\n", i);
6562                 sbuf_printf(m, "  CNTR: %08x\n", error->cursor[i].control);
6563                 sbuf_printf(m, "  POS: %08x\n", error->cursor[i].position);
6564                 sbuf_printf(m, "  BASE: %08x\n", error->cursor[i].base);
6565         }
6566 }