a2307a3d11374f843a950e116cdaec43e587546d
[profile/ivi/libdrm.git] / linux-core / intel_display.c
1 /*
2  * Copyright © 2006-2007 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *      Eric Anholt <eric@anholt.net>
25  */
26
27 #include <linux/i2c.h>
28 #include "drmP.h"
29 #include "intel_drv.h"
30 #include "i915_drm.h"
31 #include "i915_drv.h"
32
33 #include "drm_crtc_helper.h"
34
35 bool intel_pipe_has_type (struct drm_crtc *crtc, int type);
36
37 typedef struct {
38     /* given values */    
39     int n;
40     int m1, m2;
41     int p1, p2;
42     /* derived values */
43     int dot;
44     int vco;
45     int m;
46     int p;
47 } intel_clock_t;
48
49 typedef struct {
50     int min, max;
51 } intel_range_t;
52
53 typedef struct {
54     int dot_limit;
55     int p2_slow, p2_fast;
56 } intel_p2_t;
57
58 #define INTEL_P2_NUM                  2
59
60 typedef struct {
61     intel_range_t   dot, vco, n, m, m1, m2, p, p1;
62     intel_p2_t      p2;
63 } intel_limit_t;
64
65 #define I8XX_DOT_MIN              25000
66 #define I8XX_DOT_MAX             350000
67 #define I8XX_VCO_MIN             930000
68 #define I8XX_VCO_MAX            1400000
69 #define I8XX_N_MIN                    3
70 #define I8XX_N_MAX                   16
71 #define I8XX_M_MIN                   96
72 #define I8XX_M_MAX                  140
73 #define I8XX_M1_MIN                  18
74 #define I8XX_M1_MAX                  26
75 #define I8XX_M2_MIN                   6
76 #define I8XX_M2_MAX                  16
77 #define I8XX_P_MIN                    4
78 #define I8XX_P_MAX                  128
79 #define I8XX_P1_MIN                   2
80 #define I8XX_P1_MAX                  33
81 #define I8XX_P1_LVDS_MIN              1
82 #define I8XX_P1_LVDS_MAX              6
83 #define I8XX_P2_SLOW                  4
84 #define I8XX_P2_FAST                  2
85 #define I8XX_P2_LVDS_SLOW             14
86 #define I8XX_P2_LVDS_FAST             14 /* No fast option */
87 #define I8XX_P2_SLOW_LIMIT       165000
88
89 #define I9XX_DOT_MIN              20000
90 #define I9XX_DOT_MAX             400000
91 #define I9XX_VCO_MIN            1400000
92 #define I9XX_VCO_MAX            2800000
93 #define I9XX_N_MIN                    3
94 #define I9XX_N_MAX                    8
95 #define I9XX_M_MIN                   70
96 #define I9XX_M_MAX                  120
97 #define I9XX_M1_MIN                  10
98 #define I9XX_M1_MAX                  20
99 #define I9XX_M2_MIN                   5
100 #define I9XX_M2_MAX                   9
101 #define I9XX_P_SDVO_DAC_MIN           5
102 #define I9XX_P_SDVO_DAC_MAX          80
103 #define I9XX_P_LVDS_MIN               7
104 #define I9XX_P_LVDS_MAX              98
105 #define I9XX_P1_MIN                   1
106 #define I9XX_P1_MAX                   8
107 #define I9XX_P2_SDVO_DAC_SLOW                10
108 #define I9XX_P2_SDVO_DAC_FAST                 5
109 #define I9XX_P2_SDVO_DAC_SLOW_LIMIT      200000
110 #define I9XX_P2_LVDS_SLOW                    14
111 #define I9XX_P2_LVDS_FAST                     7
112 #define I9XX_P2_LVDS_SLOW_LIMIT          112000
113
114 #define INTEL_LIMIT_I8XX_DVO_DAC    0
115 #define INTEL_LIMIT_I8XX_LVDS       1
116 #define INTEL_LIMIT_I9XX_SDVO_DAC   2
117 #define INTEL_LIMIT_I9XX_LVDS       3
118
119 static const intel_limit_t intel_limits[] = {
120     { /* INTEL_LIMIT_I8XX_DVO_DAC */
121         .dot = { .min = I8XX_DOT_MIN,           .max = I8XX_DOT_MAX },
122         .vco = { .min = I8XX_VCO_MIN,           .max = I8XX_VCO_MAX },
123         .n   = { .min = I8XX_N_MIN,             .max = I8XX_N_MAX },
124         .m   = { .min = I8XX_M_MIN,             .max = I8XX_M_MAX },
125         .m1  = { .min = I8XX_M1_MIN,            .max = I8XX_M1_MAX },
126         .m2  = { .min = I8XX_M2_MIN,            .max = I8XX_M2_MAX },
127         .p   = { .min = I8XX_P_MIN,             .max = I8XX_P_MAX },
128         .p1  = { .min = I8XX_P1_MIN,            .max = I8XX_P1_MAX },
129         .p2  = { .dot_limit = I8XX_P2_SLOW_LIMIT,
130                  .p2_slow = I8XX_P2_SLOW,       .p2_fast = I8XX_P2_FAST },
131     },
132     { /* INTEL_LIMIT_I8XX_LVDS */
133         .dot = { .min = I8XX_DOT_MIN,           .max = I8XX_DOT_MAX },
134         .vco = { .min = I8XX_VCO_MIN,           .max = I8XX_VCO_MAX },
135         .n   = { .min = I8XX_N_MIN,             .max = I8XX_N_MAX },
136         .m   = { .min = I8XX_M_MIN,             .max = I8XX_M_MAX },
137         .m1  = { .min = I8XX_M1_MIN,            .max = I8XX_M1_MAX },
138         .m2  = { .min = I8XX_M2_MIN,            .max = I8XX_M2_MAX },
139         .p   = { .min = I8XX_P_MIN,             .max = I8XX_P_MAX },
140         .p1  = { .min = I8XX_P1_LVDS_MIN,       .max = I8XX_P1_LVDS_MAX },
141         .p2  = { .dot_limit = I8XX_P2_SLOW_LIMIT,
142                  .p2_slow = I8XX_P2_LVDS_SLOW,  .p2_fast = I8XX_P2_LVDS_FAST },
143     },
144     { /* INTEL_LIMIT_I9XX_SDVO_DAC */
145         .dot = { .min = I9XX_DOT_MIN,           .max = I9XX_DOT_MAX },
146         .vco = { .min = I9XX_VCO_MIN,           .max = I9XX_VCO_MAX },
147         .n   = { .min = I9XX_N_MIN,             .max = I9XX_N_MAX },
148         .m   = { .min = I9XX_M_MIN,             .max = I9XX_M_MAX },
149         .m1  = { .min = I9XX_M1_MIN,            .max = I9XX_M1_MAX },
150         .m2  = { .min = I9XX_M2_MIN,            .max = I9XX_M2_MAX },
151         .p   = { .min = I9XX_P_SDVO_DAC_MIN,    .max = I9XX_P_SDVO_DAC_MAX },
152         .p1  = { .min = I9XX_P1_MIN,            .max = I9XX_P1_MAX },
153         .p2  = { .dot_limit = I9XX_P2_SDVO_DAC_SLOW_LIMIT,
154                  .p2_slow = I9XX_P2_SDVO_DAC_SLOW,      .p2_fast = I9XX_P2_SDVO_DAC_FAST },
155     },
156     { /* INTEL_LIMIT_I9XX_LVDS */
157         .dot = { .min = I9XX_DOT_MIN,           .max = I9XX_DOT_MAX },
158         .vco = { .min = I9XX_VCO_MIN,           .max = I9XX_VCO_MAX },
159         .n   = { .min = I9XX_N_MIN,             .max = I9XX_N_MAX },
160         .m   = { .min = I9XX_M_MIN,             .max = I9XX_M_MAX },
161         .m1  = { .min = I9XX_M1_MIN,            .max = I9XX_M1_MAX },
162         .m2  = { .min = I9XX_M2_MIN,            .max = I9XX_M2_MAX },
163         .p   = { .min = I9XX_P_LVDS_MIN,        .max = I9XX_P_LVDS_MAX },
164         .p1  = { .min = I9XX_P1_MIN,            .max = I9XX_P1_MAX },
165         /* The single-channel range is 25-112Mhz, and dual-channel
166          * is 80-224Mhz.  Prefer single channel as much as possible.
167          */
168         .p2  = { .dot_limit = I9XX_P2_LVDS_SLOW_LIMIT,
169                  .p2_slow = I9XX_P2_LVDS_SLOW,  .p2_fast = I9XX_P2_LVDS_FAST },
170     },
171 };
172
173 static const intel_limit_t *intel_limit(struct drm_crtc *crtc)
174 {
175         struct drm_device *dev = crtc->dev;
176         const intel_limit_t *limit;
177         
178         if (IS_I9XX(dev)) {
179                 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
180                         limit = &intel_limits[INTEL_LIMIT_I9XX_LVDS];
181                 else
182                         limit = &intel_limits[INTEL_LIMIT_I9XX_SDVO_DAC];
183         } else {
184                 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
185                         limit = &intel_limits[INTEL_LIMIT_I8XX_LVDS];
186                 else
187                         limit = &intel_limits[INTEL_LIMIT_I8XX_DVO_DAC];
188         }
189         return limit;
190 }
191
192 /** Derive the pixel clock for the given refclk and divisors for 8xx chips. */
193
194 static void i8xx_clock(int refclk, intel_clock_t *clock)
195 {
196         clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
197         clock->p = clock->p1 * clock->p2;
198         clock->vco = refclk * clock->m / (clock->n + 2);
199         clock->dot = clock->vco / clock->p;
200 }
201
202 /** Derive the pixel clock for the given refclk and divisors for 9xx chips. */
203
204 static void i9xx_clock(int refclk, intel_clock_t *clock)
205 {
206         clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
207         clock->p = clock->p1 * clock->p2;
208         clock->vco = refclk * clock->m / (clock->n + 2);
209         clock->dot = clock->vco / clock->p;
210 }
211
212 static void intel_clock(struct drm_device *dev, int refclk,
213                         intel_clock_t *clock)
214 {
215         if (IS_I9XX(dev))
216                 return i9xx_clock (refclk, clock);
217         else
218                 return i8xx_clock (refclk, clock);
219 }
220
221 /**
222  * Returns whether any output on the specified pipe is of the specified type
223  */
224 bool intel_pipe_has_type (struct drm_crtc *crtc, int type)
225 {
226     struct drm_device *dev = crtc->dev;
227     struct drm_mode_config *mode_config = &dev->mode_config;
228     struct drm_output *l_entry;
229
230     list_for_each_entry(l_entry, &mode_config->output_list, head) {
231             if (l_entry->crtc == crtc) {
232                     struct intel_output *intel_output = l_entry->driver_private;
233                     if (intel_output->type == type)
234                             return true;
235             }
236     }
237     return false;
238 }
239
240 #define INTELPllInvalid(s)   { /* ErrorF (s) */; return false; }
241 /**
242  * Returns whether the given set of divisors are valid for a given refclk with
243  * the given outputs.
244  */
245
246 static bool intel_PLL_is_valid(struct drm_crtc *crtc, intel_clock_t *clock)
247 {
248         const intel_limit_t *limit = intel_limit (crtc);
249         
250         if (clock->p1  < limit->p1.min  || limit->p1.max  < clock->p1)
251                 INTELPllInvalid ("p1 out of range\n");
252         if (clock->p   < limit->p.min   || limit->p.max   < clock->p)
253                 INTELPllInvalid ("p out of range\n");
254         if (clock->m2  < limit->m2.min  || limit->m2.max  < clock->m2)
255                 INTELPllInvalid ("m2 out of range\n");
256         if (clock->m1  < limit->m1.min  || limit->m1.max  < clock->m1)
257                 INTELPllInvalid ("m1 out of range\n");
258         if (clock->m1 <= clock->m2)
259                 INTELPllInvalid ("m1 <= m2\n");
260         if (clock->m   < limit->m.min   || limit->m.max   < clock->m)
261                 INTELPllInvalid ("m out of range\n");
262         if (clock->n   < limit->n.min   || limit->n.max   < clock->n)
263                 INTELPllInvalid ("n out of range\n");
264         if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
265                 INTELPllInvalid ("vco out of range\n");
266         /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
267          * output, etc., rather than just a single range.
268          */
269         if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
270                 INTELPllInvalid ("dot out of range\n");
271         
272         return true;
273 }
274
275 /**
276  * Returns a set of divisors for the desired target clock with the given
277  * refclk, or FALSE.  The returned values represent the clock equation:
278  * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
279  */
280 static bool intel_find_best_PLL(struct drm_crtc *crtc, int target,
281                                 int refclk, intel_clock_t *best_clock)
282 {
283         struct drm_device *dev = crtc->dev;
284         struct drm_i915_private *dev_priv = dev->dev_private;
285         intel_clock_t clock;
286         const intel_limit_t *limit = intel_limit(crtc);
287         int err = target;
288
289         if (IS_I9XX(dev) && intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
290             (I915_READ(LVDS) & LVDS_PORT_EN) != 0) {
291                 /*
292                  * For LVDS, if the panel is on, just rely on its current
293                  * settings for dual-channel.  We haven't figured out how to
294                  * reliably set up different single/dual channel state, if we
295                  * even can.
296                  */
297                 if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
298                     LVDS_CLKB_POWER_UP)
299                         clock.p2 = limit->p2.p2_fast;
300                 else
301                         clock.p2 = limit->p2.p2_slow;
302         } else {
303                 if (target < limit->p2.dot_limit)
304                         clock.p2 = limit->p2.p2_slow;
305                 else
306                         clock.p2 = limit->p2.p2_fast;
307         }
308         
309         memset (best_clock, 0, sizeof (*best_clock));
310         
311         for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
312                 for (clock.m2 = limit->m2.min; clock.m2 < clock.m1 &&
313                              clock.m2 <= limit->m2.max; clock.m2++) {
314                         for (clock.n = limit->n.min; clock.n <= limit->n.max;
315                              clock.n++) {
316                                 for (clock.p1 = limit->p1.min;
317                                      clock.p1 <= limit->p1.max; clock.p1++) {
318                                         int this_err;
319                                         
320                                         intel_clock(dev, refclk, &clock);
321                                         
322                                         if (!intel_PLL_is_valid(crtc, &clock))
323                                                 continue;
324                                         
325                                         this_err = abs(clock.dot - target);
326                                         if (this_err < err) {
327                                                 *best_clock = clock;
328                                                 err = this_err;
329                                         }
330                                 }
331                         }
332                 }
333         }
334
335         return (err != target);
336 }
337
338 void
339 intel_set_vblank(struct drm_device *dev)
340 {
341         struct drm_i915_private *dev_priv = dev->dev_private;
342         struct drm_crtc *crtc;
343         struct intel_crtc *intel_crtc;
344         int vbl_pipe = 0;
345
346         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
347                 intel_crtc = crtc->driver_private;
348
349                 if (crtc->enabled)
350                         vbl_pipe |= (1<<intel_crtc->pipe);
351         }
352
353         dev_priv->vblank_pipe = vbl_pipe;
354         i915_enable_interrupt(dev);
355 }
356 void
357 intel_wait_for_vblank(struct drm_device *dev)
358 {
359         /* Wait for 20ms, i.e. one cycle at 50hz. */
360         udelay(20000);
361 }
362
363 void
364 intel_pipe_set_base(struct drm_crtc *crtc, int x, int y)
365 {
366         struct drm_device *dev = crtc->dev;
367         struct drm_i915_private *dev_priv = dev->dev_private;
368         struct drm_i915_master_private *master_priv;
369         struct intel_crtc *intel_crtc = crtc->driver_private;
370         int pipe = intel_crtc->pipe;
371         unsigned long Start, Offset;
372         int dspbase = (pipe == 0 ? DSPAADDR : DSPBADDR);
373         int dspsurf = (pipe == 0 ? DSPASURF : DSPBSURF);
374         int dspstride = (pipe == 0) ? DSPASTRIDE : DSPBSTRIDE;
375         int dspcntr_reg = (pipe == 0) ? DSPACNTR : DSPBCNTR;
376         u32 dspcntr;
377
378         /* no fb bound */
379         if (!crtc->fb) {
380                 DRM_DEBUG("No FB bound\n");
381                 return;
382         }
383
384         Start = crtc->fb->bo->offset;
385         Offset = y * crtc->fb->pitch + x * (crtc->fb->bits_per_pixel / 8);
386
387         I915_WRITE(dspstride, crtc->fb->pitch);
388
389         dspcntr = I915_READ(dspcntr_reg);
390         switch (crtc->fb->bits_per_pixel) {
391         case 8:
392                 dspcntr |= DISPPLANE_8BPP;
393                 break;
394         case 16:
395                 if (crtc->fb->depth == 15)
396                         dspcntr |= DISPPLANE_15_16BPP;
397                 else
398                         dspcntr |= DISPPLANE_16BPP;
399                 break;
400         case 24:
401         case 32:
402                 dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
403                 break;
404         default:
405                 DRM_ERROR("Unknown color depth\n");
406                 return;
407         }
408         I915_WRITE(dspcntr_reg, dspcntr);
409
410         DRM_DEBUG("Writing base %08lX %08lX %d %d\n", Start, Offset, x, y);
411         if (IS_I965G(dev)) {
412                 I915_WRITE(dspbase, Offset);
413                 I915_READ(dspbase);
414                 I915_WRITE(dspsurf, Start);
415                 I915_READ(dspsurf);
416         } else {
417                 I915_WRITE(dspbase, Start + Offset);
418                 I915_READ(dspbase);
419         }
420         
421
422         if (!dev->primary->master)
423                 return;
424
425         master_priv = dev->primary->master->driver_priv;
426         if (!master_priv->sarea_priv) 
427                 return;
428                 
429         switch (pipe) {
430         case 0:
431                 master_priv->sarea_priv->planeA_x = x;
432                 master_priv->sarea_priv->planeA_y = y;
433                 break;
434         case 1:
435                 master_priv->sarea_priv->planeB_x = x;
436                 master_priv->sarea_priv->planeB_y = y;
437                 break;
438         default:
439                 DRM_ERROR("Can't update pipe %d in SAREA\n", pipe);
440                 break;
441         }
442 }
443
444
445
446 /**
447  * Sets the power management mode of the pipe and plane.
448  *
449  * This code should probably grow support for turning the cursor off and back
450  * on appropriately at the same time as we're turning the pipe off/on.
451  */
452 static void intel_crtc_dpms(struct drm_crtc *crtc, int mode)
453 {
454         struct drm_device *dev = crtc->dev;
455         struct drm_i915_master_private *master_priv;
456         struct drm_i915_private *dev_priv = dev->dev_private;
457         struct intel_crtc *intel_crtc = crtc->driver_private;
458         int pipe = intel_crtc->pipe;
459         int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
460         int dspcntr_reg = (pipe == 0) ? DSPACNTR : DSPBCNTR;
461         int dspbase_reg = (pipe == 0) ? DSPAADDR : DSPBADDR;
462         int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
463         u32 temp;
464         bool enabled;
465
466         /* XXX: When our outputs are all unaware of DPMS modes other than off
467          * and on, we should map those modes to DPMSModeOff in the CRTC.
468          */
469         switch (mode) {
470         case DPMSModeOn:
471         case DPMSModeStandby:
472         case DPMSModeSuspend:
473                 /* Enable the DPLL */
474                 temp = I915_READ(dpll_reg);
475                 if ((temp & DPLL_VCO_ENABLE) == 0) {
476                         I915_WRITE(dpll_reg, temp);
477                         I915_READ(dpll_reg);
478                         /* Wait for the clocks to stabilize. */
479                         udelay(150);
480                         I915_WRITE(dpll_reg, temp | DPLL_VCO_ENABLE);
481                         I915_READ(dpll_reg);
482                         /* Wait for the clocks to stabilize. */
483                         udelay(150);
484                         I915_WRITE(dpll_reg, temp | DPLL_VCO_ENABLE);
485                         I915_READ(dpll_reg);
486                         /* Wait for the clocks to stabilize. */
487                         udelay(150);
488                 }
489                 
490                 /* Enable the pipe */
491                 temp = I915_READ(pipeconf_reg);
492                 if ((temp & PIPEACONF_ENABLE) == 0)
493                         I915_WRITE(pipeconf_reg, temp | PIPEACONF_ENABLE);
494                 
495                 /* Enable the plane */
496                 temp = I915_READ(dspcntr_reg);
497                 if ((temp & DISPLAY_PLANE_ENABLE) == 0) {
498                         I915_WRITE(dspcntr_reg, temp | DISPLAY_PLANE_ENABLE);
499                         /* Flush the plane changes */
500                         I915_WRITE(dspbase_reg, I915_READ(dspbase_reg));
501                 }
502                 
503                 intel_crtc_load_lut(crtc);
504                 
505                 /* Give the overlay scaler a chance to enable if it's on this pipe */
506                 //intel_crtc_dpms_video(crtc, TRUE); TODO
507         break;
508         case DPMSModeOff:
509                 /* Give the overlay scaler a chance to disable if it's on this pipe */
510                 //intel_crtc_dpms_video(crtc, FALSE); TODO
511                 
512                 /* Disable the VGA plane that we never use */
513                 I915_WRITE(VGACNTRL, VGA_DISP_DISABLE);
514                 
515                 /* Disable display plane */
516                 temp = I915_READ(dspcntr_reg);
517                 if ((temp & DISPLAY_PLANE_ENABLE) != 0) {
518                         I915_WRITE(dspcntr_reg, temp & ~DISPLAY_PLANE_ENABLE);
519                         /* Flush the plane changes */
520                         I915_WRITE(dspbase_reg, I915_READ(dspbase_reg));
521                         I915_READ(dspbase_reg);
522                 }
523                 
524                 if (!IS_I9XX(dev)) {
525                         /* Wait for vblank for the disable to take effect */
526                         intel_wait_for_vblank(dev);
527                 }
528                 
529                 /* Next, disable display pipes */
530                 temp = I915_READ(pipeconf_reg);
531                 if ((temp & PIPEACONF_ENABLE) != 0) {
532                         I915_WRITE(pipeconf_reg, temp & ~PIPEACONF_ENABLE);
533                         I915_READ(pipeconf_reg);
534                 }
535                 
536                 /* Wait for vblank for the disable to take effect. */
537                 intel_wait_for_vblank(dev);
538                 
539                 temp = I915_READ(dpll_reg);
540                 if ((temp & DPLL_VCO_ENABLE) != 0) {
541                         I915_WRITE(dpll_reg, temp & ~DPLL_VCO_ENABLE);
542                         I915_READ(dpll_reg);
543                 }
544                 
545                 /* Wait for the clocks to turn off. */
546                 udelay(150);
547                 break;
548         }
549
550         if (!dev->primary->master)
551                 return; 
552
553         master_priv = dev->primary->master->driver_priv;
554         if (!master_priv->sarea_priv)
555                 return;
556
557         enabled = crtc->enabled && mode != DPMSModeOff;
558         
559         switch (pipe) {
560         case 0:
561                 master_priv->sarea_priv->planeA_w = enabled ? crtc->mode.hdisplay : 0;
562                 master_priv->sarea_priv->planeA_h = enabled ? crtc->mode.vdisplay : 0;
563                 break;
564         case 1:
565                 master_priv->sarea_priv->planeB_w = enabled ? crtc->mode.hdisplay : 0;
566                 master_priv->sarea_priv->planeB_h = enabled ? crtc->mode.vdisplay : 0;
567                 break;
568         default:
569                 DRM_ERROR("Can't update pipe %d in SAREA\n", pipe);
570                 break;
571         }
572
573         intel_crtc->dpms_mode = mode;
574 }
575
576 static void intel_crtc_prepare (struct drm_crtc *crtc)
577 {
578         crtc->funcs->dpms(crtc, DPMSModeOff);
579 }
580
581 static void intel_crtc_commit (struct drm_crtc *crtc)
582 {
583         crtc->funcs->dpms(crtc, DPMSModeOn);
584 }
585
586 void intel_output_prepare (struct drm_output *output)
587 {
588         /* lvds has its own version of prepare see intel_lvds_prepare */
589         output->funcs->dpms(output, DPMSModeOff);
590 }
591
592 void intel_output_commit (struct drm_output *output)
593 {
594         /* lvds has its own version of commit see intel_lvds_commit */
595         output->funcs->dpms(output, DPMSModeOn);
596 }
597
598 static bool intel_crtc_mode_fixup(struct drm_crtc *crtc,
599                                   struct drm_display_mode *mode,
600                                   struct drm_display_mode *adjusted_mode)
601 {
602         return true;
603 }
604
605
606 /** Returns the core display clock speed for i830 - i945 */
607 static int intel_get_core_clock_speed(struct drm_device *dev)
608 {
609
610         /* Core clock values taken from the published datasheets.
611          * The 830 may go up to 166 Mhz, which we should check.
612          */
613         if (IS_I945G(dev))
614                 return 400000;
615         else if (IS_I915G(dev))
616                 return 333000;
617         else if (IS_I945GM(dev) || IS_845G(dev))
618                 return 200000;
619         else if (IS_I915GM(dev)) {
620                 u16 gcfgc = 0;
621
622                 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
623                 
624                 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
625                         return 133000;
626                 else {
627                         switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
628                         case GC_DISPLAY_CLOCK_333_MHZ:
629                                 return 333000;
630                         default:
631                         case GC_DISPLAY_CLOCK_190_200_MHZ:
632                                 return 190000;
633                         }
634                 }
635         } else if (IS_I865G(dev))
636                 return 266000;
637         else if (IS_I855(dev)) {
638 #if 0
639                 PCITAG bridge = pciTag(0, 0, 0); /* This is always the host bridge */
640                 u16 hpllcc = pciReadWord(bridge, HPLLCC);
641                 
642 #endif
643                 u16 hpllcc = 0;
644                 /* Assume that the hardware is in the high speed state.  This
645                  * should be the default.
646                  */
647                 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
648                 case GC_CLOCK_133_200:
649                 case GC_CLOCK_100_200:
650                         return 200000;
651                 case GC_CLOCK_166_250:
652                         return 250000;
653                 case GC_CLOCK_100_133:
654                         return 133000;
655                 }
656         } else /* 852, 830 */
657                 return 133000;
658         
659         return 0; /* Silence gcc warning */
660 }
661
662
663 /**
664  * Return the pipe currently connected to the panel fitter,
665  * or -1 if the panel fitter is not present or not in use
666  */
667 static int intel_panel_fitter_pipe (struct drm_device *dev)
668 {
669         struct drm_i915_private *dev_priv = dev->dev_private;
670         u32  pfit_control;
671     
672         /* i830 doesn't have a panel fitter */
673         if (IS_I830(dev))
674                 return -1;
675     
676         pfit_control = I915_READ(PFIT_CONTROL);
677     
678         /* See if the panel fitter is in use */
679         if ((pfit_control & PFIT_ENABLE) == 0)
680                 return -1;
681         
682         /* 965 can place panel fitter on either pipe */
683         if (IS_I965G(dev))
684                 return (pfit_control >> 29) & 0x3;
685         
686         /* older chips can only use pipe 1 */
687         return 1;
688 }
689
690 static void intel_crtc_mode_set(struct drm_crtc *crtc,
691                                 struct drm_display_mode *mode,
692                                 struct drm_display_mode *adjusted_mode,
693                                 int x, int y)
694 {
695         struct drm_device *dev = crtc->dev;
696         struct drm_i915_private *dev_priv = dev->dev_private;
697         struct intel_crtc *intel_crtc = crtc->driver_private;
698         int pipe = intel_crtc->pipe;
699         int fp_reg = (pipe == 0) ? FPA0 : FPB0;
700         int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
701         int dpll_md_reg = (intel_crtc->pipe == 0) ? DPLL_A_MD : DPLL_B_MD;
702         int dspcntr_reg = (pipe == 0) ? DSPACNTR : DSPBCNTR;
703         int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
704         int htot_reg = (pipe == 0) ? HTOTAL_A : HTOTAL_B;
705         int hblank_reg = (pipe == 0) ? HBLANK_A : HBLANK_B;
706         int hsync_reg = (pipe == 0) ? HSYNC_A : HSYNC_B;
707         int vtot_reg = (pipe == 0) ? VTOTAL_A : VTOTAL_B;
708         int vblank_reg = (pipe == 0) ? VBLANK_A : VBLANK_B;
709         int vsync_reg = (pipe == 0) ? VSYNC_A : VSYNC_B;
710         int dspsize_reg = (pipe == 0) ? DSPASIZE : DSPBSIZE;
711         int dsppos_reg = (pipe == 0) ? DSPAPOS : DSPBPOS;
712         int pipesrc_reg = (pipe == 0) ? PIPEASRC : PIPEBSRC;
713         int refclk;
714         intel_clock_t clock;
715         u32 dpll = 0, fp = 0, dspcntr, pipeconf;
716         bool ok, is_sdvo = false, is_dvo = false;
717         bool is_crt = false, is_lvds = false, is_tv = false;
718         struct drm_mode_config *mode_config = &dev->mode_config;
719         struct drm_output *output;
720
721         list_for_each_entry(output, &mode_config->output_list, head) {
722                 struct intel_output *intel_output = output->driver_private;
723
724                 if (output->crtc != crtc)
725                         continue;
726
727                 switch (intel_output->type) {
728                 case INTEL_OUTPUT_LVDS:
729                         is_lvds = TRUE;
730                         break;
731                 case INTEL_OUTPUT_SDVO:
732                         is_sdvo = TRUE;
733                         break;
734                 case INTEL_OUTPUT_DVO:
735                         is_dvo = TRUE;
736                         break;
737                 case INTEL_OUTPUT_TVOUT:
738                         is_tv = TRUE;
739                         break;
740                 case INTEL_OUTPUT_ANALOG:
741                         is_crt = TRUE;
742                         break;
743                 }
744         }
745         
746         if (IS_I9XX(dev)) {
747                 refclk = 96000;
748         } else {
749                 refclk = 48000;
750         }
751
752         ok = intel_find_best_PLL(crtc, adjusted_mode->clock, refclk, &clock);
753         if (!ok) {
754                 DRM_ERROR("Couldn't find PLL settings for mode!\n");
755                 return;
756         }
757
758         fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
759         
760         dpll = DPLL_VGA_MODE_DIS;
761         if (IS_I9XX(dev)) {
762                 if (is_lvds)
763                         dpll |= DPLLB_MODE_LVDS;
764                 else
765                         dpll |= DPLLB_MODE_DAC_SERIAL;
766                 if (is_sdvo) {
767                         dpll |= DPLL_DVO_HIGH_SPEED;
768                         if (IS_I945G(dev) || IS_I945GM(dev)) {
769                                 int sdvo_pixel_multiply = adjusted_mode->clock / mode->clock;
770                                 dpll |= (sdvo_pixel_multiply - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
771                         }
772                 }
773                 
774                 /* compute bitmask from p1 value */
775                 dpll |= (1 << (clock.p1 - 1)) << 16;
776                 switch (clock.p2) {
777                 case 5:
778                         dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
779                         break;
780                 case 7:
781                         dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
782                         break;
783                 case 10:
784                         dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
785                         break;
786                 case 14:
787                         dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
788                         break;
789                 }
790                 if (IS_I965G(dev))
791                         dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
792         } else {
793                 if (is_lvds) {
794                         dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
795                 } else {
796                         if (clock.p1 == 2)
797                                 dpll |= PLL_P1_DIVIDE_BY_TWO;
798                         else
799                                 dpll |= (clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
800                         if (clock.p2 == 4)
801                                 dpll |= PLL_P2_DIVIDE_BY_4;
802                 }
803         }
804         
805         if (is_tv) {
806                 /* XXX: just matching BIOS for now */
807 /*      dpll |= PLL_REF_INPUT_TVCLKINBC; */
808                 dpll |= 3;
809         }
810 #if 0
811         else if (is_lvds)
812                 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
813 #endif
814         else
815                 dpll |= PLL_REF_INPUT_DREFCLK;
816         
817         /* setup pipeconf */
818         pipeconf = I915_READ(pipeconf_reg);
819
820         /* Set up the display plane register */
821         dspcntr = DISPPLANE_GAMMA_ENABLE;
822
823         if (pipe == 0)
824                 dspcntr |= DISPPLANE_SEL_PIPE_A;
825         else
826                 dspcntr |= DISPPLANE_SEL_PIPE_B;
827         
828         if (pipe == 0 && !IS_I965G(dev)) {
829                 /* Enable pixel doubling when the dot clock is > 90% of the (display)
830                  * core speed.
831                  *
832                  * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
833                  * pipe == 0 check?
834                  */
835                 if (mode->clock > intel_get_core_clock_speed(dev) * 9 / 10)
836                         pipeconf |= PIPEACONF_DOUBLE_WIDE;
837                 else
838                         pipeconf &= ~PIPEACONF_DOUBLE_WIDE;
839         }
840
841         dspcntr |= DISPLAY_PLANE_ENABLE;
842         pipeconf |= PIPEACONF_ENABLE;
843         dpll |= DPLL_VCO_ENABLE;
844
845         
846         /* Disable the panel fitter if it was on our pipe */
847         if (intel_panel_fitter_pipe(dev) == pipe)
848                 I915_WRITE(PFIT_CONTROL, 0);
849
850         DRM_DEBUG("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
851         drm_mode_debug_printmodeline(mode);
852         
853 #if 0
854         if (!xf86ModesEqual(mode, adjusted_mode)) {
855                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
856                            "Adjusted mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
857                 xf86PrintModeline(pScrn->scrnIndex, mode);
858         }
859         i830PrintPll("chosen", &clock);
860 #endif
861
862         if (dpll & DPLL_VCO_ENABLE) {
863                 I915_WRITE(fp_reg, fp);
864                 I915_WRITE(dpll_reg, dpll & ~DPLL_VCO_ENABLE);
865                 I915_READ(dpll_reg);
866                 udelay(150);
867         }
868         
869         /* The LVDS pin pair needs to be on before the DPLLs are enabled.
870          * This is an exception to the general rule that mode_set doesn't turn
871          * things on.
872          */
873         if (is_lvds) {
874                 u32 lvds = I915_READ(LVDS);
875                 
876                 lvds |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP | LVDS_PIPEB_SELECT;
877                 /* Set the B0-B3 data pairs corresponding to whether we're going to
878                  * set the DPLLs for dual-channel mode or not.
879                  */
880                 if (clock.p2 == 7)
881                         lvds |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
882                 else
883                         lvds &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
884                 
885                 /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
886                  * appropriately here, but we need to look more thoroughly into how
887                  * panels behave in the two modes.
888                  */
889                 
890                 I915_WRITE(LVDS, lvds);
891                 I915_READ(LVDS);
892         }
893         
894         I915_WRITE(fp_reg, fp);
895         I915_WRITE(dpll_reg, dpll);
896         I915_READ(dpll_reg);
897         /* Wait for the clocks to stabilize. */
898         udelay(150);
899         
900         if (IS_I965G(dev)) {
901                 int sdvo_pixel_multiply = adjusted_mode->clock / mode->clock;
902                 I915_WRITE(dpll_md_reg, (0 << DPLL_MD_UDI_DIVIDER_SHIFT) |
903                            ((sdvo_pixel_multiply - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT));
904         } else {
905                 /* write it again -- the BIOS does, after all */
906                 I915_WRITE(dpll_reg, dpll);
907         }
908         I915_READ(dpll_reg);
909         /* Wait for the clocks to stabilize. */
910         udelay(150);
911         
912         I915_WRITE(htot_reg, (adjusted_mode->crtc_hdisplay - 1) |
913                    ((adjusted_mode->crtc_htotal - 1) << 16));
914         I915_WRITE(hblank_reg, (adjusted_mode->crtc_hblank_start - 1) |
915                    ((adjusted_mode->crtc_hblank_end - 1) << 16));
916         I915_WRITE(hsync_reg, (adjusted_mode->crtc_hsync_start - 1) |
917                    ((adjusted_mode->crtc_hsync_end - 1) << 16));
918         I915_WRITE(vtot_reg, (adjusted_mode->crtc_vdisplay - 1) |
919                    ((adjusted_mode->crtc_vtotal - 1) << 16));
920         I915_WRITE(vblank_reg, (adjusted_mode->crtc_vblank_start - 1) |
921                    ((adjusted_mode->crtc_vblank_end - 1) << 16));
922         I915_WRITE(vsync_reg, (adjusted_mode->crtc_vsync_start - 1) |
923                    ((adjusted_mode->crtc_vsync_end - 1) << 16));
924         /* pipesrc and dspsize control the size that is scaled from, which should
925          * always be the user's requested size.
926          */
927         I915_WRITE(dspsize_reg, ((mode->vdisplay - 1) << 16) | (mode->hdisplay - 1));
928         I915_WRITE(dsppos_reg, 0);
929         I915_WRITE(pipesrc_reg, ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
930         I915_WRITE(pipeconf_reg, pipeconf);
931         I915_READ(pipeconf_reg);
932         
933         intel_wait_for_vblank(dev);
934         
935         I915_WRITE(dspcntr_reg, dspcntr);
936         
937         /* Flush the plane changes */
938         intel_pipe_set_base(crtc, x, y);
939         
940         intel_set_vblank(dev);
941
942         intel_wait_for_vblank(dev);    
943 }
944
945 /** Loads the palette/gamma unit for the CRTC with the prepared values */
946 void intel_crtc_load_lut(struct drm_crtc *crtc)
947 {
948         struct drm_device *dev = crtc->dev;
949         struct drm_i915_private *dev_priv = dev->dev_private;
950         struct intel_crtc *intel_crtc = crtc->driver_private;
951         int palreg = (intel_crtc->pipe == 0) ? PALETTE_A : PALETTE_B;
952         int i;
953
954         /* The clocks have to be on to load the palette. */
955         if (!crtc->enabled)
956                 return;
957
958         for (i = 0; i < 256; i++) {
959                 I915_WRITE(palreg + 4 * i,
960                            (intel_crtc->lut_r[i] << 16) |
961                            (intel_crtc->lut_g[i] << 8) |
962                            intel_crtc->lut_b[i]);
963         }
964 }
965
966 static int intel_crtc_cursor_set(struct drm_crtc *crtc,
967                                  struct drm_buffer_object *bo,
968                                  uint32_t width, uint32_t height)
969 {
970         struct drm_device *dev = crtc->dev;
971         struct drm_i915_private *dev_priv = dev->dev_private;
972         struct intel_crtc *intel_crtc = crtc->driver_private;
973         int pipe = intel_crtc->pipe;
974         uint32_t control = (pipe == 0) ? CURACNTR : CURBCNTR;
975         uint32_t base = (pipe == 0) ? CURABASE : CURBBASE;
976         uint32_t temp;
977         size_t addr;
978
979         DRM_DEBUG("\n");
980
981         /* if we want to turn of the cursor ignore width and height */
982         if (!bo) {
983                 DRM_DEBUG("cursor off\n");
984                 /* turn of the cursor */
985                 temp = 0;
986                 temp |= CURSOR_MODE_DISABLE;
987
988                 I915_WRITE(control, temp);
989                 I915_WRITE(base, 0);
990                 return 0;
991         }
992
993         /* Currently we only support 64x64 cursors */
994         if (width != 64 || height != 64) {
995                 DRM_ERROR("we currently only support 64x64 cursors\n");
996                 return -EINVAL;
997         }
998
999         if ((bo->mem.flags & DRM_BO_MASK_MEM) != DRM_BO_FLAG_MEM_VRAM) {
1000                 DRM_ERROR("buffer needs to be in VRAM\n");
1001                 return -ENOMEM;
1002         }
1003
1004         if (bo->mem.size < width * height * 4) {
1005                 DRM_ERROR("buffer is to small\n");
1006                 return -ENOMEM;
1007         }
1008
1009         if (dev_priv->cursor_needs_physical)
1010                 addr = dev_priv->stolen_base + bo->offset;
1011         else
1012                 addr = bo->offset;
1013
1014         intel_crtc->cursor_addr = addr;
1015         temp = 0;
1016         /* set the pipe for the cursor */
1017         temp |= (pipe << 28);
1018         temp |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
1019
1020         I915_WRITE(control, temp);
1021         I915_WRITE(base, addr);
1022
1023         return 0;
1024 }
1025
1026 static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
1027 {
1028         struct drm_device *dev = crtc->dev;
1029         struct drm_i915_private *dev_priv = dev->dev_private;
1030         struct intel_crtc *intel_crtc = crtc->driver_private;
1031         int pipe = intel_crtc->pipe;
1032         uint32_t temp = 0;
1033         uint32_t adder;
1034
1035         if (x < 0) {
1036                 temp |= (CURSOR_POS_SIGN << CURSOR_X_SHIFT);
1037                 x = -x;
1038         }
1039         if (y < 0) {
1040                 temp |= (CURSOR_POS_SIGN << CURSOR_Y_SHIFT);
1041                 y = -y;
1042         }
1043
1044         temp |= ((x & CURSOR_POS_MASK) << CURSOR_X_SHIFT);
1045         temp |= ((y & CURSOR_POS_MASK) << CURSOR_Y_SHIFT);
1046
1047         adder = intel_crtc->cursor_addr;
1048         I915_WRITE((pipe == 0) ? CURAPOS : CURBPOS, temp);
1049         I915_WRITE((pipe == 0) ? CURABASE : CURBBASE, adder);
1050
1051         return 0;
1052 }
1053
1054 /** Sets the color ramps on behalf of RandR */
1055 static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
1056                                  u16 blue, int regno)
1057 {
1058         struct intel_crtc *intel_crtc = crtc->driver_private;
1059         
1060         intel_crtc->lut_r[regno] = red >> 8;
1061         intel_crtc->lut_g[regno] = green >> 8;
1062         intel_crtc->lut_b[regno] = blue >> 8;
1063 }
1064
1065 /**
1066  * Get a pipe with a simple mode set on it for doing load-based monitor
1067  * detection.
1068  *
1069  * It will be up to the load-detect code to adjust the pipe as appropriate for
1070  * its requirements.  The pipe will be connected to no other outputs.
1071  *
1072  * Currently this code will only succeed if there is a pipe with no outputs
1073  * configured for it.  In the future, it could choose to temporarily disable
1074  * some outputs to free up a pipe for its use.
1075  *
1076  * \return crtc, or NULL if no pipes are available.
1077  */
1078     
1079 /* VESA 640x480x72Hz mode to set on the pipe */
1080 static struct drm_display_mode load_detect_mode = {
1081         DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
1082                  704, 832, 0, 480, 489, 491, 520, 0, V_NHSYNC | V_NVSYNC),
1083 };
1084
1085 struct drm_crtc *intel_get_load_detect_pipe(struct drm_output *output,
1086                                             struct drm_display_mode *mode,
1087                                             int *dpms_mode)
1088 {
1089         struct drm_device *dev = output->dev;
1090         struct intel_output *intel_output = output->driver_private;
1091         struct intel_crtc *intel_crtc;
1092         struct drm_crtc *possible_crtc;
1093         struct drm_crtc *supported_crtc =NULL;
1094         struct drm_crtc *crtc = NULL;
1095         struct drm_output_helper_funcs *output_funcs;
1096         int i = -1;
1097
1098         /*
1099          * Algorithm gets a little messy:
1100          *   - if the output already has an assigned crtc, use it (but make
1101          *     sure it's on first)
1102          *   - try to find the first unused crtc that can drive this output,
1103          *     and use that if we find one
1104          *   - if there are no unused crtcs available, try to use the first
1105          *     one we found that supports the output
1106          */
1107
1108         /* See if we already have a CRTC for this output */
1109         if (output->crtc) {
1110                 crtc = output->crtc;
1111                 /* Make sure the crtc and output are running */
1112                 intel_crtc = crtc->driver_private;
1113                 *dpms_mode = intel_crtc->dpms_mode;
1114                 if (intel_crtc->dpms_mode != DPMSModeOn) {
1115                         crtc->funcs->dpms(crtc, DPMSModeOn);
1116                         output->funcs->dpms(output, DPMSModeOn);
1117                 }
1118                 return crtc;
1119         }
1120
1121         /* Find an unused one (if possible) */
1122         list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
1123                 i++;
1124                 if (!(output->possible_crtcs & (1 << i)))
1125                         continue;
1126                 if (!possible_crtc->enabled) {
1127                         crtc = possible_crtc;
1128                         break;
1129                 }
1130                 if (!supported_crtc)
1131                         supported_crtc = possible_crtc;
1132         }
1133
1134         /*
1135          * If we didn't find an unused CRTC, use the first available one
1136          * that can drive this output.
1137          */
1138         if (!crtc) {
1139                 crtc = supported_crtc;
1140                 if (!crtc)
1141                         return NULL;
1142         }
1143
1144         output->crtc = crtc;
1145         intel_output->load_detect_temp = TRUE;
1146     
1147         intel_crtc = crtc->driver_private;
1148         *dpms_mode = intel_crtc->dpms_mode;
1149
1150         if (!crtc->enabled) {
1151                 if (!mode)
1152                         mode = &load_detect_mode;
1153                 drm_crtc_helper_set_mode(crtc, mode, 0, 0);
1154         } else {
1155                 if (intel_crtc->dpms_mode != DPMSModeOn)
1156                         crtc->funcs->dpms(crtc, DPMSModeOn);
1157
1158                 output_funcs = output->helper_private;
1159                 /* Add this output to the crtc */
1160                 output_funcs->mode_set(output, &crtc->mode, &crtc->mode);
1161                 output_funcs->commit(output);
1162         }
1163         /* let the output get through one full cycle before testing */
1164         intel_wait_for_vblank(dev);
1165
1166         return crtc;
1167 }
1168
1169 void intel_release_load_detect_pipe(struct drm_output *output, int dpms_mode)
1170 {
1171         struct drm_device *dev = output->dev;
1172         struct intel_output *intel_output = output->driver_private;
1173         struct drm_crtc *crtc = output->crtc;
1174     
1175         if (intel_output->load_detect_temp) {
1176                 output->crtc = NULL;
1177                 intel_output->load_detect_temp = FALSE;
1178                 crtc->enabled = drm_crtc_in_use(crtc);
1179                 drm_disable_unused_functions(dev);
1180         }
1181
1182         /* Switch crtc and output back off if necessary */
1183         if (crtc->enabled && dpms_mode != DPMSModeOn) {
1184                 if (output->crtc == crtc)
1185                         output->funcs->dpms(output, dpms_mode);
1186                 crtc->funcs->dpms(crtc, dpms_mode);
1187         }
1188 }
1189
1190 /* Returns the clock of the currently programmed mode of the given pipe. */
1191 static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
1192 {
1193         struct drm_i915_private *dev_priv = dev->dev_private;
1194         struct intel_crtc *intel_crtc = crtc->driver_private;
1195         int pipe = intel_crtc->pipe;
1196         u32 dpll = I915_READ((pipe == 0) ? DPLL_A : DPLL_B);
1197         u32 fp;
1198         intel_clock_t clock;
1199
1200         if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
1201                 fp = I915_READ((pipe == 0) ? FPA0 : FPB0);
1202         else
1203                 fp = I915_READ((pipe == 0) ? FPA1 : FPB1);
1204
1205         clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
1206         clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
1207         clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
1208         if (IS_I9XX(dev)) {
1209                 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
1210                                DPLL_FPA01_P1_POST_DIV_SHIFT);
1211
1212                 switch (dpll & DPLL_MODE_MASK) {
1213                 case DPLLB_MODE_DAC_SERIAL:
1214                         clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
1215                                 5 : 10;
1216                         break;
1217                 case DPLLB_MODE_LVDS:
1218                         clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
1219                                 7 : 14;
1220                         break;
1221                 default:
1222                         DRM_DEBUG("Unknown DPLL mode %08x in programmed "
1223                                   "mode\n", (int)(dpll & DPLL_MODE_MASK));
1224                         return 0;
1225                 }
1226
1227                 /* XXX: Handle the 100Mhz refclk */
1228                 i9xx_clock(96000, &clock);
1229         } else {
1230                 bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
1231
1232                 if (is_lvds) {
1233                         clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
1234                                        DPLL_FPA01_P1_POST_DIV_SHIFT);
1235                         clock.p2 = 14;
1236
1237                         if ((dpll & PLL_REF_INPUT_MASK) ==
1238                             PLLB_REF_INPUT_SPREADSPECTRUMIN) {
1239                                 /* XXX: might not be 66MHz */
1240                                 i8xx_clock(66000, &clock);
1241                         } else
1242                                 i8xx_clock(48000, &clock);              
1243                 } else {
1244                         if (dpll & PLL_P1_DIVIDE_BY_TWO)
1245                                 clock.p1 = 2;
1246                         else {
1247                                 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
1248                                             DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
1249                         }
1250                         if (dpll & PLL_P2_DIVIDE_BY_4)
1251                                 clock.p2 = 4;
1252                         else
1253                                 clock.p2 = 2;
1254
1255                         i8xx_clock(48000, &clock);
1256                 }
1257         }
1258
1259         /* XXX: It would be nice to validate the clocks, but we can't reuse
1260          * i830PllIsValid() because it relies on the xf86_config output
1261          * configuration being accurate, which it isn't necessarily.
1262          */
1263
1264         return clock.dot;
1265 }
1266
1267 /** Returns the currently programmed mode of the given pipe. */
1268 struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
1269                                              struct drm_crtc *crtc)
1270 {
1271         struct drm_i915_private *dev_priv = dev->dev_private;
1272         struct intel_crtc *intel_crtc = crtc->driver_private;
1273         int pipe = intel_crtc->pipe;
1274         struct drm_display_mode *mode;
1275         int htot = I915_READ((pipe == 0) ? HTOTAL_A : HTOTAL_B);
1276         int hsync = I915_READ((pipe == 0) ? HSYNC_A : HSYNC_B);
1277         int vtot = I915_READ((pipe == 0) ? VTOTAL_A : VTOTAL_B);
1278         int vsync = I915_READ((pipe == 0) ? VSYNC_A : VSYNC_B);
1279
1280         mode = kzalloc(sizeof(*mode), GFP_KERNEL);
1281         if (!mode)
1282                 return NULL;
1283
1284         mode->clock = intel_crtc_clock_get(dev, crtc);
1285         mode->hdisplay = (htot & 0xffff) + 1;
1286         mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
1287         mode->hsync_start = (hsync & 0xffff) + 1;
1288         mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
1289         mode->vdisplay = (vtot & 0xffff) + 1;
1290         mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
1291         mode->vsync_start = (vsync & 0xffff) + 1;
1292         mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
1293
1294         drm_mode_set_name(mode);
1295         drm_mode_set_crtcinfo(mode, 0);
1296
1297         return mode;
1298 }
1299
1300 static const struct drm_crtc_helper_funcs intel_helper_funcs = {
1301         .mode_fixup = intel_crtc_mode_fixup,
1302         .mode_set = intel_crtc_mode_set,
1303         .mode_set_base = intel_pipe_set_base,
1304         .prepare = intel_crtc_prepare,
1305         .commit = intel_crtc_commit,
1306 };
1307
1308 static const struct drm_crtc_funcs intel_crtc_funcs = {
1309         .dpms = intel_crtc_dpms,
1310         .cursor_set = intel_crtc_cursor_set,
1311         .cursor_move = intel_crtc_cursor_move,
1312         .gamma_set = intel_crtc_gamma_set,
1313         .set_config = drm_crtc_helper_set_config,
1314 };
1315
1316
1317 void intel_crtc_init(struct drm_device *dev, int pipe)
1318 {
1319         struct drm_crtc *crtc;
1320         struct intel_crtc *intel_crtc;
1321         int i;
1322
1323         crtc = drm_crtc_create(dev, &intel_crtc_funcs);
1324         if (crtc == NULL)
1325                 return;
1326
1327         intel_crtc = kzalloc(sizeof(struct intel_crtc), GFP_KERNEL);
1328         if (intel_crtc == NULL) {
1329                 kfree(crtc);
1330                 return;
1331         }
1332
1333         intel_crtc->pipe = pipe;
1334         for (i = 0; i < 256; i++) {
1335                 intel_crtc->lut_r[i] = i;
1336                 intel_crtc->lut_g[i] = i;
1337                 intel_crtc->lut_b[i] = i;
1338         }
1339
1340         intel_crtc->cursor_addr = 0;
1341         intel_crtc->dpms_mode = DPMSModeOff;
1342         drm_crtc_helper_add(crtc, &intel_helper_funcs);
1343
1344         crtc->driver_private = intel_crtc;
1345 }
1346
1347 struct drm_crtc *intel_get_crtc_from_pipe(struct drm_device *dev, int pipe)
1348 {
1349         struct drm_crtc *crtc = NULL;
1350
1351         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1352                 struct intel_crtc *intel_crtc = crtc->driver_private;
1353                 if (intel_crtc->pipe == pipe)
1354                         break;
1355         }
1356         return crtc;
1357 }
1358
1359 int intel_output_clones(struct drm_device *dev, int type_mask)
1360 {
1361         int index_mask = 0;
1362         struct drm_output *output;
1363         int entry = 0;
1364
1365         list_for_each_entry(output, &dev->mode_config.output_list, head) {
1366                 struct intel_output *intel_output = output->driver_private;
1367                 if (type_mask & (1 << intel_output->type))
1368                         index_mask |= (1 << entry);
1369                 entry++;
1370         }
1371         return index_mask;
1372 }
1373
1374
1375 static void intel_setup_outputs(struct drm_device *dev)
1376 {
1377         struct drm_output *output;
1378
1379         intel_crt_init(dev);
1380
1381         /* Set up integrated LVDS */
1382         if (IS_MOBILE(dev) && !IS_I830(dev))
1383                 intel_lvds_init(dev);
1384
1385         if (IS_I9XX(dev)) {
1386                 intel_sdvo_init(dev, SDVOB);
1387                 intel_sdvo_init(dev, SDVOC);
1388         } else
1389                 intel_dvo_init(dev);
1390
1391         if (IS_I9XX(dev) && !IS_I915G(dev))
1392                 intel_tv_init(dev);
1393
1394         list_for_each_entry(output, &dev->mode_config.output_list, head) {
1395                 struct intel_output *intel_output = output->driver_private;
1396                 int crtc_mask = 0, clone_mask = 0;
1397                 
1398                 /* valid crtcs */
1399                 switch(intel_output->type) {
1400                 case INTEL_OUTPUT_DVO:
1401                 case INTEL_OUTPUT_SDVO:
1402                         crtc_mask = ((1 << 0)|
1403                                      (1 << 1));
1404                         clone_mask = ((1 << INTEL_OUTPUT_ANALOG) |
1405                                       (1 << INTEL_OUTPUT_DVO) |
1406                                       (1 << INTEL_OUTPUT_SDVO));
1407                         break;
1408                 case INTEL_OUTPUT_ANALOG:
1409                         crtc_mask = ((1 << 0)|
1410                                      (1 << 1));
1411                         clone_mask = ((1 << INTEL_OUTPUT_ANALOG) |
1412                                       (1 << INTEL_OUTPUT_DVO) |
1413                                       (1 << INTEL_OUTPUT_SDVO));
1414                         break;
1415                 case INTEL_OUTPUT_LVDS:
1416                         crtc_mask = (1 << 1);
1417                         clone_mask = (1 << INTEL_OUTPUT_LVDS);
1418                         break;
1419                 case INTEL_OUTPUT_TVOUT:
1420                         crtc_mask = ((1 << 0) |
1421                                      (1 << 1));
1422                         clone_mask = (1 << INTEL_OUTPUT_TVOUT);
1423                         break;
1424                 }
1425                 output->possible_crtcs = crtc_mask;
1426                 output->possible_clones = intel_output_clones(dev, clone_mask);
1427         }
1428 }
1429
1430 static const struct drm_mode_config_funcs intel_mode_funcs = {
1431         .resize_fb = NULL,
1432 };
1433
1434 void intel_modeset_init(struct drm_device *dev)
1435 {
1436         int num_pipe;
1437         int i;
1438
1439         drm_mode_config_init(dev);
1440
1441         dev->mode_config.min_width = 0;
1442         dev->mode_config.min_height = 0;
1443
1444         dev->mode_config.funcs = (void *)&intel_mode_funcs;
1445
1446         if (IS_I965G(dev)) {
1447                 dev->mode_config.max_width = 8192;
1448                 dev->mode_config.max_height = 8192;
1449         } else {
1450                 dev->mode_config.max_width = 2048;
1451                 dev->mode_config.max_height = 2048;
1452         }
1453
1454         /* set memory base */
1455         if (IS_I9XX(dev))
1456                 dev->mode_config.fb_base = pci_resource_start(dev->pdev, 2);
1457         else
1458                 dev->mode_config.fb_base = pci_resource_start(dev->pdev, 0);
1459
1460         if (IS_MOBILE(dev) || IS_I9XX(dev))
1461                 num_pipe = 2;
1462         else
1463                 num_pipe = 1;
1464         DRM_DEBUG("%d display pipe%s available.\n",
1465                   num_pipe, num_pipe > 1 ? "s" : "");
1466
1467         for (i = 0; i < num_pipe; i++) {
1468                 intel_crtc_init(dev, i);
1469         }
1470
1471         intel_setup_outputs(dev);
1472
1473         //drm_initial_config(dev, false);
1474 }
1475
1476 void intel_modeset_cleanup(struct drm_device *dev)
1477 {
1478         drm_mode_config_cleanup(dev);
1479 }