modesetting-101: rename modeflags, as to avoid conflicts with the xorg definitions
[platform/upstream/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_connector *l_entry;
229
230     list_for_each_entry(l_entry, &mode_config->connector_list, head) {
231             if (l_entry->encoder &&
232                 l_entry->encoder->crtc == crtc) {
233                     struct intel_output *intel_output = to_intel_output(l_entry);
234                     if (intel_output->type == type)
235                             return true;
236             }
237     }
238     return false;
239 }
240
241 #define INTELPllInvalid(s)   { /* ErrorF (s) */; return false; }
242 /**
243  * Returns whether the given set of divisors are valid for a given refclk with
244  * the given connectors.
245  */
246
247 static bool intel_PLL_is_valid(struct drm_crtc *crtc, intel_clock_t *clock)
248 {
249         const intel_limit_t *limit = intel_limit (crtc);
250         
251         if (clock->p1  < limit->p1.min  || limit->p1.max  < clock->p1)
252                 INTELPllInvalid ("p1 out of range\n");
253         if (clock->p   < limit->p.min   || limit->p.max   < clock->p)
254                 INTELPllInvalid ("p out of range\n");
255         if (clock->m2  < limit->m2.min  || limit->m2.max  < clock->m2)
256                 INTELPllInvalid ("m2 out of range\n");
257         if (clock->m1  < limit->m1.min  || limit->m1.max  < clock->m1)
258                 INTELPllInvalid ("m1 out of range\n");
259         if (clock->m1 <= clock->m2)
260                 INTELPllInvalid ("m1 <= m2\n");
261         if (clock->m   < limit->m.min   || limit->m.max   < clock->m)
262                 INTELPllInvalid ("m out of range\n");
263         if (clock->n   < limit->n.min   || limit->n.max   < clock->n)
264                 INTELPllInvalid ("n out of range\n");
265         if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
266                 INTELPllInvalid ("vco out of range\n");
267         /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
268          * connector, etc., rather than just a single range.
269          */
270         if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
271                 INTELPllInvalid ("dot out of range\n");
272         
273         return true;
274 }
275
276 /**
277  * Returns a set of divisors for the desired target clock with the given
278  * refclk, or FALSE.  The returned values represent the clock equation:
279  * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
280  */
281 static bool intel_find_best_PLL(struct drm_crtc *crtc, int target,
282                                 int refclk, intel_clock_t *best_clock)
283 {
284         struct drm_device *dev = crtc->dev;
285         struct drm_i915_private *dev_priv = dev->dev_private;
286         intel_clock_t clock;
287         const intel_limit_t *limit = intel_limit(crtc);
288         int err = target;
289
290         if (IS_I9XX(dev) && intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
291             (I915_READ(LVDS) & LVDS_PORT_EN) != 0) {
292                 /*
293                  * For LVDS, if the panel is on, just rely on its current
294                  * settings for dual-channel.  We haven't figured out how to
295                  * reliably set up different single/dual channel state, if we
296                  * even can.
297                  */
298                 if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
299                     LVDS_CLKB_POWER_UP)
300                         clock.p2 = limit->p2.p2_fast;
301                 else
302                         clock.p2 = limit->p2.p2_slow;
303         } else {
304                 if (target < limit->p2.dot_limit)
305                         clock.p2 = limit->p2.p2_slow;
306                 else
307                         clock.p2 = limit->p2.p2_fast;
308         }
309         
310         memset (best_clock, 0, sizeof (*best_clock));
311         
312         for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
313                 for (clock.m2 = limit->m2.min; clock.m2 < clock.m1 &&
314                              clock.m2 <= limit->m2.max; clock.m2++) {
315                         for (clock.n = limit->n.min; clock.n <= limit->n.max;
316                              clock.n++) {
317                                 for (clock.p1 = limit->p1.min;
318                                      clock.p1 <= limit->p1.max; clock.p1++) {
319                                         int this_err;
320                                         
321                                         intel_clock(dev, refclk, &clock);
322                                         
323                                         if (!intel_PLL_is_valid(crtc, &clock))
324                                                 continue;
325                                         
326                                         this_err = abs(clock.dot - target);
327                                         if (this_err < err) {
328                                                 *best_clock = clock;
329                                                 err = this_err;
330                                         }
331                                 }
332                         }
333                 }
334         }
335
336         return (err != target);
337 }
338
339 void
340 intel_set_vblank(struct drm_device *dev)
341 {
342         struct drm_i915_private *dev_priv = dev->dev_private;
343         struct drm_crtc *crtc;
344         struct intel_crtc *intel_crtc;
345         int vbl_pipe = 0;
346
347         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
348                 intel_crtc = to_intel_crtc(crtc);
349
350                 if (crtc->enabled)
351                         vbl_pipe |= (1<<intel_crtc->pipe);
352         }
353
354         dev_priv->vblank_pipe = vbl_pipe;
355         i915_enable_interrupt(dev);
356 }
357 void
358 intel_wait_for_vblank(struct drm_device *dev)
359 {
360         /* Wait for 20ms, i.e. one cycle at 50hz. */
361         udelay(20000);
362 }
363
364 void
365 intel_pipe_set_base(struct drm_crtc *crtc, int x, int y)
366 {
367         struct drm_device *dev = crtc->dev;
368         struct drm_i915_private *dev_priv = dev->dev_private;
369         struct drm_i915_master_private *master_priv;
370         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
371         struct intel_framebuffer *intel_fb;
372         int pipe = intel_crtc->pipe;
373         unsigned long Start, Offset;
374         int dspbase = (pipe == 0 ? DSPAADDR : DSPBADDR);
375         int dspsurf = (pipe == 0 ? DSPASURF : DSPBSURF);
376         int dspstride = (pipe == 0) ? DSPASTRIDE : DSPBSTRIDE;
377         int dspcntr_reg = (pipe == 0) ? DSPACNTR : DSPBCNTR;
378         u32 dspcntr;
379
380         /* no fb bound */
381         if (!crtc->fb) {
382                 DRM_DEBUG("No FB bound\n");
383                 return;
384         }
385
386         intel_fb = to_intel_framebuffer(crtc->fb);
387
388         Start = intel_fb->bo->offset;
389         Offset = y * crtc->fb->pitch + x * (crtc->fb->bits_per_pixel / 8);
390
391         I915_WRITE(dspstride, crtc->fb->pitch);
392
393         dspcntr = I915_READ(dspcntr_reg);
394         switch (crtc->fb->bits_per_pixel) {
395         case 8:
396                 dspcntr |= DISPPLANE_8BPP;
397                 break;
398         case 16:
399                 if (crtc->fb->depth == 15)
400                         dspcntr |= DISPPLANE_15_16BPP;
401                 else
402                         dspcntr |= DISPPLANE_16BPP;
403                 break;
404         case 24:
405         case 32:
406                 dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
407                 break;
408         default:
409                 DRM_ERROR("Unknown color depth\n");
410                 return;
411         }
412         I915_WRITE(dspcntr_reg, dspcntr);
413
414         DRM_DEBUG("Writing base %08lX %08lX %d %d\n", Start, Offset, x, y);
415         if (IS_I965G(dev)) {
416                 I915_WRITE(dspbase, Offset);
417                 I915_READ(dspbase);
418                 I915_WRITE(dspsurf, Start);
419                 I915_READ(dspsurf);
420         } else {
421                 I915_WRITE(dspbase, Start + Offset);
422                 I915_READ(dspbase);
423         }
424         
425
426         if (!dev->primary->master)
427                 return;
428
429         master_priv = dev->primary->master->driver_priv;
430         if (!master_priv->sarea_priv) 
431                 return;
432                 
433         switch (pipe) {
434         case 0:
435                 master_priv->sarea_priv->planeA_x = x;
436                 master_priv->sarea_priv->planeA_y = y;
437                 break;
438         case 1:
439                 master_priv->sarea_priv->planeB_x = x;
440                 master_priv->sarea_priv->planeB_y = y;
441                 break;
442         default:
443                 DRM_ERROR("Can't update pipe %d in SAREA\n", pipe);
444                 break;
445         }
446 }
447
448
449
450 /**
451  * Sets the power management mode of the pipe and plane.
452  *
453  * This code should probably grow support for turning the cursor off and back
454  * on appropriately at the same time as we're turning the pipe off/on.
455  */
456 static void intel_crtc_dpms(struct drm_crtc *crtc, int mode)
457 {
458         struct drm_device *dev = crtc->dev;
459         struct drm_i915_master_private *master_priv;
460         struct drm_i915_private *dev_priv = dev->dev_private;
461         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
462         int pipe = intel_crtc->pipe;
463         int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
464         int dspcntr_reg = (pipe == 0) ? DSPACNTR : DSPBCNTR;
465         int dspbase_reg = (pipe == 0) ? DSPAADDR : DSPBADDR;
466         int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
467         u32 temp;
468         bool enabled;
469
470         /* XXX: When our outputs are all unaware of DPMS modes other than off
471          * and on, we should map those modes to DPMSModeOff in the CRTC.
472          */
473         switch (mode) {
474         case DPMSModeOn:
475         case DPMSModeStandby:
476         case DPMSModeSuspend:
477                 /* Enable the DPLL */
478                 temp = I915_READ(dpll_reg);
479                 if ((temp & DPLL_VCO_ENABLE) == 0) {
480                         I915_WRITE(dpll_reg, temp);
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                         I915_WRITE(dpll_reg, temp | DPLL_VCO_ENABLE);
489                         I915_READ(dpll_reg);
490                         /* Wait for the clocks to stabilize. */
491                         udelay(150);
492                 }
493                 
494                 /* Enable the pipe */
495                 temp = I915_READ(pipeconf_reg);
496                 if ((temp & PIPEACONF_ENABLE) == 0)
497                         I915_WRITE(pipeconf_reg, temp | PIPEACONF_ENABLE);
498                 
499                 /* Enable the plane */
500                 temp = I915_READ(dspcntr_reg);
501                 if ((temp & DISPLAY_PLANE_ENABLE) == 0) {
502                         I915_WRITE(dspcntr_reg, temp | DISPLAY_PLANE_ENABLE);
503                         /* Flush the plane changes */
504                         I915_WRITE(dspbase_reg, I915_READ(dspbase_reg));
505                 }
506                 
507                 intel_crtc_load_lut(crtc);
508                 
509                 /* Give the overlay scaler a chance to enable if it's on this pipe */
510                 //intel_crtc_dpms_video(crtc, TRUE); TODO
511         break;
512         case DPMSModeOff:
513                 /* Give the overlay scaler a chance to disable if it's on this pipe */
514                 //intel_crtc_dpms_video(crtc, FALSE); TODO
515                 
516                 /* Disable the VGA plane that we never use */
517                 I915_WRITE(VGACNTRL, VGA_DISP_DISABLE);
518                 
519                 /* Disable display plane */
520                 temp = I915_READ(dspcntr_reg);
521                 if ((temp & DISPLAY_PLANE_ENABLE) != 0) {
522                         I915_WRITE(dspcntr_reg, temp & ~DISPLAY_PLANE_ENABLE);
523                         /* Flush the plane changes */
524                         I915_WRITE(dspbase_reg, I915_READ(dspbase_reg));
525                         I915_READ(dspbase_reg);
526                 }
527                 
528                 if (!IS_I9XX(dev)) {
529                         /* Wait for vblank for the disable to take effect */
530                         intel_wait_for_vblank(dev);
531                 }
532                 
533                 /* Next, disable display pipes */
534                 temp = I915_READ(pipeconf_reg);
535                 if ((temp & PIPEACONF_ENABLE) != 0) {
536                         I915_WRITE(pipeconf_reg, temp & ~PIPEACONF_ENABLE);
537                         I915_READ(pipeconf_reg);
538                 }
539                 
540                 /* Wait for vblank for the disable to take effect. */
541                 intel_wait_for_vblank(dev);
542                 
543                 temp = I915_READ(dpll_reg);
544                 if ((temp & DPLL_VCO_ENABLE) != 0) {
545                         I915_WRITE(dpll_reg, temp & ~DPLL_VCO_ENABLE);
546                         I915_READ(dpll_reg);
547                 }
548                 
549                 /* Wait for the clocks to turn off. */
550                 udelay(150);
551                 break;
552         }
553
554         if (!dev->primary->master)
555                 return; 
556
557         master_priv = dev->primary->master->driver_priv;
558         if (!master_priv->sarea_priv)
559                 return;
560
561         enabled = crtc->enabled && mode != DPMSModeOff;
562         
563         switch (pipe) {
564         case 0:
565                 master_priv->sarea_priv->planeA_w = enabled ? crtc->mode.hdisplay : 0;
566                 master_priv->sarea_priv->planeA_h = enabled ? crtc->mode.vdisplay : 0;
567                 break;
568         case 1:
569                 master_priv->sarea_priv->planeB_w = enabled ? crtc->mode.hdisplay : 0;
570                 master_priv->sarea_priv->planeB_h = enabled ? crtc->mode.vdisplay : 0;
571                 break;
572         default:
573                 DRM_ERROR("Can't update pipe %d in SAREA\n", pipe);
574                 break;
575         }
576
577         intel_crtc->dpms_mode = mode;
578 }
579
580 static void intel_crtc_prepare (struct drm_crtc *crtc)
581 {
582         struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
583         crtc_funcs->dpms(crtc, DPMSModeOff);
584 }
585
586 static void intel_crtc_commit (struct drm_crtc *crtc)
587 {
588         struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
589         crtc_funcs->dpms(crtc, DPMSModeOn);
590 }
591
592 void intel_encoder_prepare (struct drm_encoder *encoder)
593 {
594         struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
595         /* lvds has its own version of prepare see intel_lvds_prepare */
596         encoder_funcs->dpms(encoder, DPMSModeOff);
597 }
598
599 void intel_encoder_commit (struct drm_encoder *encoder)
600 {
601         struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
602         /* lvds has its own version of commit see intel_lvds_commit */
603         encoder_funcs->dpms(encoder, DPMSModeOn);
604 }
605
606 static bool intel_crtc_mode_fixup(struct drm_crtc *crtc,
607                                   struct drm_display_mode *mode,
608                                   struct drm_display_mode *adjusted_mode)
609 {
610         return true;
611 }
612
613
614 /** Returns the core display clock speed for i830 - i945 */
615 static int intel_get_core_clock_speed(struct drm_device *dev)
616 {
617
618         /* Core clock values taken from the published datasheets.
619          * The 830 may go up to 166 Mhz, which we should check.
620          */
621         if (IS_I945G(dev))
622                 return 400000;
623         else if (IS_I915G(dev))
624                 return 333000;
625         else if (IS_I945GM(dev) || IS_845G(dev))
626                 return 200000;
627         else if (IS_I915GM(dev)) {
628                 u16 gcfgc = 0;
629
630                 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
631                 
632                 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
633                         return 133000;
634                 else {
635                         switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
636                         case GC_DISPLAY_CLOCK_333_MHZ:
637                                 return 333000;
638                         default:
639                         case GC_DISPLAY_CLOCK_190_200_MHZ:
640                                 return 190000;
641                         }
642                 }
643         } else if (IS_I865G(dev))
644                 return 266000;
645         else if (IS_I855(dev)) {
646 #if 0
647                 PCITAG bridge = pciTag(0, 0, 0); /* This is always the host bridge */
648                 u16 hpllcc = pciReadWord(bridge, HPLLCC);
649                 
650 #endif
651                 u16 hpllcc = 0;
652                 /* Assume that the hardware is in the high speed state.  This
653                  * should be the default.
654                  */
655                 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
656                 case GC_CLOCK_133_200:
657                 case GC_CLOCK_100_200:
658                         return 200000;
659                 case GC_CLOCK_166_250:
660                         return 250000;
661                 case GC_CLOCK_100_133:
662                         return 133000;
663                 }
664         } else /* 852, 830 */
665                 return 133000;
666         
667         return 0; /* Silence gcc warning */
668 }
669
670
671 /**
672  * Return the pipe currently connected to the panel fitter,
673  * or -1 if the panel fitter is not present or not in use
674  */
675 static int intel_panel_fitter_pipe (struct drm_device *dev)
676 {
677         struct drm_i915_private *dev_priv = dev->dev_private;
678         u32  pfit_control;
679     
680         /* i830 doesn't have a panel fitter */
681         if (IS_I830(dev))
682                 return -1;
683     
684         pfit_control = I915_READ(PFIT_CONTROL);
685     
686         /* See if the panel fitter is in use */
687         if ((pfit_control & PFIT_ENABLE) == 0)
688                 return -1;
689         
690         /* 965 can place panel fitter on either pipe */
691         if (IS_I965G(dev))
692                 return (pfit_control >> 29) & 0x3;
693         
694         /* older chips can only use pipe 1 */
695         return 1;
696 }
697
698 static void intel_crtc_mode_set(struct drm_crtc *crtc,
699                                 struct drm_display_mode *mode,
700                                 struct drm_display_mode *adjusted_mode,
701                                 int x, int y)
702 {
703         struct drm_device *dev = crtc->dev;
704         struct drm_i915_private *dev_priv = dev->dev_private;
705         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
706         int pipe = intel_crtc->pipe;
707         int fp_reg = (pipe == 0) ? FPA0 : FPB0;
708         int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
709         int dpll_md_reg = (intel_crtc->pipe == 0) ? DPLL_A_MD : DPLL_B_MD;
710         int dspcntr_reg = (pipe == 0) ? DSPACNTR : DSPBCNTR;
711         int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
712         int htot_reg = (pipe == 0) ? HTOTAL_A : HTOTAL_B;
713         int hblank_reg = (pipe == 0) ? HBLANK_A : HBLANK_B;
714         int hsync_reg = (pipe == 0) ? HSYNC_A : HSYNC_B;
715         int vtot_reg = (pipe == 0) ? VTOTAL_A : VTOTAL_B;
716         int vblank_reg = (pipe == 0) ? VBLANK_A : VBLANK_B;
717         int vsync_reg = (pipe == 0) ? VSYNC_A : VSYNC_B;
718         int dspsize_reg = (pipe == 0) ? DSPASIZE : DSPBSIZE;
719         int dsppos_reg = (pipe == 0) ? DSPAPOS : DSPBPOS;
720         int pipesrc_reg = (pipe == 0) ? PIPEASRC : PIPEBSRC;
721         int refclk;
722         intel_clock_t clock;
723         u32 dpll = 0, fp = 0, dspcntr, pipeconf;
724         bool ok, is_sdvo = false, is_dvo = false;
725         bool is_crt = false, is_lvds = false, is_tv = false;
726         struct drm_mode_config *mode_config = &dev->mode_config;
727         struct drm_connector *connector;
728
729         list_for_each_entry(connector, &mode_config->connector_list, head) {
730                 struct intel_output *intel_output = to_intel_output(connector);
731
732                 if (!connector->encoder || connector->encoder->crtc != crtc)
733                         continue;
734
735                 switch (intel_output->type) {
736                 case INTEL_OUTPUT_LVDS:
737                         is_lvds = TRUE;
738                         break;
739                 case INTEL_OUTPUT_SDVO:
740                         is_sdvo = TRUE;
741                         break;
742                 case INTEL_OUTPUT_DVO:
743                         is_dvo = TRUE;
744                         break;
745                 case INTEL_OUTPUT_TVOUT:
746                         is_tv = TRUE;
747                         break;
748                 case INTEL_OUTPUT_ANALOG:
749                         is_crt = TRUE;
750                         break;
751                 }
752         }
753         
754         if (IS_I9XX(dev)) {
755                 refclk = 96000;
756         } else {
757                 refclk = 48000;
758         }
759
760         ok = intel_find_best_PLL(crtc, adjusted_mode->clock, refclk, &clock);
761         if (!ok) {
762                 DRM_ERROR("Couldn't find PLL settings for mode!\n");
763                 return;
764         }
765
766         fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
767         
768         dpll = DPLL_VGA_MODE_DIS;
769         if (IS_I9XX(dev)) {
770                 if (is_lvds)
771                         dpll |= DPLLB_MODE_LVDS;
772                 else
773                         dpll |= DPLLB_MODE_DAC_SERIAL;
774                 if (is_sdvo) {
775                         dpll |= DPLL_DVO_HIGH_SPEED;
776                         if (IS_I945G(dev) || IS_I945GM(dev)) {
777                                 int sdvo_pixel_multiply = adjusted_mode->clock / mode->clock;
778                                 dpll |= (sdvo_pixel_multiply - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
779                         }
780                 }
781                 
782                 /* compute bitmask from p1 value */
783                 dpll |= (1 << (clock.p1 - 1)) << 16;
784                 switch (clock.p2) {
785                 case 5:
786                         dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
787                         break;
788                 case 7:
789                         dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
790                         break;
791                 case 10:
792                         dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
793                         break;
794                 case 14:
795                         dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
796                         break;
797                 }
798                 if (IS_I965G(dev))
799                         dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
800         } else {
801                 if (is_lvds) {
802                         dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
803                 } else {
804                         if (clock.p1 == 2)
805                                 dpll |= PLL_P1_DIVIDE_BY_TWO;
806                         else
807                                 dpll |= (clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
808                         if (clock.p2 == 4)
809                                 dpll |= PLL_P2_DIVIDE_BY_4;
810                 }
811         }
812         
813         if (is_tv) {
814                 /* XXX: just matching BIOS for now */
815 /*      dpll |= PLL_REF_INPUT_TVCLKINBC; */
816                 dpll |= 3;
817         }
818 #if 0
819         else if (is_lvds)
820                 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
821 #endif
822         else
823                 dpll |= PLL_REF_INPUT_DREFCLK;
824         
825         /* setup pipeconf */
826         pipeconf = I915_READ(pipeconf_reg);
827
828         /* Set up the display plane register */
829         dspcntr = DISPPLANE_GAMMA_ENABLE;
830
831         if (pipe == 0)
832                 dspcntr |= DISPPLANE_SEL_PIPE_A;
833         else
834                 dspcntr |= DISPPLANE_SEL_PIPE_B;
835         
836         if (pipe == 0 && !IS_I965G(dev)) {
837                 /* Enable pixel doubling when the dot clock is > 90% of the (display)
838                  * core speed.
839                  *
840                  * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
841                  * pipe == 0 check?
842                  */
843                 if (mode->clock > intel_get_core_clock_speed(dev) * 9 / 10)
844                         pipeconf |= PIPEACONF_DOUBLE_WIDE;
845                 else
846                         pipeconf &= ~PIPEACONF_DOUBLE_WIDE;
847         }
848
849         dspcntr |= DISPLAY_PLANE_ENABLE;
850         pipeconf |= PIPEACONF_ENABLE;
851         dpll |= DPLL_VCO_ENABLE;
852
853         
854         /* Disable the panel fitter if it was on our pipe */
855         if (intel_panel_fitter_pipe(dev) == pipe)
856                 I915_WRITE(PFIT_CONTROL, 0);
857
858         DRM_DEBUG("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
859         drm_mode_debug_printmodeline(mode);
860         
861 #if 0
862         if (!xf86ModesEqual(mode, adjusted_mode)) {
863                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
864                            "Adjusted mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
865                 xf86PrintModeline(pScrn->scrnIndex, mode);
866         }
867         i830PrintPll("chosen", &clock);
868 #endif
869
870         if (dpll & DPLL_VCO_ENABLE) {
871                 I915_WRITE(fp_reg, fp);
872                 I915_WRITE(dpll_reg, dpll & ~DPLL_VCO_ENABLE);
873                 I915_READ(dpll_reg);
874                 udelay(150);
875         }
876         
877         /* The LVDS pin pair needs to be on before the DPLLs are enabled.
878          * This is an exception to the general rule that mode_set doesn't turn
879          * things on.
880          */
881         if (is_lvds) {
882                 u32 lvds = I915_READ(LVDS);
883                 
884                 lvds |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP | LVDS_PIPEB_SELECT;
885                 /* Set the B0-B3 data pairs corresponding to whether we're going to
886                  * set the DPLLs for dual-channel mode or not.
887                  */
888                 if (clock.p2 == 7)
889                         lvds |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
890                 else
891                         lvds &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
892                 
893                 /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
894                  * appropriately here, but we need to look more thoroughly into how
895                  * panels behave in the two modes.
896                  */
897                 
898                 I915_WRITE(LVDS, lvds);
899                 I915_READ(LVDS);
900         }
901         
902         I915_WRITE(fp_reg, fp);
903         I915_WRITE(dpll_reg, dpll);
904         I915_READ(dpll_reg);
905         /* Wait for the clocks to stabilize. */
906         udelay(150);
907         
908         if (IS_I965G(dev)) {
909                 int sdvo_pixel_multiply = adjusted_mode->clock / mode->clock;
910                 I915_WRITE(dpll_md_reg, (0 << DPLL_MD_UDI_DIVIDER_SHIFT) |
911                            ((sdvo_pixel_multiply - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT));
912         } else {
913                 /* write it again -- the BIOS does, after all */
914                 I915_WRITE(dpll_reg, dpll);
915         }
916         I915_READ(dpll_reg);
917         /* Wait for the clocks to stabilize. */
918         udelay(150);
919         
920         I915_WRITE(htot_reg, (adjusted_mode->crtc_hdisplay - 1) |
921                    ((adjusted_mode->crtc_htotal - 1) << 16));
922         I915_WRITE(hblank_reg, (adjusted_mode->crtc_hblank_start - 1) |
923                    ((adjusted_mode->crtc_hblank_end - 1) << 16));
924         I915_WRITE(hsync_reg, (adjusted_mode->crtc_hsync_start - 1) |
925                    ((adjusted_mode->crtc_hsync_end - 1) << 16));
926         I915_WRITE(vtot_reg, (adjusted_mode->crtc_vdisplay - 1) |
927                    ((adjusted_mode->crtc_vtotal - 1) << 16));
928         I915_WRITE(vblank_reg, (adjusted_mode->crtc_vblank_start - 1) |
929                    ((adjusted_mode->crtc_vblank_end - 1) << 16));
930         I915_WRITE(vsync_reg, (adjusted_mode->crtc_vsync_start - 1) |
931                    ((adjusted_mode->crtc_vsync_end - 1) << 16));
932         /* pipesrc and dspsize control the size that is scaled from, which should
933          * always be the user's requested size.
934          */
935         I915_WRITE(dspsize_reg, ((mode->vdisplay - 1) << 16) | (mode->hdisplay - 1));
936         I915_WRITE(dsppos_reg, 0);
937         I915_WRITE(pipesrc_reg, ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
938         I915_WRITE(pipeconf_reg, pipeconf);
939         I915_READ(pipeconf_reg);
940         
941         intel_wait_for_vblank(dev);
942         
943         I915_WRITE(dspcntr_reg, dspcntr);
944         
945         /* Flush the plane changes */
946         intel_pipe_set_base(crtc, x, y);
947         
948         intel_set_vblank(dev);
949
950         intel_wait_for_vblank(dev);    
951 }
952
953 /** Loads the palette/gamma unit for the CRTC with the prepared values */
954 void intel_crtc_load_lut(struct drm_crtc *crtc)
955 {
956         struct drm_device *dev = crtc->dev;
957         struct drm_i915_private *dev_priv = dev->dev_private;
958         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
959         int palreg = (intel_crtc->pipe == 0) ? PALETTE_A : PALETTE_B;
960         int i;
961
962         /* The clocks have to be on to load the palette. */
963         if (!crtc->enabled)
964                 return;
965
966         for (i = 0; i < 256; i++) {
967                 I915_WRITE(palreg + 4 * i,
968                            (intel_crtc->lut_r[i] << 16) |
969                            (intel_crtc->lut_g[i] << 8) |
970                            intel_crtc->lut_b[i]);
971         }
972 }
973
974 static int intel_crtc_cursor_set(struct drm_crtc *crtc,
975                                  uint32_t handle,
976                                  uint32_t width, uint32_t height)
977 {
978         struct drm_device *dev = crtc->dev;
979         struct drm_i915_private *dev_priv = dev->dev_private;
980         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
981         struct drm_buffer_object *bo;
982         int pipe = intel_crtc->pipe;
983         uint32_t control = (pipe == 0) ? CURACNTR : CURBCNTR;
984         uint32_t base = (pipe == 0) ? CURABASE : CURBBASE;
985         uint32_t temp;
986         int ret;
987         size_t addr;
988
989         DRM_DEBUG("\n");
990
991         /* if we want to turn of the cursor ignore width and height */
992         if (!handle) {
993                 DRM_DEBUG("cursor off\n");
994                 /* turn of the cursor */
995                 temp = 0;
996                 temp |= CURSOR_MODE_DISABLE;
997
998                 I915_WRITE(control, temp);
999                 I915_WRITE(base, 0);
1000                 return 0;
1001         }
1002
1003         /* Currently we only support 64x64 cursors */
1004         if (width != 64 || height != 64) {
1005                 DRM_ERROR("we currently only support 64x64 cursors\n");
1006                 return -EINVAL;
1007         }
1008
1009         ret = drm_get_buffer_object(dev, &bo, handle);
1010         if (ret) {
1011                 return -EINVAL;
1012         }
1013
1014         if ((bo->mem.flags & DRM_BO_MASK_MEM) != DRM_BO_FLAG_MEM_VRAM) {
1015                 DRM_ERROR("buffer needs to be in VRAM\n");
1016                 return -ENOMEM;
1017         }
1018
1019         if (bo->mem.size < width * height * 4) {
1020                 DRM_ERROR("buffer is to small\n");
1021                 return -ENOMEM;
1022         }
1023
1024         if (dev_priv->cursor_needs_physical)
1025                 addr = dev_priv->stolen_base + bo->offset;
1026         else
1027                 addr = bo->offset;
1028
1029         intel_crtc->cursor_addr = addr;
1030         temp = 0;
1031         /* set the pipe for the cursor */
1032         temp |= (pipe << 28);
1033         temp |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
1034
1035         I915_WRITE(control, temp);
1036         I915_WRITE(base, addr);
1037
1038         return 0;
1039 }
1040
1041 static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
1042 {
1043         struct drm_device *dev = crtc->dev;
1044         struct drm_i915_private *dev_priv = dev->dev_private;
1045         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1046         int pipe = intel_crtc->pipe;
1047         uint32_t temp = 0;
1048         uint32_t adder;
1049
1050         if (x < 0) {
1051                 temp |= (CURSOR_POS_SIGN << CURSOR_X_SHIFT);
1052                 x = -x;
1053         }
1054         if (y < 0) {
1055                 temp |= (CURSOR_POS_SIGN << CURSOR_Y_SHIFT);
1056                 y = -y;
1057         }
1058
1059         temp |= ((x & CURSOR_POS_MASK) << CURSOR_X_SHIFT);
1060         temp |= ((y & CURSOR_POS_MASK) << CURSOR_Y_SHIFT);
1061
1062         adder = intel_crtc->cursor_addr;
1063         I915_WRITE((pipe == 0) ? CURAPOS : CURBPOS, temp);
1064         I915_WRITE((pipe == 0) ? CURABASE : CURBBASE, adder);
1065
1066         return 0;
1067 }
1068
1069 /** Sets the color ramps on behalf of RandR */
1070 void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
1071                                  u16 blue, int regno)
1072 {
1073         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1074
1075         intel_crtc->lut_r[regno] = red >> 8;
1076         intel_crtc->lut_g[regno] = green >> 8;
1077         intel_crtc->lut_b[regno] = blue >> 8;
1078 }
1079
1080 static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
1081                                  u16 *blue, uint32_t size)
1082 {
1083         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1084         int i;
1085
1086         if (size != 256)
1087                 return;
1088
1089         for (i = 0; i < 256; i++) {
1090                 intel_crtc->lut_r[i] = red[i] >> 8;
1091                 intel_crtc->lut_g[i] = green[i] >> 8;
1092                 intel_crtc->lut_b[i] = blue[i] >> 8;
1093         }
1094
1095         intel_crtc_load_lut(crtc);
1096 }
1097
1098 /**
1099  * Get a pipe with a simple mode set on it for doing load-based monitor
1100  * detection.
1101  *
1102  * It will be up to the load-detect code to adjust the pipe as appropriate for
1103  * its requirements.  The pipe will be connected to no other outputs.
1104  *
1105  * Currently this code will only succeed if there is a pipe with no outputs
1106  * configured for it.  In the future, it could choose to temporarily disable
1107  * some outputs to free up a pipe for its use.
1108  *
1109  * \return crtc, or NULL if no pipes are available.
1110  */
1111     
1112 /* VESA 640x480x72Hz mode to set on the pipe */
1113 static struct drm_display_mode load_detect_mode = {
1114         DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
1115                  704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
1116 };
1117
1118 struct drm_crtc *intel_get_load_detect_pipe(struct intel_output *intel_output,
1119                                             struct drm_display_mode *mode,
1120                                             int *dpms_mode)
1121 {
1122         struct intel_crtc *intel_crtc;
1123         struct drm_crtc *possible_crtc;
1124         struct drm_crtc *supported_crtc =NULL;
1125         struct drm_encoder *encoder = &intel_output->enc;
1126         struct drm_crtc *crtc = NULL;
1127         struct drm_device *dev = encoder->dev;
1128         struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
1129         struct drm_crtc_helper_funcs *crtc_funcs;
1130         int i = -1;
1131
1132         /*
1133          * Algorithm gets a little messy:
1134          *   - if the connector already has an assigned crtc, use it (but make
1135          *     sure it's on first)
1136          *   - try to find the first unused crtc that can drive this connector,
1137          *     and use that if we find one
1138          *   - if there are no unused crtcs available, try to use the first
1139          *     one we found that supports the connector
1140          */
1141
1142         /* See if we already have a CRTC for this connector */
1143         if (encoder->crtc) {
1144                 crtc = encoder->crtc;
1145                 /* Make sure the crtc and connector are running */
1146                 intel_crtc = to_intel_crtc(crtc);
1147                 *dpms_mode = intel_crtc->dpms_mode;
1148                 if (intel_crtc->dpms_mode != DPMSModeOn) {
1149                         crtc_funcs = crtc->helper_private;
1150                         crtc_funcs->dpms(crtc, DPMSModeOn);
1151                         encoder_funcs->dpms(encoder, DPMSModeOn);
1152                 }
1153                 return crtc;
1154         }
1155
1156         /* Find an unused one (if possible) */
1157         list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
1158                 i++;
1159                 if (!(encoder->possible_crtcs & (1 << i)))
1160                         continue;
1161                 if (!possible_crtc->enabled) {
1162                         crtc = possible_crtc;
1163                         break;
1164                 }
1165                 if (!supported_crtc)
1166                         supported_crtc = possible_crtc;
1167         }
1168
1169         /*
1170          * If we didn't find an unused CRTC, don't use any.
1171          */
1172         if (!crtc) {
1173                 return NULL;
1174         }
1175
1176         encoder->crtc = crtc;
1177         intel_output->load_detect_temp = TRUE;
1178     
1179         intel_crtc = to_intel_crtc(crtc);
1180         *dpms_mode = intel_crtc->dpms_mode;
1181
1182         if (!crtc->enabled) {
1183                 if (!mode)
1184                         mode = &load_detect_mode;
1185                 drm_crtc_helper_set_mode(crtc, mode, 0, 0);
1186         } else {
1187                 if (intel_crtc->dpms_mode != DPMSModeOn) {
1188                         crtc_funcs = crtc->helper_private;
1189                         crtc_funcs->dpms(crtc, DPMSModeOn);
1190                 }
1191
1192                 /* Add this connector to the crtc */
1193                 encoder_funcs->mode_set(encoder, &crtc->mode, &crtc->mode);
1194                 encoder_funcs->commit(encoder);
1195         }
1196         /* let the connector get through one full cycle before testing */
1197         intel_wait_for_vblank(dev);
1198
1199         return crtc;
1200 }
1201
1202 void intel_release_load_detect_pipe(struct intel_output *intel_output, int dpms_mode)
1203 {
1204         struct drm_encoder *encoder = &intel_output->enc;
1205         struct drm_device *dev = encoder->dev;
1206         struct drm_crtc *crtc = encoder->crtc;
1207         struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
1208         struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
1209     
1210         if (intel_output->load_detect_temp) {
1211                 encoder->crtc = NULL;
1212                 intel_output->load_detect_temp = FALSE;
1213                 crtc->enabled = drm_helper_crtc_in_use(crtc);
1214                 drm_helper_disable_unused_functions(dev);
1215         }
1216
1217         /* Switch crtc and output back off if necessary */
1218         if (crtc->enabled && dpms_mode != DPMSModeOn) {
1219                 if (encoder->crtc == crtc)
1220                         encoder_funcs->dpms(encoder, dpms_mode);
1221                 crtc_funcs->dpms(crtc, dpms_mode);
1222         }
1223 }
1224
1225 /* Returns the clock of the currently programmed mode of the given pipe. */
1226 static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
1227 {
1228         struct drm_i915_private *dev_priv = dev->dev_private;
1229         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1230         int pipe = intel_crtc->pipe;
1231         u32 dpll = I915_READ((pipe == 0) ? DPLL_A : DPLL_B);
1232         u32 fp;
1233         intel_clock_t clock;
1234
1235         if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
1236                 fp = I915_READ((pipe == 0) ? FPA0 : FPB0);
1237         else
1238                 fp = I915_READ((pipe == 0) ? FPA1 : FPB1);
1239
1240         clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
1241         clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
1242         clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
1243         if (IS_I9XX(dev)) {
1244                 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
1245                                DPLL_FPA01_P1_POST_DIV_SHIFT);
1246
1247                 switch (dpll & DPLL_MODE_MASK) {
1248                 case DPLLB_MODE_DAC_SERIAL:
1249                         clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
1250                                 5 : 10;
1251                         break;
1252                 case DPLLB_MODE_LVDS:
1253                         clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
1254                                 7 : 14;
1255                         break;
1256                 default:
1257                         DRM_DEBUG("Unknown DPLL mode %08x in programmed "
1258                                   "mode\n", (int)(dpll & DPLL_MODE_MASK));
1259                         return 0;
1260                 }
1261
1262                 /* XXX: Handle the 100Mhz refclk */
1263                 i9xx_clock(96000, &clock);
1264         } else {
1265                 bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
1266
1267                 if (is_lvds) {
1268                         clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
1269                                        DPLL_FPA01_P1_POST_DIV_SHIFT);
1270                         clock.p2 = 14;
1271
1272                         if ((dpll & PLL_REF_INPUT_MASK) ==
1273                             PLLB_REF_INPUT_SPREADSPECTRUMIN) {
1274                                 /* XXX: might not be 66MHz */
1275                                 i8xx_clock(66000, &clock);
1276                         } else
1277                                 i8xx_clock(48000, &clock);              
1278                 } else {
1279                         if (dpll & PLL_P1_DIVIDE_BY_TWO)
1280                                 clock.p1 = 2;
1281                         else {
1282                                 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
1283                                             DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
1284                         }
1285                         if (dpll & PLL_P2_DIVIDE_BY_4)
1286                                 clock.p2 = 4;
1287                         else
1288                                 clock.p2 = 2;
1289
1290                         i8xx_clock(48000, &clock);
1291                 }
1292         }
1293
1294         /* XXX: It would be nice to validate the clocks, but we can't reuse
1295          * i830PllIsValid() because it relies on the xf86_config connector
1296          * configuration being accurate, which it isn't necessarily.
1297          */
1298
1299         return clock.dot;
1300 }
1301
1302 /** Returns the currently programmed mode of the given pipe. */
1303 struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
1304                                              struct drm_crtc *crtc)
1305 {
1306         struct drm_i915_private *dev_priv = dev->dev_private;
1307         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1308         int pipe = intel_crtc->pipe;
1309         struct drm_display_mode *mode;
1310         int htot = I915_READ((pipe == 0) ? HTOTAL_A : HTOTAL_B);
1311         int hsync = I915_READ((pipe == 0) ? HSYNC_A : HSYNC_B);
1312         int vtot = I915_READ((pipe == 0) ? VTOTAL_A : VTOTAL_B);
1313         int vsync = I915_READ((pipe == 0) ? VSYNC_A : VSYNC_B);
1314
1315         mode = kzalloc(sizeof(*mode), GFP_KERNEL);
1316         if (!mode)
1317                 return NULL;
1318
1319         mode->clock = intel_crtc_clock_get(dev, crtc);
1320         mode->hdisplay = (htot & 0xffff) + 1;
1321         mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
1322         mode->hsync_start = (hsync & 0xffff) + 1;
1323         mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
1324         mode->vdisplay = (vtot & 0xffff) + 1;
1325         mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
1326         mode->vsync_start = (vsync & 0xffff) + 1;
1327         mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
1328
1329         drm_mode_set_name(mode);
1330         drm_mode_set_crtcinfo(mode, 0);
1331
1332         return mode;
1333 }
1334
1335 static void intel_crtc_destroy(struct drm_crtc *crtc)
1336 {
1337         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1338
1339         drm_crtc_cleanup(crtc);
1340         kfree(intel_crtc);
1341 }
1342
1343 static const struct drm_crtc_helper_funcs intel_helper_funcs = {
1344         .dpms = intel_crtc_dpms,
1345         .mode_fixup = intel_crtc_mode_fixup,
1346         .mode_set = intel_crtc_mode_set,
1347         .mode_set_base = intel_pipe_set_base,
1348         .prepare = intel_crtc_prepare,
1349         .commit = intel_crtc_commit,
1350 };
1351
1352 static const struct drm_crtc_funcs intel_crtc_funcs = {
1353         .cursor_set = intel_crtc_cursor_set,
1354         .cursor_move = intel_crtc_cursor_move,
1355         .gamma_set = intel_crtc_gamma_set,
1356         .set_config = drm_crtc_helper_set_config,
1357         .destroy = intel_crtc_destroy,
1358 };
1359
1360
1361 void intel_crtc_init(struct drm_device *dev, int pipe)
1362 {
1363         struct intel_crtc *intel_crtc;
1364         int i;
1365
1366         intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
1367         if (intel_crtc == NULL)
1368                 return;
1369
1370         drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
1371
1372         drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
1373         intel_crtc->pipe = pipe;
1374         for (i = 0; i < 256; i++) {
1375                 intel_crtc->lut_r[i] = i;
1376                 intel_crtc->lut_g[i] = i;
1377                 intel_crtc->lut_b[i] = i;
1378         }
1379
1380         intel_crtc->cursor_addr = 0;
1381         intel_crtc->dpms_mode = DPMSModeOff;
1382         drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
1383
1384         intel_crtc->mode_set.crtc = &intel_crtc->base;
1385         intel_crtc->mode_set.connectors = (struct drm_connector **)(intel_crtc + 1);
1386         intel_crtc->mode_set.num_connectors = 0;
1387
1388         if (i915_fbpercrtc) {
1389                 
1390
1391
1392         }
1393 }
1394
1395 struct drm_crtc *intel_get_crtc_from_pipe(struct drm_device *dev, int pipe)
1396 {
1397         struct drm_crtc *crtc = NULL;
1398
1399         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1400                 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1401                 if (intel_crtc->pipe == pipe)
1402                         break;
1403         }
1404         return crtc;
1405 }
1406
1407 int intel_connector_clones(struct drm_device *dev, int type_mask)
1408 {
1409         int index_mask = 0;
1410         struct drm_connector *connector;
1411         int entry = 0;
1412
1413         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1414                 struct intel_output *intel_output = to_intel_output(connector);
1415                 if (type_mask & (1 << intel_output->type))
1416                         index_mask |= (1 << entry);
1417                 entry++;
1418         }
1419         return index_mask;
1420 }
1421
1422
1423 static void intel_setup_outputs(struct drm_device *dev)
1424 {
1425         struct drm_connector *connector;
1426
1427         intel_crt_init(dev);
1428
1429         /* Set up integrated LVDS */
1430         if (IS_MOBILE(dev) && !IS_I830(dev))
1431                 intel_lvds_init(dev);
1432
1433         if (IS_I9XX(dev)) {
1434                 intel_sdvo_init(dev, SDVOB);
1435                 intel_sdvo_init(dev, SDVOC);
1436         } else
1437                 intel_dvo_init(dev);
1438
1439         if (IS_I9XX(dev) && !IS_I915G(dev))
1440                 intel_tv_init(dev);
1441
1442         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1443                 struct intel_output *intel_output = to_intel_output(connector);
1444                 struct drm_encoder *encoder = &intel_output->enc;
1445                 int crtc_mask = 0, clone_mask = 0;
1446                 
1447                 /* valid crtcs */
1448                 switch(intel_output->type) {
1449                 case INTEL_OUTPUT_DVO:
1450                 case INTEL_OUTPUT_SDVO:
1451                         crtc_mask = ((1 << 0)|
1452                                      (1 << 1));
1453                         clone_mask = ((1 << INTEL_OUTPUT_ANALOG) |
1454                                       (1 << INTEL_OUTPUT_DVO) |
1455                                       (1 << INTEL_OUTPUT_SDVO));
1456                         break;
1457                 case INTEL_OUTPUT_ANALOG:
1458                         crtc_mask = ((1 << 0)|
1459                                      (1 << 1));
1460                         clone_mask = ((1 << INTEL_OUTPUT_ANALOG) |
1461                                       (1 << INTEL_OUTPUT_DVO) |
1462                                       (1 << INTEL_OUTPUT_SDVO));
1463                         break;
1464                 case INTEL_OUTPUT_LVDS:
1465                         crtc_mask = (1 << 1);
1466                         clone_mask = (1 << INTEL_OUTPUT_LVDS);
1467                         break;
1468                 case INTEL_OUTPUT_TVOUT:
1469                         crtc_mask = ((1 << 0) |
1470                                      (1 << 1));
1471                         clone_mask = (1 << INTEL_OUTPUT_TVOUT);
1472                         break;
1473                 }
1474                 encoder->possible_crtcs = crtc_mask;
1475                 encoder->possible_clones = intel_connector_clones(dev, clone_mask);
1476         }
1477 }
1478
1479 static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
1480 {
1481         struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
1482         struct drm_device *dev = fb->dev;
1483         if (fb->fbdev)
1484                 intelfb_remove(dev, fb);
1485
1486         drm_framebuffer_cleanup(fb);
1487
1488         kfree(intel_fb);
1489 }
1490       
1491 static const struct drm_framebuffer_funcs intel_fb_funcs = {
1492         .destroy = intel_user_framebuffer_destroy,
1493 };
1494
1495 struct drm_framebuffer *intel_user_framebuffer_create(struct drm_device *dev,
1496                                                       struct drm_file *file_priv,
1497                                                       struct drm_mode_fb_cmd *mode_cmd)
1498 {
1499         struct intel_framebuffer *intel_fb;
1500
1501         intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1502         if (!intel_fb)
1503                 return NULL;
1504
1505         drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
1506         drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
1507
1508         if (file_priv) {
1509                 mutex_lock(&dev->struct_mutex);
1510                 intel_fb->bo = drm_lookup_buffer_object(file_priv, intel_fb->base.mm_handle, 0);
1511                 mutex_unlock(&dev->struct_mutex);
1512                 if (!intel_fb->bo) {
1513                         kfree(intel_fb);
1514                         return NULL;
1515                 }
1516         }
1517         return &intel_fb->base;
1518 }
1519
1520 static int intel_insert_new_fb(struct drm_device *dev, struct drm_file *file_priv,
1521                                 struct drm_framebuffer *fb, struct drm_mode_fb_cmd *mode_cmd)
1522 {
1523         struct intel_framebuffer *intel_fb;
1524         struct drm_buffer_object *bo;
1525         struct drm_crtc *crtc;
1526
1527         intel_fb = to_intel_framebuffer(fb);
1528
1529         mutex_lock(&dev->struct_mutex);
1530         bo = drm_lookup_buffer_object(file_priv, mode_cmd->handle, 0);
1531         mutex_unlock(&dev->struct_mutex);
1532         
1533         if (!bo)
1534                 return -EINVAL;
1535         drm_helper_mode_fill_fb_struct(fb, mode_cmd);
1536        
1537         drm_bo_usage_deref_unlocked(&intel_fb->bo);
1538
1539         intel_fb->bo = bo;
1540
1541         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1542                 if (crtc->fb == fb) {
1543                         struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
1544                         crtc_funcs->mode_set_base(crtc, crtc->x, crtc->y);
1545                 }
1546         }
1547         return 0;
1548 }
1549
1550 static const struct drm_mode_config_funcs intel_mode_funcs = {
1551         .resize_fb = intel_insert_new_fb,
1552         .fb_create = intel_user_framebuffer_create,
1553         .fb_changed = intelfb_probe,
1554 };
1555
1556 void intel_modeset_init(struct drm_device *dev)
1557 {
1558         int num_pipe;
1559         int i;
1560
1561         drm_mode_config_init(dev);
1562
1563         dev->mode_config.min_width = 0;
1564         dev->mode_config.min_height = 0;
1565
1566         dev->mode_config.funcs = (void *)&intel_mode_funcs;
1567
1568         if (IS_I965G(dev)) {
1569                 dev->mode_config.max_width = 8192;
1570                 dev->mode_config.max_height = 8192;
1571         } else {
1572                 dev->mode_config.max_width = 2048;
1573                 dev->mode_config.max_height = 2048;
1574         }
1575
1576         /* set memory base */
1577         if (IS_I9XX(dev))
1578                 dev->mode_config.fb_base = pci_resource_start(dev->pdev, 2);
1579         else
1580                 dev->mode_config.fb_base = pci_resource_start(dev->pdev, 0);
1581
1582         if (IS_MOBILE(dev) || IS_I9XX(dev))
1583                 num_pipe = 2;
1584         else
1585                 num_pipe = 1;
1586         DRM_DEBUG("%d display pipe%s available.\n",
1587                   num_pipe, num_pipe > 1 ? "s" : "");
1588
1589         for (i = 0; i < num_pipe; i++) {
1590                 intel_crtc_init(dev, i);
1591         }
1592
1593         intel_setup_outputs(dev);
1594
1595         /* setup fbs */
1596         //drm_initial_config(dev, false);
1597 }
1598
1599 void intel_modeset_cleanup(struct drm_device *dev)
1600 {
1601         drm_mode_config_cleanup(dev);
1602 }
1603
1604
1605 /* current intel driver doesn't take advantage of encoders
1606    always give back the encoder for the connector
1607 */
1608 struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
1609 {
1610         struct intel_output *intel_output = to_intel_output(connector);
1611
1612         return &intel_output->enc;
1613 }