2f28ca1b0d441d1883460414fc8e92f23203d0f8
[profile/ivi/libdrm.git] / linux-core / intel_fb.c
1 /*
2  * Copyright © 2007 David Airlie
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  *     David Airlie
25  */
26     /*
27      *  Modularization
28      */
29
30 #include <linux/module.h>
31 #include <linux/kernel.h>
32 #include <linux/errno.h>
33 #include <linux/string.h>
34 #include <linux/mm.h>
35 #include <linux/tty.h>
36 #include <linux/slab.h>
37 #include <linux/delay.h>
38 #include <linux/fb.h>
39 #include <linux/init.h>
40
41 #include "drmP.h"
42 #include "drm.h"
43 #include "drm_crtc.h"
44 #include "intel_drv.h"
45 #include "i915_drm.h"
46 #include "i915_drv.h"
47
48 struct intelfb_par {
49         struct drm_device *dev;
50         struct drm_display_mode *our_mode;
51         struct intel_framebuffer *intel_fb;
52         int crtc_count;
53         struct list_head mode_set_list;
54 };
55 /*
56 static int
57 var_to_refresh(const struct fb_var_screeninfo *var)
58 {
59         int xtot = var->xres + var->left_margin + var->right_margin +
60                 var->hsync_len;
61         int ytot = var->yres + var->upper_margin + var->lower_margin +
62                 var->vsync_len;
63
64         return (1000000000 / var->pixclock * 1000 + 500) / xtot / ytot;
65 }*/
66
67 static int intelfb_setcolreg(unsigned regno, unsigned red, unsigned green,
68                         unsigned blue, unsigned transp,
69                         struct fb_info *info)
70 {
71         struct intelfb_par *par = info->par;
72         struct drm_mode_set *modeset;
73
74         list_for_each_entry(modeset, &par->mode_set_list, head) {
75                 struct drm_crtc *crtc = modeset->crtc;
76                 struct drm_framebuffer *fb = modeset->fb;
77
78                 if (regno > 255)
79                         return 1;
80
81                 if (fb->depth == 8) {
82                         intel_crtc_fb_gamma_set(crtc, red, green, blue, regno);
83                         return 0;
84                 }
85
86                 if (regno < 16) {
87                         switch (fb->depth) {
88                         case 15:
89                                 fb->pseudo_palette[regno] = ((red & 0xf800) >> 1) |
90                                         ((green & 0xf800) >>  6) |
91                                         ((blue & 0xf800) >> 11);
92                                 break;
93                         case 16:
94                                 fb->pseudo_palette[regno] = (red & 0xf800) |
95                                         ((green & 0xfc00) >>  5) |
96                                         ((blue  & 0xf800) >> 11);
97                                 break;
98                         case 24:
99                         case 32:
100                                 fb->pseudo_palette[regno] = ((red & 0xff00) << 8) |
101                                         (green & 0xff00) |
102                                         ((blue  & 0xff00) >> 8);
103                                 break;
104                         }
105                 }
106         }
107         return 0;
108 }
109
110 static int intelfb_check_var(struct fb_var_screeninfo *var,
111                         struct fb_info *info)
112 {
113         struct intelfb_par *par = info->par;
114         struct intel_framebuffer *intel_fb = par->intel_fb;
115         struct drm_framebuffer *fb = &intel_fb->base;
116         int depth;
117
118         if (var->pixclock == -1 || !var->pixclock)
119                 return -EINVAL;
120
121         /* Need to resize the fb object !!! */
122         if (var->xres > fb->width || var->yres > fb->height) {
123                 DRM_ERROR("Requested width/height is greater than current fb object %dx%d > %dx%d\n",var->xres,var->yres,fb->width,fb->height);
124                 DRM_ERROR("Need resizing code.\n");
125                 return -EINVAL;
126         }
127
128         switch (var->bits_per_pixel) {
129         case 16:
130                 depth = (var->green.length == 6) ? 16 : 15;
131                 break;
132         case 32:
133                 depth = (var->transp.length > 0) ? 32 : 24;
134                 break;
135         default:
136                 depth = var->bits_per_pixel;
137                 break;
138         }
139                 
140         switch (depth) {
141         case 8:
142                 var->red.offset = 0;
143                 var->green.offset = 0;
144                 var->blue.offset = 0;
145                 var->red.length = 8;
146                 var->green.length = 8;
147                 var->blue.length = 8;
148                 var->transp.length = 0;
149                 var->transp.offset = 0;
150                 break;
151         case 15:
152                 var->red.offset = 10;
153                 var->green.offset = 5;
154                 var->blue.offset = 0;
155                 var->red.length = 5;
156                 var->green.length = 5;
157                 var->blue.length = 5;
158                 var->transp.length = 1;
159                 var->transp.offset = 15;
160                 break;
161         case 16:
162                 var->red.offset = 11;
163                 var->green.offset = 5;
164                 var->blue.offset = 0;
165                 var->red.length = 5;
166                 var->green.length = 6;
167                 var->blue.length = 5;
168                 var->transp.length = 0;
169                 var->transp.offset = 0;
170                 break;
171         case 24:
172                 var->red.offset = 16;
173                 var->green.offset = 8;
174                 var->blue.offset = 0;
175                 var->red.length = 8;
176                 var->green.length = 8;
177                 var->blue.length = 8;
178                 var->transp.length = 0;
179                 var->transp.offset = 0;
180                 break;
181         case 32:
182                 var->red.offset = 16;
183                 var->green.offset = 8;
184                 var->blue.offset = 0;
185                 var->red.length = 8;
186                 var->green.length = 8;
187                 var->blue.length = 8;
188                 var->transp.length = 8;
189                 var->transp.offset = 24;
190                 break;
191         default:
192                 return -EINVAL; 
193         }
194
195         return 0;
196 }
197
198 /* this will let fbcon do the mode init */
199 /* FIXME: take mode config lock? */
200 static int intelfb_set_par(struct fb_info *info)
201 {
202         struct intelfb_par *par = info->par;
203         struct drm_device *dev = par->dev;
204         struct fb_var_screeninfo *var = &info->var;
205         int found = 0;
206
207         DRM_DEBUG("%d %d\n", var->xres, var->pixclock);
208
209
210         if (var->pixclock != -1) {
211
212                 DRM_ERROR("PIXEL CLCOK SET\n");
213 #if 0
214                 struct intel_framebuffer *intel_fb = par->intel_fb;
215                 struct drm_framebuffer *fb = &intel_fb->base;
216                 struct drm_display_mode *drm_mode, *search_mode;
217                 struct drm_connector *connector = NULL;
218
219                 switch (var->bits_per_pixel) {
220                 case 16:
221                         fb->depth = (var->green.length == 6) ? 16 : 15;
222                         break;
223                 case 32:
224                         fb->depth = (var->transp.length > 0) ? 32 : 24;
225                         break;
226                 default:
227                         fb->depth = var->bits_per_pixel;
228                         break;
229                 }
230                 
231                 fb->bits_per_pixel = var->bits_per_pixel;
232                 
233                 info->fix.line_length = fb->pitch;
234                 info->fix.smem_len = info->fix.line_length * fb->height;
235                 info->fix.visual = (fb->depth == 8) ? FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_TRUECOLOR;
236                 
237                 info->screen_size = info->fix.smem_len; /* ??? */
238                 /* reuse desired mode if possible */
239                 /* create a drm mode */
240                 drm_mode = drm_mode_create(dev);
241                 drm_mode->hdisplay = var->xres;
242                 drm_mode->hsync_start = drm_mode->hdisplay + var->right_margin;
243                 drm_mode->hsync_end = drm_mode->hsync_start + var->hsync_len;
244                 drm_mode->htotal = drm_mode->hsync_end + var->left_margin;
245                 drm_mode->vdisplay = var->yres;
246                 drm_mode->vsync_start = drm_mode->vdisplay + var->lower_margin;
247                 drm_mode->vsync_end = drm_mode->vsync_start + var->vsync_len;
248                 drm_mode->vtotal = drm_mode->vsync_end + var->upper_margin;
249                 drm_mode->clock = PICOS2KHZ(var->pixclock);
250                 drm_mode->vrefresh = drm_mode_vrefresh(drm_mode);
251                 drm_mode->flags = 0;
252                 drm_mode->flags |= var->sync & FB_SYNC_HOR_HIGH_ACT ? V_PHSYNC : V_NHSYNC;
253                 drm_mode->flags |= var->sync & FB_SYNC_VERT_HIGH_ACT ? V_PVSYNC : V_NVSYNC;
254                 
255                 drm_mode_set_name(drm_mode);
256                 drm_mode_set_crtcinfo(drm_mode, CRTC_INTERLACE_HALVE_V);
257                 
258                 found = 0;
259                 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
260                         if (connector->encoder &&
261                             connector->encoder->crtc == par->set.crtc){
262                                 found = 1;
263                                 break;
264                         }
265                 }
266                 
267                 /* no connector bound, bail */
268                 if (!found)
269                         return -EINVAL;
270                 
271                 found = 0;
272                 drm_mode_debug_printmodeline(drm_mode);
273                 list_for_each_entry(search_mode, &connector->modes, head) {
274                         drm_mode_debug_printmodeline(search_mode);
275                         if (drm_mode_equal(drm_mode, search_mode)) {
276                                 drm_mode_destroy(dev, drm_mode);
277                                 drm_mode = search_mode;
278                                 found = 1;
279                                 break;
280                         }
281                 }
282                 
283                 /* If we didn't find a matching mode that exists on our connector,
284                  * create a new attachment for the incoming user specified mode
285                  */
286                 if (!found) {
287                         if (par->our_mode) {
288                                 /* this also destroys the mode */
289                                 drm_mode_detachmode_crtc(dev, par->our_mode);
290                         }
291                         
292                         par->set.mode = drm_mode;
293                         par->our_mode = drm_mode;
294                         drm_mode_debug_printmodeline(drm_mode);
295                         /* attach mode */
296                         drm_mode_attachmode_crtc(dev, par->set.crtc, par->set.mode);
297                 } else {
298                         par->set.mode = drm_mode;
299                         if (par->our_mode)
300                                 drm_mode_detachmode_crtc(dev, par->our_mode);
301                         par->our_mode = NULL;
302                 }
303                 return par->set.crtc->funcs->set_config(&par->set);
304 #endif
305                 return -EINVAL;
306         } else {
307                 struct drm_mode_set *modeset;
308                 int ret;
309
310                 list_for_each_entry(modeset, &par->mode_set_list, head) {
311                         if (modeset->num_connectors) {
312                                 ret = modeset->crtc->funcs->set_config(modeset);
313                                 if (ret)
314                                         return ret;
315                         }
316                 }
317                 return 0;
318         }
319 }
320
321 #if 0
322 static void intelfb_copyarea(struct fb_info *info,
323                         const struct fb_copyarea *region)
324 {
325         struct intelfb_par *par = info->par;
326         struct drm_device *dev = par->dev;
327         struct drm_i915_private *dev_priv = dev->dev_private;
328         u32 src_x1, src_y1, dst_x1, dst_y1, dst_x2, dst_y2, offset;
329         u32 cmd, rop_depth_pitch, src_pitch;
330         RING_LOCALS;
331
332         cmd = XY_SRC_COPY_BLT_CMD;
333         src_x1 = region->sx;
334         src_y1 = region->sy;
335         dst_x1 = region->dx;
336         dst_y1 = region->dy;
337         dst_x2 = region->dx + region->width;
338         dst_y2 = region->dy + region->height;
339         offset = par->fb->offset;
340         rop_depth_pitch = BLT_ROP_GXCOPY | par->fb->pitch;
341         src_pitch = par->fb->pitch;
342
343         switch (par->fb->bits_per_pixel) {
344         case 16:
345                 rop_depth_pitch |= BLT_DEPTH_16_565;
346                 break;
347         case 32:
348                 rop_depth_pitch |= BLT_DEPTH_32;
349                 cmd |= XY_SRC_COPY_BLT_WRITE_ALPHA | XY_SRC_COPY_BLT_WRITE_RGB;
350                 break;
351         }
352
353         BEGIN_LP_RING(8);
354         OUT_RING(cmd);
355         OUT_RING(rop_depth_pitch);
356         OUT_RING((dst_y1 << 16) | (dst_x1 & 0xffff));
357         OUT_RING((dst_y2 << 16) | (dst_x2 & 0xffff));
358         OUT_RING(offset);
359         OUT_RING((src_y1 << 16) | (src_x1 & 0xffff));
360         OUT_RING(src_pitch);
361         OUT_RING(offset);
362         ADVANCE_LP_RING();
363 }
364
365 #define ROUND_UP_TO(x, y)       (((x) + (y) - 1) / (y) * (y))
366 #define ROUND_DOWN_TO(x, y)     ((x) / (y) * (y))
367
368 void intelfb_imageblit(struct fb_info *info, const struct fb_image *image)
369 {
370         struct intelfb_par *par = info->par;
371         struct drm_device *dev = par->dev;
372         struct drm_i915_private *dev_priv = dev->dev_private;
373         u32 cmd, rop_pitch_depth, tmp;
374         int nbytes, ndwords, pad;
375         u32 dst_x1, dst_y1, dst_x2, dst_y2, offset, bg, fg;
376         int dat, ix, iy, iw;
377         int i, j;
378         RING_LOCALS;
379
380         /* size in bytes of a padded scanline */
381         nbytes = ROUND_UP_TO(image->width, 16) / 8;
382
383         /* Total bytes of padded scanline data to write out. */
384         nbytes *= image->height;
385
386         /*
387         * Check if the glyph data exceeds the immediate mode limit.
388         * It would take a large font (1K pixels) to hit this limit.
389         */
390         if (nbytes > 128 || image->depth != 1)
391                 return cfb_imageblit(info, image);
392
393         /* Src data is packaged a dword (32-bit) at a time. */
394         ndwords = ROUND_UP_TO(nbytes, 4) / 4;
395
396         /*
397         * Ring has to be padded to a quad word. But because the command starts
398         with 7 bytes, pad only if there is an even number of ndwords
399         */
400         pad = !(ndwords % 2);
401
402         DRM_DEBUG("imageblit %dx%dx%d to (%d,%d)\n", image->width,
403                 image->height, image->depth, image->dx, image->dy);
404         DRM_DEBUG("nbytes: %d, ndwords: %d, pad: %d\n", nbytes, ndwords, pad);
405
406         tmp = (XY_MONO_SRC_COPY_IMM_BLT & 0xff) + ndwords;
407         cmd = (XY_MONO_SRC_COPY_IMM_BLT & ~0xff) | tmp;
408         offset = par->fb->offset;
409         dst_x1 = image->dx;
410         dst_y1 = image->dy;
411         dst_x2 = image->dx + image->width;
412         dst_y2 = image->dy + image->height;
413         rop_pitch_depth = BLT_ROP_GXCOPY | par->fb->pitch;
414
415         switch (par->fb->bits_per_pixel) {
416         case 8:
417                 rop_pitch_depth |= BLT_DEPTH_8;
418                 fg = image->fg_color;
419                 bg = image->bg_color;
420                 break;
421         case 16:
422                 rop_pitch_depth |= BLT_DEPTH_16_565;
423                 fg = par->fb->pseudo_palette[image->fg_color];
424                 bg = par->fb->pseudo_palette[image->bg_color];
425                 break;
426         case 32:
427                 rop_pitch_depth |= BLT_DEPTH_32;
428                 cmd |= XY_SRC_COPY_BLT_WRITE_ALPHA | XY_SRC_COPY_BLT_WRITE_RGB;
429                 fg = par->fb->pseudo_palette[image->fg_color];
430                 bg = par->fb->pseudo_palette[image->bg_color];
431                 break;
432         default:
433                 DRM_ERROR("unknown depth %d\n", par->fb->bits_per_pixel);
434                 break;
435         }
436         
437         BEGIN_LP_RING(8 + ndwords);
438         OUT_RING(cmd);
439         OUT_RING(rop_pitch_depth);
440         OUT_RING((dst_y1 << 16) | (dst_x1 & 0xffff));
441         OUT_RING((dst_y2 << 16) | (dst_x2 & 0xffff));
442         OUT_RING(offset);
443         OUT_RING(bg);
444         OUT_RING(fg);
445         ix = iy = 0;
446         iw = ROUND_UP_TO(image->width, 8) / 8;
447         while (ndwords--) {
448                 dat = 0;
449                 for (j = 0; j < 2; ++j) {
450                         for (i = 0; i < 2; ++i) {
451                                 if (ix != iw || i == 0)
452                                         dat |= image->data[iy*iw + ix++] << (i+j*2)*8;
453                         }
454                         if (ix == iw && iy != (image->height - 1)) {
455                                 ix = 0;
456                                 ++iy;
457                         }
458                 }
459                 OUT_RING(dat);
460         }
461         if (pad)
462                 OUT_RING(MI_NOOP);
463         ADVANCE_LP_RING();
464 }
465 #endif
466 static int intelfb_pan_display(struct fb_var_screeninfo *var,
467                                 struct fb_info *info)
468 {
469         struct intelfb_par *par = info->par;
470         struct drm_mode_set *modeset;
471         int ret = 0;
472         DRM_DEBUG("\n");
473
474         list_for_each_entry(modeset, &par->mode_set_list, head) {
475                 modeset->x = var->xoffset;
476                 modeset->y = var->yoffset;
477
478                 if (modeset->num_connectors) {
479                         ret = modeset->crtc->funcs->set_config(modeset);
480                   
481                         if (!ret) {
482                                 info->var.xoffset = var->xoffset;
483                                 info->var.yoffset = var->yoffset;
484                         }
485                 }
486         }
487
488         return ret;
489 }
490
491 static struct fb_ops intelfb_ops = {
492         .owner = THIS_MODULE,
493         //.fb_open = intelfb_open,
494         //.fb_read = intelfb_read,
495         //.fb_write = intelfb_write,
496         //.fb_release = intelfb_release,
497         //.fb_ioctl = intelfb_ioctl,
498         .fb_check_var = intelfb_check_var,
499         .fb_set_par = intelfb_set_par,
500         .fb_setcolreg = intelfb_setcolreg,
501         .fb_fillrect = cfb_fillrect,
502         .fb_copyarea = cfb_copyarea, //intelfb_copyarea,
503         .fb_imageblit = cfb_imageblit, //intelfb_imageblit,
504         .fb_pan_display = intelfb_pan_display,
505 };
506
507 /**
508  * Curretly it is assumed that the old framebuffer is reused.
509  *
510  * LOCKING
511  * caller should hold the mode config lock.
512  *
513  */
514 int intelfb_resize(struct drm_device *dev, struct drm_crtc *crtc)
515 {
516         struct fb_info *info;
517         struct drm_framebuffer *fb;
518         struct drm_display_mode *mode = crtc->desired_mode;
519
520         fb = crtc->fb;
521         if (!fb)
522                 return 1;
523
524         info = fb->fbdev;
525         if (!info)
526                 return 1;
527
528         if (!mode)
529                 return 1;
530
531         info->var.xres = mode->hdisplay;
532         info->var.right_margin = mode->hsync_start - mode->hdisplay;
533         info->var.hsync_len = mode->hsync_end - mode->hsync_start;
534         info->var.left_margin = mode->htotal - mode->hsync_end;
535         info->var.yres = mode->vdisplay;
536         info->var.lower_margin = mode->vsync_start - mode->vdisplay;
537         info->var.vsync_len = mode->vsync_end - mode->vsync_start;
538         info->var.upper_margin = mode->vtotal - mode->vsync_end;
539         info->var.pixclock = 10000000 / mode->htotal * 1000 / mode->vtotal * 100;
540         /* avoid overflow */
541         info->var.pixclock = info->var.pixclock * 1000 / mode->vrefresh;
542
543         return 0;
544 }
545 EXPORT_SYMBOL(intelfb_resize);
546
547 int intelfb_create(struct drm_device *dev, uint32_t fb_width, uint32_t fb_height, struct intel_framebuffer **intel_fb_p)
548 {
549         struct fb_info *info;
550         struct intelfb_par *par;
551         struct drm_framebuffer *fb;
552         struct intel_framebuffer *intel_fb;
553         struct drm_mode_fb_cmd mode_cmd;
554         struct drm_buffer_object *fbo = NULL;
555         struct device *device = &dev->pdev->dev; 
556         int ret;
557
558         mode_cmd.width = fb_width;/* crtc->desired_mode->hdisplay; */
559         mode_cmd.height = fb_height;/* crtc->desired_mode->vdisplay; */
560         
561         mode_cmd.bpp = 32;
562         mode_cmd.pitch = mode_cmd.width * ((mode_cmd.bpp + 1) / 8);
563         mode_cmd.depth = 24;
564
565         ret = drm_buffer_object_create(dev, mode_cmd.pitch * mode_cmd.height, 
566                                         drm_bo_type_kernel,
567                                         DRM_BO_FLAG_READ |
568                                         DRM_BO_FLAG_WRITE |
569                                         DRM_BO_FLAG_MEM_TT |
570                                         DRM_BO_FLAG_MEM_VRAM |
571                                         DRM_BO_FLAG_NO_EVICT,
572                                         DRM_BO_HINT_DONT_FENCE, 0, 0,
573                                         &fbo);
574         if (ret || !fbo) {
575                 printk(KERN_ERR "failed to allocate framebuffer\n");
576                 return -EINVAL;
577         }
578         
579
580         fb = intel_user_framebuffer_create(dev, NULL, &mode_cmd);
581         if (!fb) {
582                 drm_bo_usage_deref_unlocked(&fbo);
583                 DRM_ERROR("failed to allocate fb.\n");
584                 return -EINVAL;
585         }
586
587         intel_fb = to_intel_framebuffer(fb);
588         *intel_fb_p = intel_fb;
589
590         intel_fb->bo = fbo;
591
592         info = framebuffer_alloc(sizeof(struct intelfb_par), device);
593         if (!info)
594                 return -EINVAL;
595
596         par = info->par;
597
598         strcpy(info->fix.id, "inteldrmfb");
599         info->fix.type = FB_TYPE_PACKED_PIXELS;
600         info->fix.visual = FB_VISUAL_TRUECOLOR;
601         info->fix.type_aux = 0;
602         info->fix.xpanstep = 1; /* doing it in hw */
603         info->fix.ypanstep = 1; /* doing it in hw */
604         info->fix.ywrapstep = 0;
605         info->fix.accel = FB_ACCEL_I830;
606         info->fix.type_aux = 0;
607
608         info->flags = FBINFO_DEFAULT;
609
610         info->fbops = &intelfb_ops;
611
612         info->fix.line_length = fb->pitch;
613         info->fix.smem_start = intel_fb->bo->offset + dev->mode_config.fb_base;
614         info->fix.smem_len = info->fix.line_length * fb->height;
615
616         info->flags = FBINFO_DEFAULT;
617
618         ret = drm_bo_kmap(intel_fb->bo, 0, intel_fb->bo->num_pages, &intel_fb->kmap);
619         if (ret)
620                 DRM_ERROR("error mapping fb: %d\n", ret);
621
622         info->screen_base = intel_fb->kmap.virtual;
623         info->screen_size = info->fix.smem_len; /* FIXME */
624         info->pseudo_palette = fb->pseudo_palette;
625         info->var.xres_virtual = fb->width;
626         info->var.yres_virtual = fb->height;
627         info->var.bits_per_pixel = fb->bits_per_pixel;
628         info->var.xoffset = 0;
629         info->var.yoffset = 0;
630         info->var.activate = FB_ACTIVATE_NOW;
631         info->var.height = -1;
632         info->var.width = -1;
633
634         info->var.xres = fb_width;
635         info->var.yres = fb_height;
636
637         if (IS_I9XX(dev)) {
638                 info->fix.mmio_start = pci_resource_start(dev->pdev, 0);
639                 info->fix.mmio_len = pci_resource_len(dev->pdev, 0);
640         } else {
641                 info->fix.mmio_start = pci_resource_start(dev->pdev, 1);
642                 info->fix.mmio_len = pci_resource_len(dev->pdev, 1);
643         }
644
645         info->pixmap.size = 64*1024;
646         info->pixmap.buf_align = 8;
647         info->pixmap.access_align = 32;
648         info->pixmap.flags = FB_PIXMAP_SYSTEM;
649         info->pixmap.scan_align = 1;
650
651         DRM_DEBUG("fb depth is %d\n", fb->depth);
652         DRM_DEBUG("   pitch is %d\n", fb->pitch);
653         switch(fb->depth) {
654         case 8:
655                 info->var.red.offset = 0;
656                 info->var.green.offset = 0;
657                 info->var.blue.offset = 0;
658                 info->var.red.length = 8; /* 8bit DAC */
659                 info->var.green.length = 8;
660                 info->var.blue.length = 8;
661                 info->var.transp.offset = 0;
662                 info->var.transp.length = 0;
663                 break;
664         case 15:
665                 info->var.red.offset = 10;
666                 info->var.green.offset = 5;
667                 info->var.blue.offset = 0;
668                 info->var.red.length = 5;
669                 info->var.green.length = 5;
670                 info->var.blue.length = 5;
671                 info->var.transp.offset = 15;
672                 info->var.transp.length = 1;
673                 break;
674         case 16:
675                 info->var.red.offset = 11;
676                 info->var.green.offset = 5;
677                 info->var.blue.offset = 0;
678                 info->var.red.length = 5;
679                 info->var.green.length = 6;
680                 info->var.blue.length = 5;
681                 info->var.transp.offset = 0;
682                 break;
683         case 24:
684                 info->var.red.offset = 16;
685                 info->var.green.offset = 8;
686                 info->var.blue.offset = 0;
687                 info->var.red.length = 8;
688                 info->var.green.length = 8;
689                 info->var.blue.length = 8;
690                 info->var.transp.offset = 0;
691                 info->var.transp.length = 0;
692                 break;
693         case 32:
694                 info->var.red.offset = 16;
695                 info->var.green.offset = 8;
696                 info->var.blue.offset = 0;
697                 info->var.red.length = 8;
698                 info->var.green.length = 8;
699                 info->var.blue.length = 8;
700                 info->var.transp.offset = 24;
701                 info->var.transp.length = 8;
702                 break;
703         default:
704                 break;
705         }
706
707         fb->fbdev = info;
708
709         INIT_LIST_HEAD(&par->mode_set_list);
710
711         par->intel_fb = intel_fb;
712
713         /* To allow resizeing without swapping buffers */
714         printk("allocated %dx%d fb: 0x%08lx, bo %p\n", intel_fb->base.width,
715                intel_fb->base.height, intel_fb->bo->offset, fbo);
716
717         return 0;
718 }
719
720 int intelfb_probe(struct drm_device *dev)
721 {
722         struct fb_info *info;
723         struct intelfb_par *par;
724         struct device *device = &dev->pdev->dev; 
725         struct intel_framebuffer *intel_fb;
726         struct drm_mode_set *modeset;
727         struct drm_crtc *crtc;
728         struct drm_connector *connector;
729         int ret;
730         int crtc_count = 0, i;
731         unsigned int fb_width = (unsigned)-1, fb_height = (unsigned)-1;
732
733         DRM_DEBUG("\n");
734
735         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
736                 if (crtc->desired_mode) {
737                         if (crtc->desired_mode->hdisplay < fb_width)
738                                 fb_width = crtc->desired_mode->hdisplay;
739
740                         if (crtc->desired_mode->vdisplay < fb_height)
741                                 fb_height = crtc->desired_mode->vdisplay;
742                 }
743                 crtc_count++;
744         }
745
746         if (fb_width == -1 || fb_height == -1) {
747                 return -EINVAL;
748         }
749
750         DRM_DEBUG("here %d %d\n", fb_width, fb_height);
751         ret = intelfb_create(dev, fb_width, fb_height, &intel_fb);
752         if (ret)
753                 return -EINVAL;
754
755         DRM_DEBUG("here %p\n", intel_fb);
756         info = intel_fb->base.fbdev;
757         par = info->par;
758
759         DRM_DEBUG("crtc count is %d\n", crtc_count);
760         /* make up a couple of config sets */
761         for (i = 0; i < crtc_count; i++) {
762                 int conn_count = 0;
763
764                 modeset = kzalloc(sizeof(struct drm_mode_set), GFP_KERNEL);
765                 modeset->fb = &intel_fb->base;
766
767                 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
768                         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
769                         if (intel_crtc->pipe == i)
770                                 modeset->crtc = crtc;
771                 }
772
773
774                 DRM_DEBUG("1\n");
775                 /* find out how many connectors are on this */
776                 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
777                         if (connector->encoder)
778                                 if (connector->encoder->crtc == modeset->crtc)
779                                         conn_count++;
780                 }
781
782                 list_add_tail(&modeset->head, &par->mode_set_list);
783
784                 if (!conn_count)
785                         continue;
786                 
787                 DRM_DEBUG("cc %d\n", conn_count);
788                 modeset->connectors = kcalloc(conn_count, sizeof(struct drm_connector *), GFP_KERNEL);
789                 if (!modeset->connectors) {
790                         ret = -ENOMEM;
791                         goto fail;
792                 }
793
794                 modeset->num_connectors = conn_count;
795                 
796                 conn_count = 0;
797                 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
798                         if (connector->encoder)
799                                 if (connector->encoder->crtc == modeset->crtc)
800                                         modeset->connectors[conn_count++] = connector;
801                 }
802                 modeset->mode = modeset->crtc->desired_mode;
803         }
804        
805         info->var.pixclock = -1;
806 #if 0
807         info->var.right_margin = mode->hsync_start - mode->hdisplay;
808         info->var.hsync_len = mode->hsync_end - mode->hsync_start;
809         info->var.left_margin = mode->htotal - mode->hsync_end;
810         info->var.lower_margin = mode->vsync_start - mode->vdisplay;
811         info->var.vsync_len = mode->vsync_end - mode->vsync_start;
812         info->var.upper_margin = mode->vtotal - mode->vsync_end;
813         info->var.pixclock = KHZ2PICOS(mode->clock);
814
815         if (mode->flags & V_PHSYNC)
816                 info->var.sync |= FB_SYNC_HOR_HIGH_ACT;
817
818         if (mode->flags & V_PVSYNC)
819                 info->var.sync |= FB_SYNC_VERT_HIGH_ACT;
820
821         if (mode->flags & V_INTERLACE)
822                 info->var.vmode = FB_VMODE_INTERLACED;
823         else if (mode->flags & V_DBLSCAN)
824                 info->var.vmode = FB_VMODE_DOUBLE;
825         else
826                 info->var.vmode = FB_VMODE_NONINTERLACED;
827
828 #endif
829
830         if (register_framebuffer(info) < 0)
831                 return -EINVAL;
832
833         printk(KERN_INFO "fb%d: %s frame buffer device\n", info->node,
834                 info->fix.id);
835         return 0;
836 fail:
837         /*  TODO */
838         return ret;
839 }
840 EXPORT_SYMBOL(intelfb_probe);
841
842 int intelfb_remove(struct drm_device *dev, struct drm_framebuffer *fb)
843 {
844         struct fb_info *info;
845         struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
846
847         if (!fb)
848                 return -EINVAL;
849
850         info = fb->fbdev;
851         
852         if (info) {
853                 unregister_framebuffer(info);
854                 drm_bo_kunmap(&intel_fb->kmap);
855                 drm_bo_usage_deref_unlocked(&intel_fb->bo);
856                 framebuffer_release(info);
857         }
858         return 0;
859 }
860 EXPORT_SYMBOL(intelfb_remove);
861 MODULE_LICENSE("GPL");