2 * Copyright © 2006-2007 Intel Corporation
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:
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
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.
24 * Eric Anholt <eric@anholt.net>
27 #include <linux/i2c.h>
29 #include "intel_drv.h"
33 #include "drm_crtc_helper.h"
35 bool intel_pipe_has_type (struct drm_crtc *crtc, int type);
58 #define INTEL_P2_NUM 2
61 intel_range_t dot, vco, n, m, m1, m2, p, p1;
65 #define I8XX_DOT_MIN 25000
66 #define I8XX_DOT_MAX 350000
67 #define I8XX_VCO_MIN 930000
68 #define I8XX_VCO_MAX 1400000
72 #define I8XX_M_MAX 140
73 #define I8XX_M1_MIN 18
74 #define I8XX_M1_MAX 26
76 #define I8XX_M2_MAX 16
78 #define I8XX_P_MAX 128
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
89 #define I9XX_DOT_MIN 20000
90 #define I9XX_DOT_MAX 400000
91 #define I9XX_VCO_MIN 1400000
92 #define I9XX_VCO_MAX 2800000
96 #define I9XX_M_MAX 120
97 #define I9XX_M1_MIN 10
98 #define I9XX_M1_MAX 20
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
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
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 },
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 },
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 },
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.
168 .p2 = { .dot_limit = I9XX_P2_LVDS_SLOW_LIMIT,
169 .p2_slow = I9XX_P2_LVDS_SLOW, .p2_fast = I9XX_P2_LVDS_FAST },
173 static const intel_limit_t *intel_limit(struct drm_crtc *crtc)
175 struct drm_device *dev = crtc->dev;
176 const intel_limit_t *limit;
179 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
180 limit = &intel_limits[INTEL_LIMIT_I9XX_LVDS];
182 limit = &intel_limits[INTEL_LIMIT_I9XX_SDVO_DAC];
184 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
185 limit = &intel_limits[INTEL_LIMIT_I8XX_LVDS];
187 limit = &intel_limits[INTEL_LIMIT_I8XX_DVO_DAC];
192 /** Derive the pixel clock for the given refclk and divisors for 8xx chips. */
194 static void i8xx_clock(int refclk, intel_clock_t *clock)
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;
202 /** Derive the pixel clock for the given refclk and divisors for 9xx chips. */
204 static void i9xx_clock(int refclk, intel_clock_t *clock)
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;
212 static void intel_clock(struct drm_device *dev, int refclk,
213 intel_clock_t *clock)
216 i9xx_clock (refclk, clock);
218 i8xx_clock (refclk, clock);
222 * Returns whether any output on the specified pipe is of the specified type
224 bool intel_pipe_has_type (struct drm_crtc *crtc, int type)
226 struct drm_device *dev = crtc->dev;
227 struct drm_mode_config *mode_config = &dev->mode_config;
228 struct drm_connector *l_entry;
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)
241 #define INTELPllInvalid(s) { /* ErrorF (s) */; return false; }
243 * Returns whether the given set of divisors are valid for a given refclk with
244 * the given connectors.
247 static bool intel_PLL_is_valid(struct drm_crtc *crtc, intel_clock_t *clock)
249 const intel_limit_t *limit = intel_limit (crtc);
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.
270 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
271 INTELPllInvalid ("dot out of range\n");
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.
281 static bool intel_find_best_PLL(struct drm_crtc *crtc, int target,
282 int refclk, intel_clock_t *best_clock)
284 struct drm_device *dev = crtc->dev;
285 struct drm_i915_private *dev_priv = dev->dev_private;
287 const intel_limit_t *limit = intel_limit(crtc);
290 if (IS_I9XX(dev) && intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
291 (I915_READ(LVDS) & LVDS_PORT_EN) != 0) {
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
298 if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
300 clock.p2 = limit->p2.p2_fast;
302 clock.p2 = limit->p2.p2_slow;
304 if (target < limit->p2.dot_limit)
305 clock.p2 = limit->p2.p2_slow;
307 clock.p2 = limit->p2.p2_fast;
310 memset (best_clock, 0, sizeof (*best_clock));
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;
317 for (clock.p1 = limit->p1.min;
318 clock.p1 <= limit->p1.max; clock.p1++) {
321 intel_clock(dev, refclk, &clock);
323 if (!intel_PLL_is_valid(crtc, &clock))
326 this_err = abs(clock.dot - target);
327 if (this_err < err) {
336 return (err != target);
340 intel_wait_for_vblank(struct drm_device *dev)
342 /* Wait for 20ms, i.e. one cycle at 50hz. */
347 intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
348 struct drm_framebuffer *old_fb)
350 struct drm_device *dev = crtc->dev;
351 struct drm_i915_private *dev_priv = dev->dev_private;
352 struct drm_i915_master_private *master_priv;
353 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
354 struct intel_framebuffer *intel_fb;
355 struct drm_i915_gem_object *obj_priv;
356 struct drm_gem_object *obj;
357 int pipe = intel_crtc->pipe;
358 unsigned long Start, Offset;
359 int dspbase = (pipe == 0 ? DSPAADDR : DSPBADDR);
360 int dspsurf = (pipe == 0 ? DSPASURF : DSPBSURF);
361 int dspstride = (pipe == 0) ? DSPASTRIDE : DSPBSTRIDE;
362 int dspcntr_reg = (pipe == 0) ? DSPACNTR : DSPBCNTR;
363 u32 dspcntr, alignment;
367 DRM_DEBUG("No FB bound\n");
371 intel_fb = to_intel_framebuffer(crtc->fb);
373 obj_priv = obj->driver_private;
375 switch (obj_priv->tiling_mode) {
376 case I915_TILING_NONE:
377 alignment = 64 * 1024;
381 alignment = 1024 * 1024;
383 alignment = 512 * 1024;
386 /* FIXME: Is this true? */
387 DRM_ERROR("Y tiled not allowed for scan out buffers\n");
393 if (i915_gem_object_pin(intel_fb->obj, alignment))
396 i915_gem_object_set_to_gtt_domain(intel_fb->obj, 1);
398 Start = obj_priv->gtt_offset;
399 Offset = y * crtc->fb->pitch + x * (crtc->fb->bits_per_pixel / 8);
401 I915_WRITE(dspstride, crtc->fb->pitch);
403 dspcntr = I915_READ(dspcntr_reg);
404 switch (crtc->fb->bits_per_pixel) {
406 dspcntr |= DISPPLANE_8BPP;
409 if (crtc->fb->depth == 15)
410 dspcntr |= DISPPLANE_15_16BPP;
412 dspcntr |= DISPPLANE_16BPP;
416 dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
419 DRM_ERROR("Unknown color depth\n");
422 I915_WRITE(dspcntr_reg, dspcntr);
424 DRM_DEBUG("Writing base %08lX %08lX %d %d\n", Start, Offset, x, y);
426 I915_WRITE(dspbase, Offset);
428 I915_WRITE(dspsurf, Start);
431 I915_WRITE(dspbase, Start + Offset);
435 intel_wait_for_vblank(dev);
438 intel_fb = to_intel_framebuffer(old_fb);
439 i915_gem_object_unpin(intel_fb->obj);
442 if (!dev->primary->master)
445 master_priv = dev->primary->master->driver_priv;
446 if (!master_priv->sarea_priv)
451 master_priv->sarea_priv->pipeA_x = x;
452 master_priv->sarea_priv->pipeA_y = y;
455 master_priv->sarea_priv->pipeB_x = x;
456 master_priv->sarea_priv->pipeB_y = y;
459 DRM_ERROR("Can't update pipe %d in SAREA\n", pipe);
467 * Sets the power management mode of the pipe and plane.
469 * This code should probably grow support for turning the cursor off and back
470 * on appropriately at the same time as we're turning the pipe off/on.
472 static void intel_crtc_dpms(struct drm_crtc *crtc, int mode)
474 struct drm_device *dev = crtc->dev;
475 struct drm_i915_master_private *master_priv;
476 struct drm_i915_private *dev_priv = dev->dev_private;
477 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
478 int pipe = intel_crtc->pipe;
479 int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
480 int dspcntr_reg = (pipe == 0) ? DSPACNTR : DSPBCNTR;
481 int dspbase_reg = (pipe == 0) ? DSPAADDR : DSPBADDR;
482 int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
486 /* XXX: When our outputs are all unaware of DPMS modes other than off
487 * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
490 case DRM_MODE_DPMS_ON:
491 case DRM_MODE_DPMS_STANDBY:
492 case DRM_MODE_DPMS_SUSPEND:
493 /* Enable the DPLL */
494 temp = I915_READ(dpll_reg);
495 if ((temp & DPLL_VCO_ENABLE) == 0) {
496 I915_WRITE(dpll_reg, temp);
498 /* Wait for the clocks to stabilize. */
500 I915_WRITE(dpll_reg, temp | DPLL_VCO_ENABLE);
502 /* Wait for the clocks to stabilize. */
504 I915_WRITE(dpll_reg, temp | DPLL_VCO_ENABLE);
506 /* Wait for the clocks to stabilize. */
510 /* Enable the pipe */
511 temp = I915_READ(pipeconf_reg);
512 if ((temp & PIPEACONF_ENABLE) == 0)
513 I915_WRITE(pipeconf_reg, temp | PIPEACONF_ENABLE);
515 /* Enable the 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));
523 intel_crtc_load_lut(crtc);
525 /* Give the overlay scaler a chance to enable if it's on this pipe */
526 //intel_crtc_dpms_video(crtc, true); TODO
528 case DRM_MODE_DPMS_OFF:
529 /* Give the overlay scaler a chance to disable if it's on this pipe */
530 //intel_crtc_dpms_video(crtc, FALSE); TODO
532 /* Disable the VGA plane that we never use */
533 I915_WRITE(VGACNTRL, VGA_DISP_DISABLE);
535 /* Disable display plane */
536 temp = I915_READ(dspcntr_reg);
537 if ((temp & DISPLAY_PLANE_ENABLE) != 0) {
538 I915_WRITE(dspcntr_reg, temp & ~DISPLAY_PLANE_ENABLE);
539 /* Flush the plane changes */
540 I915_WRITE(dspbase_reg, I915_READ(dspbase_reg));
541 I915_READ(dspbase_reg);
545 /* Wait for vblank for the disable to take effect */
546 intel_wait_for_vblank(dev);
549 /* Next, disable display pipes */
550 temp = I915_READ(pipeconf_reg);
551 if ((temp & PIPEACONF_ENABLE) != 0) {
552 I915_WRITE(pipeconf_reg, temp & ~PIPEACONF_ENABLE);
553 I915_READ(pipeconf_reg);
556 /* Wait for vblank for the disable to take effect. */
557 intel_wait_for_vblank(dev);
559 temp = I915_READ(dpll_reg);
560 if ((temp & DPLL_VCO_ENABLE) != 0) {
561 I915_WRITE(dpll_reg, temp & ~DPLL_VCO_ENABLE);
565 /* Wait for the clocks to turn off. */
570 if (!dev->primary->master)
573 master_priv = dev->primary->master->driver_priv;
574 if (!master_priv->sarea_priv)
577 enabled = crtc->enabled && mode != DRM_MODE_DPMS_OFF;
581 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
582 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
585 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
586 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
589 DRM_ERROR("Can't update pipe %d in SAREA\n", pipe);
593 intel_crtc->dpms_mode = mode;
596 static void intel_crtc_prepare (struct drm_crtc *crtc)
598 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
599 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
602 static void intel_crtc_commit (struct drm_crtc *crtc)
604 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
605 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
608 void intel_encoder_prepare (struct drm_encoder *encoder)
610 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
611 /* lvds has its own version of prepare see intel_lvds_prepare */
612 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
615 void intel_encoder_commit (struct drm_encoder *encoder)
617 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
618 /* lvds has its own version of commit see intel_lvds_commit */
619 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
622 static bool intel_crtc_mode_fixup(struct drm_crtc *crtc,
623 struct drm_display_mode *mode,
624 struct drm_display_mode *adjusted_mode)
630 /** Returns the core display clock speed for i830 - i945 */
631 static int intel_get_core_clock_speed(struct drm_device *dev)
634 /* Core clock values taken from the published datasheets.
635 * The 830 may go up to 166 Mhz, which we should check.
639 else if (IS_I915G(dev))
641 else if (IS_I945GM(dev) || IS_845G(dev))
643 else if (IS_I915GM(dev)) {
646 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
648 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
651 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
652 case GC_DISPLAY_CLOCK_333_MHZ:
655 case GC_DISPLAY_CLOCK_190_200_MHZ:
659 } else if (IS_I865G(dev))
661 else if (IS_I855(dev)) {
663 /* Assume that the hardware is in the high speed state. This
664 * should be the default.
666 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
667 case GC_CLOCK_133_200:
668 case GC_CLOCK_100_200:
670 case GC_CLOCK_166_250:
672 case GC_CLOCK_100_133:
675 } else /* 852, 830 */
678 return 0; /* Silence gcc warning */
683 * Return the pipe currently connected to the panel fitter,
684 * or -1 if the panel fitter is not present or not in use
686 static int intel_panel_fitter_pipe (struct drm_device *dev)
688 struct drm_i915_private *dev_priv = dev->dev_private;
691 /* i830 doesn't have a panel fitter */
695 pfit_control = I915_READ(PFIT_CONTROL);
697 /* See if the panel fitter is in use */
698 if ((pfit_control & PFIT_ENABLE) == 0)
701 /* 965 can place panel fitter on either pipe */
703 return (pfit_control >> 29) & 0x3;
705 /* older chips can only use pipe 1 */
709 static void intel_crtc_mode_set(struct drm_crtc *crtc,
710 struct drm_display_mode *mode,
711 struct drm_display_mode *adjusted_mode,
713 struct drm_framebuffer *old_fb)
715 struct drm_device *dev = crtc->dev;
716 struct drm_i915_private *dev_priv = dev->dev_private;
717 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
718 int pipe = intel_crtc->pipe;
719 int fp_reg = (pipe == 0) ? FPA0 : FPB0;
720 int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
721 int dpll_md_reg = (intel_crtc->pipe == 0) ? DPLL_A_MD : DPLL_B_MD;
722 int dspcntr_reg = (pipe == 0) ? DSPACNTR : DSPBCNTR;
723 int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
724 int htot_reg = (pipe == 0) ? HTOTAL_A : HTOTAL_B;
725 int hblank_reg = (pipe == 0) ? HBLANK_A : HBLANK_B;
726 int hsync_reg = (pipe == 0) ? HSYNC_A : HSYNC_B;
727 int vtot_reg = (pipe == 0) ? VTOTAL_A : VTOTAL_B;
728 int vblank_reg = (pipe == 0) ? VBLANK_A : VBLANK_B;
729 int vsync_reg = (pipe == 0) ? VSYNC_A : VSYNC_B;
730 int dspsize_reg = (pipe == 0) ? DSPASIZE : DSPBSIZE;
731 int dsppos_reg = (pipe == 0) ? DSPAPOS : DSPBPOS;
732 int pipesrc_reg = (pipe == 0) ? PIPEASRC : PIPEBSRC;
735 u32 dpll = 0, fp = 0, dspcntr, pipeconf;
736 bool ok, is_sdvo = false, is_dvo = false;
737 bool is_crt = false, is_lvds = false, is_tv = false;
738 struct drm_mode_config *mode_config = &dev->mode_config;
739 struct drm_connector *connector;
741 drm_vblank_pre_modeset(dev, pipe);
743 list_for_each_entry(connector, &mode_config->connector_list, head) {
744 struct intel_output *intel_output = to_intel_output(connector);
746 if (!connector->encoder || connector->encoder->crtc != crtc)
749 switch (intel_output->type) {
750 case INTEL_OUTPUT_LVDS:
753 case INTEL_OUTPUT_SDVO:
754 case INTEL_OUTPUT_HDMI:
757 case INTEL_OUTPUT_DVO:
760 case INTEL_OUTPUT_TVOUT:
763 case INTEL_OUTPUT_ANALOG:
775 ok = intel_find_best_PLL(crtc, adjusted_mode->clock, refclk, &clock);
777 DRM_ERROR("Couldn't find PLL settings for mode!\n");
781 fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
783 dpll = DPLL_VGA_MODE_DIS;
786 dpll |= DPLLB_MODE_LVDS;
788 dpll |= DPLLB_MODE_DAC_SERIAL;
790 dpll |= DPLL_DVO_HIGH_SPEED;
791 if (IS_I945G(dev) || IS_I945GM(dev)) {
792 int sdvo_pixel_multiply = adjusted_mode->clock / mode->clock;
793 dpll |= (sdvo_pixel_multiply - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
797 /* compute bitmask from p1 value */
798 dpll |= (1 << (clock.p1 - 1)) << 16;
801 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
804 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
807 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
810 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
814 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
817 dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
820 dpll |= PLL_P1_DIVIDE_BY_TWO;
822 dpll |= (clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
824 dpll |= PLL_P2_DIVIDE_BY_4;
829 /* XXX: just matching BIOS for now */
830 /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
834 dpll |= PLL_REF_INPUT_DREFCLK;
837 pipeconf = I915_READ(pipeconf_reg);
839 /* Set up the display plane register */
840 dspcntr = DISPPLANE_GAMMA_ENABLE;
843 dspcntr |= DISPPLANE_SEL_PIPE_A;
845 dspcntr |= DISPPLANE_SEL_PIPE_B;
847 if (pipe == 0 && !IS_I965G(dev)) {
848 /* Enable pixel doubling when the dot clock is > 90% of the (display)
851 * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
854 if (mode->clock > intel_get_core_clock_speed(dev) * 9 / 10)
855 pipeconf |= PIPEACONF_DOUBLE_WIDE;
857 pipeconf &= ~PIPEACONF_DOUBLE_WIDE;
860 dspcntr |= DISPLAY_PLANE_ENABLE;
861 pipeconf |= PIPEACONF_ENABLE;
862 dpll |= DPLL_VCO_ENABLE;
865 /* Disable the panel fitter if it was on our pipe */
866 if (intel_panel_fitter_pipe(dev) == pipe)
867 I915_WRITE(PFIT_CONTROL, 0);
869 DRM_DEBUG("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
870 drm_mode_debug_printmodeline(mode);
873 if (dpll & DPLL_VCO_ENABLE) {
874 I915_WRITE(fp_reg, fp);
875 I915_WRITE(dpll_reg, dpll & ~DPLL_VCO_ENABLE);
880 /* The LVDS pin pair needs to be on before the DPLLs are enabled.
881 * This is an exception to the general rule that mode_set doesn't turn
885 u32 lvds = I915_READ(LVDS);
887 lvds |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP | LVDS_PIPEB_SELECT;
888 /* Set the B0-B3 data pairs corresponding to whether we're going to
889 * set the DPLLs for dual-channel mode or not.
892 lvds |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
894 lvds &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
896 /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
897 * appropriately here, but we need to look more thoroughly into how
898 * panels behave in the two modes.
901 I915_WRITE(LVDS, lvds);
905 I915_WRITE(fp_reg, fp);
906 I915_WRITE(dpll_reg, dpll);
908 /* Wait for the clocks to stabilize. */
912 int sdvo_pixel_multiply = adjusted_mode->clock / mode->clock;
913 I915_WRITE(dpll_md_reg, (0 << DPLL_MD_UDI_DIVIDER_SHIFT) |
914 ((sdvo_pixel_multiply - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT));
916 /* write it again -- the BIOS does, after all */
917 I915_WRITE(dpll_reg, dpll);
920 /* Wait for the clocks to stabilize. */
923 I915_WRITE(htot_reg, (adjusted_mode->crtc_hdisplay - 1) |
924 ((adjusted_mode->crtc_htotal - 1) << 16));
925 I915_WRITE(hblank_reg, (adjusted_mode->crtc_hblank_start - 1) |
926 ((adjusted_mode->crtc_hblank_end - 1) << 16));
927 I915_WRITE(hsync_reg, (adjusted_mode->crtc_hsync_start - 1) |
928 ((adjusted_mode->crtc_hsync_end - 1) << 16));
929 I915_WRITE(vtot_reg, (adjusted_mode->crtc_vdisplay - 1) |
930 ((adjusted_mode->crtc_vtotal - 1) << 16));
931 I915_WRITE(vblank_reg, (adjusted_mode->crtc_vblank_start - 1) |
932 ((adjusted_mode->crtc_vblank_end - 1) << 16));
933 I915_WRITE(vsync_reg, (adjusted_mode->crtc_vsync_start - 1) |
934 ((adjusted_mode->crtc_vsync_end - 1) << 16));
935 /* pipesrc and dspsize control the size that is scaled from, which should
936 * always be the user's requested size.
938 I915_WRITE(dspsize_reg, ((mode->vdisplay - 1) << 16) | (mode->hdisplay - 1));
939 I915_WRITE(dsppos_reg, 0);
940 I915_WRITE(pipesrc_reg, ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
941 I915_WRITE(pipeconf_reg, pipeconf);
942 I915_READ(pipeconf_reg);
944 intel_wait_for_vblank(dev);
946 I915_WRITE(dspcntr_reg, dspcntr);
948 /* Flush the plane changes */
949 intel_pipe_set_base(crtc, x, y, old_fb);
951 drm_vblank_post_modeset(dev, pipe);
954 /** Loads the palette/gamma unit for the CRTC with the prepared values */
955 void intel_crtc_load_lut(struct drm_crtc *crtc)
957 struct drm_device *dev = crtc->dev;
958 struct drm_i915_private *dev_priv = dev->dev_private;
959 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
960 int palreg = (intel_crtc->pipe == 0) ? PALETTE_A : PALETTE_B;
963 /* The clocks have to be on to load the palette. */
967 for (i = 0; i < 256; i++) {
968 I915_WRITE(palreg + 4 * i,
969 (intel_crtc->lut_r[i] << 16) |
970 (intel_crtc->lut_g[i] << 8) |
971 intel_crtc->lut_b[i]);
975 static int intel_crtc_cursor_set(struct drm_crtc *crtc,
976 struct drm_file *file_priv,
978 uint32_t width, uint32_t height)
980 struct drm_device *dev = crtc->dev;
981 struct drm_i915_private *dev_priv = dev->dev_private;
982 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
983 struct drm_gem_object *bo;
984 struct drm_i915_gem_object *obj_priv;
985 int pipe = intel_crtc->pipe;
986 uint32_t control = (pipe == 0) ? CURACNTR : CURBCNTR;
987 uint32_t base = (pipe == 0) ? CURABASE : CURBBASE;
994 /* if we want to turn off the cursor ignore width and height */
996 DRM_DEBUG("cursor off\n");
997 temp = CURSOR_MODE_DISABLE;
1003 /* Currently we only support 64x64 cursors */
1004 if (width != 64 || height != 64) {
1005 DRM_ERROR("we currently only support 64x64 cursors\n");
1009 bo = drm_gem_object_lookup(dev, file_priv, handle);
1013 obj_priv = bo->driver_private;
1015 if (bo->size < width * height * 4) {
1016 DRM_ERROR("buffer is to small\n");
1017 drm_gem_object_unreference(bo);
1021 if (dev_priv->cursor_needs_physical) {
1022 addr = dev->agp->base + obj_priv->gtt_offset;
1024 addr = obj_priv->gtt_offset;
1027 ret = i915_gem_object_pin(bo, PAGE_SIZE);
1029 DRM_ERROR("failed to pin cursor bo\n");
1030 drm_gem_object_unreference(bo);
1035 /* set the pipe for the cursor */
1036 temp |= (pipe << 28);
1037 temp |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
1040 I915_WRITE(control, temp);
1041 I915_WRITE(base, addr);
1043 if (intel_crtc->cursor_bo) {
1044 i915_gem_object_unpin(intel_crtc->cursor_bo);
1045 drm_gem_object_unreference(intel_crtc->cursor_bo);
1048 intel_crtc->cursor_addr = addr;
1049 intel_crtc->cursor_bo = bo;
1054 static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
1056 struct drm_device *dev = crtc->dev;
1057 struct drm_i915_private *dev_priv = dev->dev_private;
1058 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1059 int pipe = intel_crtc->pipe;
1064 temp |= (CURSOR_POS_SIGN << CURSOR_X_SHIFT);
1068 temp |= (CURSOR_POS_SIGN << CURSOR_Y_SHIFT);
1072 temp |= ((x & CURSOR_POS_MASK) << CURSOR_X_SHIFT);
1073 temp |= ((y & CURSOR_POS_MASK) << CURSOR_Y_SHIFT);
1075 adder = intel_crtc->cursor_addr;
1076 I915_WRITE((pipe == 0) ? CURAPOS : CURBPOS, temp);
1077 I915_WRITE((pipe == 0) ? CURABASE : CURBBASE, adder);
1082 /** Sets the color ramps on behalf of RandR */
1083 void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
1084 u16 blue, int regno)
1086 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1088 intel_crtc->lut_r[regno] = red >> 8;
1089 intel_crtc->lut_g[regno] = green >> 8;
1090 intel_crtc->lut_b[regno] = blue >> 8;
1093 static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
1094 u16 *blue, uint32_t size)
1096 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1102 for (i = 0; i < 256; i++) {
1103 intel_crtc->lut_r[i] = red[i] >> 8;
1104 intel_crtc->lut_g[i] = green[i] >> 8;
1105 intel_crtc->lut_b[i] = blue[i] >> 8;
1108 intel_crtc_load_lut(crtc);
1112 * Get a pipe with a simple mode set on it for doing load-based monitor
1115 * It will be up to the load-detect code to adjust the pipe as appropriate for
1116 * its requirements. The pipe will be connected to no other outputs.
1118 * Currently this code will only succeed if there is a pipe with no outputs
1119 * configured for it. In the future, it could choose to temporarily disable
1120 * some outputs to free up a pipe for its use.
1122 * \return crtc, or NULL if no pipes are available.
1125 /* VESA 640x480x72Hz mode to set on the pipe */
1126 static struct drm_display_mode load_detect_mode = {
1127 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
1128 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
1131 struct drm_crtc *intel_get_load_detect_pipe(struct intel_output *intel_output,
1132 struct drm_display_mode *mode,
1135 struct intel_crtc *intel_crtc;
1136 struct drm_crtc *possible_crtc;
1137 struct drm_crtc *supported_crtc =NULL;
1138 struct drm_encoder *encoder = &intel_output->enc;
1139 struct drm_crtc *crtc = NULL;
1140 struct drm_device *dev = encoder->dev;
1141 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
1142 struct drm_crtc_helper_funcs *crtc_funcs;
1146 * Algorithm gets a little messy:
1147 * - if the connector already has an assigned crtc, use it (but make
1148 * sure it's on first)
1149 * - try to find the first unused crtc that can drive this connector,
1150 * and use that if we find one
1151 * - if there are no unused crtcs available, try to use the first
1152 * one we found that supports the connector
1155 /* See if we already have a CRTC for this connector */
1156 if (encoder->crtc) {
1157 crtc = encoder->crtc;
1158 /* Make sure the crtc and connector are running */
1159 intel_crtc = to_intel_crtc(crtc);
1160 *dpms_mode = intel_crtc->dpms_mode;
1161 if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
1162 crtc_funcs = crtc->helper_private;
1163 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
1164 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
1169 /* Find an unused one (if possible) */
1170 list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
1172 if (!(encoder->possible_crtcs & (1 << i)))
1174 if (!possible_crtc->enabled) {
1175 crtc = possible_crtc;
1178 if (!supported_crtc)
1179 supported_crtc = possible_crtc;
1183 * If we didn't find an unused CRTC, don't use any.
1189 encoder->crtc = crtc;
1190 intel_output->load_detect_temp = true;
1192 intel_crtc = to_intel_crtc(crtc);
1193 *dpms_mode = intel_crtc->dpms_mode;
1195 if (!crtc->enabled) {
1197 mode = &load_detect_mode;
1198 drm_crtc_helper_set_mode(crtc, mode, 0, 0, crtc->fb);
1200 if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
1201 crtc_funcs = crtc->helper_private;
1202 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
1205 /* Add this connector to the crtc */
1206 encoder_funcs->mode_set(encoder, &crtc->mode, &crtc->mode);
1207 encoder_funcs->commit(encoder);
1209 /* let the connector get through one full cycle before testing */
1210 intel_wait_for_vblank(dev);
1215 void intel_release_load_detect_pipe(struct intel_output *intel_output, int dpms_mode)
1217 struct drm_encoder *encoder = &intel_output->enc;
1218 struct drm_device *dev = encoder->dev;
1219 struct drm_crtc *crtc = encoder->crtc;
1220 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
1221 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
1223 if (intel_output->load_detect_temp) {
1224 encoder->crtc = NULL;
1225 intel_output->load_detect_temp = false;
1226 crtc->enabled = drm_helper_crtc_in_use(crtc);
1227 drm_helper_disable_unused_functions(dev);
1230 /* Switch crtc and output back off if necessary */
1231 if (crtc->enabled && dpms_mode != DRM_MODE_DPMS_ON) {
1232 if (encoder->crtc == crtc)
1233 encoder_funcs->dpms(encoder, dpms_mode);
1234 crtc_funcs->dpms(crtc, dpms_mode);
1238 /* Returns the clock of the currently programmed mode of the given pipe. */
1239 static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
1241 struct drm_i915_private *dev_priv = dev->dev_private;
1242 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1243 int pipe = intel_crtc->pipe;
1244 u32 dpll = I915_READ((pipe == 0) ? DPLL_A : DPLL_B);
1246 intel_clock_t clock;
1248 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
1249 fp = I915_READ((pipe == 0) ? FPA0 : FPB0);
1251 fp = I915_READ((pipe == 0) ? FPA1 : FPB1);
1253 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
1254 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
1255 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
1257 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
1258 DPLL_FPA01_P1_POST_DIV_SHIFT);
1260 switch (dpll & DPLL_MODE_MASK) {
1261 case DPLLB_MODE_DAC_SERIAL:
1262 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
1265 case DPLLB_MODE_LVDS:
1266 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
1270 DRM_DEBUG("Unknown DPLL mode %08x in programmed "
1271 "mode\n", (int)(dpll & DPLL_MODE_MASK));
1275 /* XXX: Handle the 100Mhz refclk */
1276 i9xx_clock(96000, &clock);
1278 bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
1281 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
1282 DPLL_FPA01_P1_POST_DIV_SHIFT);
1285 if ((dpll & PLL_REF_INPUT_MASK) ==
1286 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
1287 /* XXX: might not be 66MHz */
1288 i8xx_clock(66000, &clock);
1290 i8xx_clock(48000, &clock);
1292 if (dpll & PLL_P1_DIVIDE_BY_TWO)
1295 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
1296 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
1298 if (dpll & PLL_P2_DIVIDE_BY_4)
1303 i8xx_clock(48000, &clock);
1307 /* XXX: It would be nice to validate the clocks, but we can't reuse
1308 * i830PllIsValid() because it relies on the xf86_config connector
1309 * configuration being accurate, which it isn't necessarily.
1315 /** Returns the currently programmed mode of the given pipe. */
1316 struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
1317 struct drm_crtc *crtc)
1319 struct drm_i915_private *dev_priv = dev->dev_private;
1320 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1321 int pipe = intel_crtc->pipe;
1322 struct drm_display_mode *mode;
1323 int htot = I915_READ((pipe == 0) ? HTOTAL_A : HTOTAL_B);
1324 int hsync = I915_READ((pipe == 0) ? HSYNC_A : HSYNC_B);
1325 int vtot = I915_READ((pipe == 0) ? VTOTAL_A : VTOTAL_B);
1326 int vsync = I915_READ((pipe == 0) ? VSYNC_A : VSYNC_B);
1328 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
1332 mode->clock = intel_crtc_clock_get(dev, crtc);
1333 mode->hdisplay = (htot & 0xffff) + 1;
1334 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
1335 mode->hsync_start = (hsync & 0xffff) + 1;
1336 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
1337 mode->vdisplay = (vtot & 0xffff) + 1;
1338 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
1339 mode->vsync_start = (vsync & 0xffff) + 1;
1340 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
1342 drm_mode_set_name(mode);
1343 drm_mode_set_crtcinfo(mode, 0);
1348 static void intel_crtc_destroy(struct drm_crtc *crtc)
1350 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1352 drm_crtc_cleanup(crtc);
1356 static const struct drm_crtc_helper_funcs intel_helper_funcs = {
1357 .dpms = intel_crtc_dpms,
1358 .mode_fixup = intel_crtc_mode_fixup,
1359 .mode_set = intel_crtc_mode_set,
1360 .mode_set_base = intel_pipe_set_base,
1361 .prepare = intel_crtc_prepare,
1362 .commit = intel_crtc_commit,
1365 static const struct drm_crtc_funcs intel_crtc_funcs = {
1366 .cursor_set = intel_crtc_cursor_set,
1367 .cursor_move = intel_crtc_cursor_move,
1368 .gamma_set = intel_crtc_gamma_set,
1369 .set_config = drm_crtc_helper_set_config,
1370 .destroy = intel_crtc_destroy,
1374 static void intel_crtc_init(struct drm_device *dev, int pipe)
1376 struct intel_crtc *intel_crtc;
1379 intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
1380 if (intel_crtc == NULL)
1383 drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
1385 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
1386 intel_crtc->pipe = pipe;
1387 for (i = 0; i < 256; i++) {
1388 intel_crtc->lut_r[i] = i;
1389 intel_crtc->lut_g[i] = i;
1390 intel_crtc->lut_b[i] = i;
1393 intel_crtc->cursor_addr = 0;
1394 intel_crtc->dpms_mode = DRM_MODE_DPMS_OFF;
1395 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
1397 intel_crtc->mode_set.crtc = &intel_crtc->base;
1398 intel_crtc->mode_set.connectors = (struct drm_connector **)(intel_crtc + 1);
1399 intel_crtc->mode_set.num_connectors = 0;
1401 if (i915_fbpercrtc) {
1408 struct drm_crtc *intel_get_crtc_from_pipe(struct drm_device *dev, int pipe)
1410 struct drm_crtc *crtc = NULL;
1412 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1413 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1414 if (intel_crtc->pipe == pipe)
1420 static int intel_connector_clones(struct drm_device *dev, int type_mask)
1423 struct drm_connector *connector;
1426 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1427 struct intel_output *intel_output = to_intel_output(connector);
1428 if (type_mask & (1 << intel_output->type))
1429 index_mask |= (1 << entry);
1436 static void intel_setup_outputs(struct drm_device *dev)
1438 struct drm_connector *connector;
1440 intel_crt_init(dev);
1442 /* Set up integrated LVDS */
1443 if (IS_MOBILE(dev) && !IS_I830(dev))
1444 intel_lvds_init(dev);
1449 found = intel_sdvo_init(dev, SDVOB);
1450 if (!found && SUPPORTS_INTEGRATED_HDMI(dev))
1451 intel_hdmi_init(dev, SDVOB);
1453 found = intel_sdvo_init(dev, SDVOC);
1454 if (!found && SUPPORTS_INTEGRATED_HDMI(dev))
1455 intel_hdmi_init(dev, SDVOC);
1457 intel_dvo_init(dev);
1459 if (IS_I9XX(dev) && IS_MOBILE(dev))
1462 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1463 struct intel_output *intel_output = to_intel_output(connector);
1464 struct drm_encoder *encoder = &intel_output->enc;
1465 int crtc_mask = 0, clone_mask = 0;
1468 switch(intel_output->type) {
1469 case INTEL_OUTPUT_HDMI:
1470 crtc_mask = ((1 << 0)|
1472 clone_mask = ((1 << INTEL_OUTPUT_HDMI));
1474 case INTEL_OUTPUT_DVO:
1475 case INTEL_OUTPUT_SDVO:
1476 crtc_mask = ((1 << 0)|
1478 clone_mask = ((1 << INTEL_OUTPUT_ANALOG) |
1479 (1 << INTEL_OUTPUT_DVO) |
1480 (1 << INTEL_OUTPUT_SDVO));
1482 case INTEL_OUTPUT_ANALOG:
1483 crtc_mask = ((1 << 0)|
1485 clone_mask = ((1 << INTEL_OUTPUT_ANALOG) |
1486 (1 << INTEL_OUTPUT_DVO) |
1487 (1 << INTEL_OUTPUT_SDVO));
1489 case INTEL_OUTPUT_LVDS:
1490 crtc_mask = (1 << 1);
1491 clone_mask = (1 << INTEL_OUTPUT_LVDS);
1493 case INTEL_OUTPUT_TVOUT:
1494 crtc_mask = ((1 << 0) |
1496 clone_mask = (1 << INTEL_OUTPUT_TVOUT);
1499 encoder->possible_crtcs = crtc_mask;
1500 encoder->possible_clones = intel_connector_clones(dev, clone_mask);
1504 static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
1506 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
1507 struct drm_device *dev = fb->dev;
1510 intelfb_remove(dev, fb);
1512 drm_framebuffer_cleanup(fb);
1513 mutex_lock(&dev->struct_mutex);
1514 drm_gem_object_unreference(intel_fb->obj);
1515 mutex_unlock(&dev->struct_mutex);
1520 static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
1521 struct drm_file *file_priv,
1522 unsigned int *handle)
1524 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
1525 struct drm_gem_object *object = intel_fb->obj;
1527 return drm_gem_handle_create(file_priv, object, handle);
1530 static const struct drm_framebuffer_funcs intel_fb_funcs = {
1531 .destroy = intel_user_framebuffer_destroy,
1532 .create_handle = intel_user_framebuffer_create_handle,
1535 int intel_framebuffer_create(struct drm_device *dev,
1536 struct drm_mode_fb_cmd *mode_cmd,
1537 struct drm_framebuffer **fb,
1538 struct drm_gem_object *obj)
1540 struct intel_framebuffer *intel_fb;
1543 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1547 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
1549 DRM_ERROR("framebuffer init failed %d\n", ret);
1553 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
1555 intel_fb->obj = obj;
1557 *fb = &intel_fb->base;
1563 static struct drm_framebuffer *
1564 intel_user_framebuffer_create(struct drm_device *dev,
1565 struct drm_file *filp,
1566 struct drm_mode_fb_cmd *mode_cmd)
1568 struct drm_gem_object *obj;
1569 struct drm_framebuffer *fb;
1572 obj = drm_gem_object_lookup(dev, filp, mode_cmd->handle);
1576 ret = intel_framebuffer_create(dev, mode_cmd, &fb, obj);
1578 drm_gem_object_unreference(obj);
1585 static const struct drm_mode_config_funcs intel_mode_funcs = {
1586 .fb_create = intel_user_framebuffer_create,
1587 .fb_changed = intelfb_probe,
1590 void intel_modeset_init(struct drm_device *dev)
1595 drm_mode_config_init(dev);
1597 dev->mode_config.min_width = 0;
1598 dev->mode_config.min_height = 0;
1600 dev->mode_config.funcs = (void *)&intel_mode_funcs;
1602 if (IS_I965G(dev)) {
1603 dev->mode_config.max_width = 8192;
1604 dev->mode_config.max_height = 8192;
1606 dev->mode_config.max_width = 2048;
1607 dev->mode_config.max_height = 2048;
1610 /* set memory base */
1612 dev->mode_config.fb_base = pci_resource_start(dev->pdev, 2);
1614 dev->mode_config.fb_base = pci_resource_start(dev->pdev, 0);
1616 if (IS_MOBILE(dev) || IS_I9XX(dev))
1620 DRM_DEBUG("%d display pipe%s available.\n",
1621 num_pipe, num_pipe > 1 ? "s" : "");
1623 for (i = 0; i < num_pipe; i++) {
1624 intel_crtc_init(dev, i);
1627 intel_setup_outputs(dev);
1630 void intel_modeset_cleanup(struct drm_device *dev)
1632 drm_mode_config_cleanup(dev);
1636 /* current intel driver doesn't take advantage of encoders
1637 always give back the encoder for the connector
1639 struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
1641 struct intel_output *intel_output = to_intel_output(connector);
1643 return &intel_output->enc;