156f8ad16c4caa4a2eed50df0a7a4389584f6320
[linux-2.6.git] / drivers / staging / gma500 / psb_fb.c
1 /**************************************************************************
2  * Copyright (c) 2007, Intel Corporation.
3  * All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU General Public License,
7  * version 2, as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
17  *
18  **************************************************************************/
19
20 #include <linux/module.h>
21 #include <linux/kernel.h>
22 #include <linux/errno.h>
23 #include <linux/string.h>
24 #include <linux/mm.h>
25 #include <linux/tty.h>
26 #include <linux/slab.h>
27 #include <linux/delay.h>
28 #include <linux/fb.h>
29 #include <linux/init.h>
30 #include <linux/console.h>
31
32 #include <drm/drmP.h>
33 #include <drm/drm.h>
34 #include <drm/drm_crtc.h>
35
36 #include "psb_drv.h"
37 #include "psb_intel_reg.h"
38 #include "psb_intel_drv.h"
39 #include "psb_fb.h"
40
41 static void psb_user_framebuffer_destroy(struct drm_framebuffer *fb);
42 static int psb_user_framebuffer_create_handle(struct drm_framebuffer *fb,
43                                               struct drm_file *file_priv,
44                                               unsigned int *handle);
45
46 static const struct drm_framebuffer_funcs psb_fb_funcs = {
47         .destroy = psb_user_framebuffer_destroy,
48         .create_handle = psb_user_framebuffer_create_handle,
49 };
50
51 #define CMAP_TOHW(_val, _width) ((((_val) << (_width)) + 0x7FFF - (_val)) >> 16)
52
53 void *psbfb_vdc_reg(struct drm_device *dev)
54 {
55         struct drm_psb_private *dev_priv;
56         dev_priv = (struct drm_psb_private *) dev->dev_private;
57         return dev_priv->vdc_reg;
58 }
59 /*EXPORT_SYMBOL(psbfb_vdc_reg); */
60
61 static int psbfb_setcolreg(unsigned regno, unsigned red, unsigned green,
62                            unsigned blue, unsigned transp,
63                            struct fb_info *info)
64 {
65         struct psb_fbdev *fbdev = info->par;
66         struct drm_framebuffer *fb = fbdev->psb_fb_helper.fb;
67         uint32_t v;
68
69         if (!fb)
70                 return -ENOMEM;
71
72         if (regno > 255)
73                 return 1;
74
75         red = CMAP_TOHW(red, info->var.red.length);
76         blue = CMAP_TOHW(blue, info->var.blue.length);
77         green = CMAP_TOHW(green, info->var.green.length);
78         transp = CMAP_TOHW(transp, info->var.transp.length);
79
80         v = (red << info->var.red.offset) |
81             (green << info->var.green.offset) |
82             (blue << info->var.blue.offset) |
83             (transp << info->var.transp.offset);
84
85         if (regno < 16) {
86                 switch (fb->bits_per_pixel) {
87                 case 16:
88                         ((uint32_t *) info->pseudo_palette)[regno] = v;
89                         break;
90                 case 24:
91                 case 32:
92                         ((uint32_t *) info->pseudo_palette)[regno] = v;
93                         break;
94                 }
95         }
96
97         return 0;
98 }
99
100 static int psbfb_kms_off(struct drm_device *dev, int suspend)
101 {
102         struct drm_framebuffer *fb = 0;
103         struct psb_framebuffer *psbfb = to_psb_fb(fb);
104         DRM_DEBUG("psbfb_kms_off_ioctl\n");
105
106         mutex_lock(&dev->mode_config.mutex);
107         list_for_each_entry(fb, &dev->mode_config.fb_list, head) {
108                 struct fb_info *info = psbfb->fbdev;
109
110                 if (suspend) {
111                         fb_set_suspend(info, 1);
112                         drm_fb_helper_blank(FB_BLANK_POWERDOWN, info);
113                 }
114         }
115         mutex_unlock(&dev->mode_config.mutex);
116         return 0;
117 }
118
119 int psbfb_kms_off_ioctl(struct drm_device *dev, void *data,
120                         struct drm_file *file_priv)
121 {
122         int ret;
123
124         if (drm_psb_no_fb)
125                 return 0;
126         console_lock();
127         ret = psbfb_kms_off(dev, 0);
128         console_unlock();
129
130         return ret;
131 }
132
133 static int psbfb_kms_on(struct drm_device *dev, int resume)
134 {
135         struct drm_framebuffer *fb = 0;
136         struct psb_framebuffer *psbfb = to_psb_fb(fb);
137
138         DRM_DEBUG("psbfb_kms_on_ioctl\n");
139
140         mutex_lock(&dev->mode_config.mutex);
141         list_for_each_entry(fb, &dev->mode_config.fb_list, head) {
142                 struct fb_info *info = psbfb->fbdev;
143
144                 if (resume) {
145                         fb_set_suspend(info, 0);
146                         drm_fb_helper_blank(FB_BLANK_UNBLANK, info);
147                 }
148         }
149         mutex_unlock(&dev->mode_config.mutex);
150
151         return 0;
152 }
153
154 int psbfb_kms_on_ioctl(struct drm_device *dev, void *data,
155                        struct drm_file *file_priv)
156 {
157         int ret;
158
159         if (drm_psb_no_fb)
160                 return 0;
161         console_lock();
162         ret = psbfb_kms_on(dev, 0);
163         console_unlock();
164         drm_helper_disable_unused_functions(dev);
165         return ret;
166 }
167
168 void psbfb_suspend(struct drm_device *dev)
169 {
170         console_lock();
171         psbfb_kms_off(dev, 1);
172         console_unlock();
173 }
174
175 void psbfb_resume(struct drm_device *dev)
176 {
177         console_lock();
178         psbfb_kms_on(dev, 1);
179         console_unlock();
180         drm_helper_disable_unused_functions(dev);
181 }
182
183 static int psbfb_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
184 {
185         int page_num = 0;
186         int i;
187         unsigned long address = 0;
188         int ret;
189         unsigned long pfn;
190         struct psb_framebuffer *psbfb = vma->vm_private_data;
191         struct drm_device *dev = psbfb->base.dev;
192         struct drm_psb_private *dev_priv = dev->dev_private;
193         unsigned long phys_addr = (unsigned long)dev_priv->stolen_base;
194
195         page_num = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
196
197         address = (unsigned long)vmf->virtual_address;
198
199         vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
200
201         for (i = 0; i < page_num; i++) {
202                 pfn = (phys_addr >> PAGE_SHIFT); /* phys_to_pfn(phys_addr); */
203
204                 ret = vm_insert_mixed(vma, address, pfn);
205                 if (unlikely((ret == -EBUSY) || (ret != 0 && i > 0)))
206                         break;
207                 else if (unlikely(ret != 0)) {
208                         ret = (ret == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS;
209                         return ret;
210                 }
211
212                 address += PAGE_SIZE;
213                 phys_addr += PAGE_SIZE;
214         }
215
216         return VM_FAULT_NOPAGE;
217 }
218
219 static void psbfb_vm_open(struct vm_area_struct *vma)
220 {
221         DRM_DEBUG("vm_open\n");
222 }
223
224 static void psbfb_vm_close(struct vm_area_struct *vma)
225 {
226         DRM_DEBUG("vm_close\n");
227 }
228
229 static struct vm_operations_struct psbfb_vm_ops = {
230         .fault  = psbfb_vm_fault,
231         .open   = psbfb_vm_open,
232         .close  = psbfb_vm_close
233 };
234
235 static int psbfb_mmap(struct fb_info *info, struct vm_area_struct *vma)
236 {
237         struct psb_fbdev *fbdev = info->par;
238         struct psb_framebuffer *psbfb = &fbdev->pfb;
239         char *fb_screen_base = NULL;
240         struct drm_device *dev = psbfb->base.dev;
241         struct drm_psb_private *dev_priv = dev->dev_private;
242
243         if (vma->vm_pgoff != 0)
244                 return -EINVAL;
245         if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
246                 return -EINVAL;
247
248         if (!psbfb->addr_space)
249                 psbfb->addr_space = vma->vm_file->f_mapping;
250
251         fb_screen_base = (char *)info->screen_base;
252
253         DRM_DEBUG("vm_pgoff 0x%lx, screen base %p vram_addr %p\n",
254                                 vma->vm_pgoff, fb_screen_base,
255                                 dev_priv->vram_addr);
256
257         /* If this is a GEM object then info->screen_base is the virtual
258            kernel remapping of the object. FIXME: Review if this is
259            suitable for our mmap work */
260         vma->vm_ops = &psbfb_vm_ops;
261         vma->vm_private_data = (void *)psbfb;
262         vma->vm_flags |= VM_RESERVED | VM_IO |
263                                         VM_MIXEDMAP | VM_DONTEXPAND;
264         return 0;
265 }
266
267 static int psbfb_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg)
268 {
269         struct psb_fbdev *fbdev = info->par;
270         struct psb_framebuffer *psbfb = &fbdev->pfb;
271         struct drm_device *dev = psbfb->base.dev;
272         struct drm_psb_private *dev_priv = dev->dev_private;
273         u32 __user *p = (u32 __user *)arg;
274         u32 l;
275         u32 buf[32];
276         switch (cmd) {
277         case 0x12345678:
278                 if (!capable(CAP_SYS_RAWIO))
279                         return -EPERM;
280                 if (get_user(l, p))
281                         return -EFAULT;
282                 if (l > 32)
283                         return -EMSGSIZE;
284                 if (copy_from_user(buf, p + 1, l * sizeof(u32)))
285                         return -EFAULT;
286                 psbfb_2d_submit(dev_priv, buf, l);
287                 return 0;
288         default:
289                 return -ENOTTY;
290         }
291 }
292
293 static struct fb_ops psbfb_ops = {
294         .owner = THIS_MODULE,
295         .fb_check_var = drm_fb_helper_check_var,
296         .fb_set_par = drm_fb_helper_set_par,
297         .fb_blank = drm_fb_helper_blank,
298         .fb_setcolreg = psbfb_setcolreg,
299         .fb_fillrect = psbfb_fillrect,
300         .fb_copyarea = psbfb_copyarea,
301         .fb_imageblit = psbfb_imageblit,
302         .fb_mmap = psbfb_mmap,
303         .fb_sync = psbfb_sync,
304         .fb_ioctl = psbfb_ioctl,
305 };
306
307 /**
308  *      psb_framebuffer_init    -       initialize a framebuffer
309  *      @dev: our DRM device
310  *      @fb: framebuffer to set up
311  *      @mode_cmd: mode description
312  *      @gt: backing object
313  *
314  *      Configure and fill in the boilerplate for our frame buffer. Return
315  *      0 on success or an error code if we fail.
316  */
317 static int psb_framebuffer_init(struct drm_device *dev,
318                                 struct psb_framebuffer *fb,
319                                 struct drm_mode_fb_cmd *mode_cmd,
320                                 struct gtt_range *gt)
321 {
322         int ret;
323
324         if (mode_cmd->pitch & 63)
325                 return -EINVAL;
326         switch (mode_cmd->bpp) {
327         case 8:
328         case 16:
329         case 24:
330         case 32:
331                 break;
332         default:
333                 return -EINVAL;
334         }
335         ret = drm_framebuffer_init(dev, &fb->base, &psb_fb_funcs);
336         if (ret) {
337                 dev_err(dev->dev, "framebuffer init failed: %d\n", ret);
338                 return ret;
339         }
340         drm_helper_mode_fill_fb_struct(&fb->base, mode_cmd);
341         fb->gtt = gt;
342         return 0;
343 }
344                 
345 /**
346  *      psb_framebuffer_create  -       create a framebuffer backed by gt
347  *      @dev: our DRM device
348  *      @mode_cmd: the description of the requested mode
349  *      @gt: the backing object
350  *
351  *      Create a framebuffer object backed by the gt, and fill in the
352  *      boilerplate required
353  *
354  *      TODO: review object references
355  */
356 static struct drm_framebuffer *psb_framebuffer_create
357                         (struct drm_device *dev,
358                          struct drm_mode_fb_cmd *mode_cmd,
359                          struct gtt_range *gt)
360 {
361         struct psb_framebuffer *fb;
362         int ret;
363
364         fb = kzalloc(sizeof(*fb), GFP_KERNEL);
365         if (!fb)
366                 return ERR_PTR(-ENOMEM);
367
368         ret = psb_framebuffer_init(dev, fb, mode_cmd, gt);
369         if (ret) {
370                 kfree(fb);
371                 return ERR_PTR(ret);
372         }
373         return &fb->base;
374 }
375
376 /**
377  *      psbfb_alloc             -       allocate frame buffer memory
378  *      @dev: the DRM device
379  *      @aligned_size: space needed
380  *
381  *      Allocate the frame buffer. In the usual case we get a GTT range that
382  *      is stolen memory backed and life is simple. If there isn't sufficient
383  *      stolen memory or the system has no stolen memory we allocate a range
384  *      and back it with a GEM object.
385  *
386  *      In this case the GEM object has no handle. 
387  *
388  *      FIXME: console speed up - allocate twice the space if room and use
389  *      hardware scrolling for acceleration.
390  */
391 static struct gtt_range *psbfb_alloc(struct drm_device *dev, int aligned_size)
392 {
393         struct gtt_range *backing;
394         /* Begin by trying to use stolen memory backing */
395         backing = psb_gtt_alloc_range(dev, aligned_size, "fb", 1);
396         if (backing)
397                 return backing;
398         /* Next try using GEM host memory */
399         backing = psb_gtt_alloc_range(dev, aligned_size, "fb(gem)", 0);
400         if (backing == NULL)
401                 return NULL;
402
403         /* Now back it with an object */
404         if (drm_gem_object_init(dev, &backing->gem, aligned_size) != 0) {
405                 psb_gtt_free_range(dev, backing);
406                 return NULL;
407         }
408         return backing;
409 }
410         
411 /**
412  *      psbfb_create            -       create a framebuffer
413  *      @fbdev: the framebuffer device
414  *      @sizes: specification of the layout
415  *
416  *      Create a framebuffer to the specifications provided
417  */
418 static int psbfb_create(struct psb_fbdev *fbdev,
419                                 struct drm_fb_helper_surface_size *sizes)
420 {
421         struct drm_device *dev = fbdev->psb_fb_helper.dev;
422         struct drm_psb_private *dev_priv = dev->dev_private;
423         struct fb_info *info;
424         struct drm_framebuffer *fb;
425         struct psb_framebuffer *psbfb = &fbdev->pfb;
426         struct drm_mode_fb_cmd mode_cmd;
427         struct device *device = &dev->pdev->dev;
428         int size;
429         int ret;
430         struct gtt_range *backing;
431
432         mode_cmd.width = sizes->surface_width;
433         mode_cmd.height = sizes->surface_height;
434         mode_cmd.bpp = sizes->surface_bpp;
435
436         /* No 24bit packed */
437         if (mode_cmd.bpp == 24)
438                 mode_cmd.bpp = 32;
439
440         /* HW requires pitch to be 64 byte aligned */
441         mode_cmd.pitch =  ALIGN(mode_cmd.width * ((mode_cmd.bpp + 7) / 8), 64);
442         mode_cmd.depth = sizes->surface_depth;
443
444         size = mode_cmd.pitch * mode_cmd.height;
445         size = ALIGN(size, PAGE_SIZE);
446
447         /* Allocate the framebuffer in the GTT with stolen page backing */
448         backing = psbfb_alloc(dev, size);
449         if (backing == NULL)
450                 return -ENOMEM;
451
452         mutex_lock(&dev->struct_mutex);
453
454         info = framebuffer_alloc(0, device);
455         if (!info) {
456                 ret = -ENOMEM;
457                 goto out_err1;
458         }
459         info->par = fbdev;
460
461         ret = psb_framebuffer_init(dev, psbfb, &mode_cmd, backing);
462         if (ret)
463                 goto out_unref;
464
465         fb = &psbfb->base;
466         psbfb->fbdev = info;
467
468         fbdev->psb_fb_helper.fb = fb;
469         fbdev->psb_fb_helper.fbdev = info;
470
471         strcpy(info->fix.id, "psbfb");
472
473         info->flags = FBINFO_DEFAULT;
474         info->fbops = &psbfb_ops;
475
476         ret = fb_alloc_cmap(&info->cmap, 256, 0);
477         if (ret) {
478                 ret = -ENOMEM;
479                 goto out_unref;
480         }
481
482         info->fix.smem_start = dev->mode_config.fb_base;
483         info->fix.smem_len = size;
484
485         if (backing->stolen) {
486                 /* Accessed stolen memory directly */
487                 info->screen_base = (char *)dev_priv->vram_addr +
488                                                         backing->offset;
489         } else {
490                 /* Pin the pages into the GTT and create a mapping to them */
491                 psb_gtt_pin(backing);
492                 info->screen_base = vm_map_ram(backing->pages, backing->npage,
493                                 -1, PAGE_KERNEL);
494                 if (info->screen_base == NULL) {
495                         psb_gtt_unpin(backing);
496                         ret = -ENOMEM;
497                         goto out_unref;
498                 }
499                 psbfb->vm_map = 1;
500         }
501         info->screen_size = size;
502 /*      memset(info->screen_base, 0, size); */
503
504         if (dev_priv->pg->stolen_size) {
505                 info->apertures = alloc_apertures(1);
506                 if (!info->apertures) {
507                         ret = -ENOMEM;
508                         goto out_unref;
509                 }
510                 info->apertures->ranges[0].base = dev->mode_config.fb_base;
511                 info->apertures->ranges[0].size = dev_priv->pg->stolen_size;
512         }
513
514         drm_fb_helper_fill_fix(info, fb->pitch, fb->depth);
515         drm_fb_helper_fill_var(info, &fbdev->psb_fb_helper,
516                                 sizes->fb_width, sizes->fb_height);
517
518         info->fix.mmio_start = pci_resource_start(dev->pdev, 0);
519         info->fix.mmio_len = pci_resource_len(dev->pdev, 0);
520
521         info->pixmap.size = 64 * 1024;
522         info->pixmap.buf_align = 8;
523         info->pixmap.access_align = 32;
524         info->pixmap.flags = FB_PIXMAP_SYSTEM;
525         info->pixmap.scan_align = 1;
526
527         DRM_DEBUG("fb depth is %d\n", fb->depth);
528         DRM_DEBUG("   pitch is %d\n", fb->pitch);
529
530         printk(KERN_INFO"allocated %dx%d fb\n",
531                                 psbfb->base.width, psbfb->base.height);
532
533         mutex_unlock(&dev->struct_mutex);
534
535         return 0;
536 out_unref:
537         if (backing->stolen)
538                 psb_gtt_free_range(dev, backing);
539         else {
540                 if (psbfb->vm_map)
541                         vm_unmap_ram(info->screen_base, backing->npage);
542                 drm_gem_object_unreference(&backing->gem);
543         }
544 out_err1:
545         mutex_unlock(&dev->struct_mutex);
546         psb_gtt_free_range(dev, backing);
547         return ret;
548 }
549
550 /**
551  *      psb_user_framebuffer_create     -       create framebuffer
552  *      @dev: our DRM device
553  *      @filp: client file
554  *      @cmd: mode request
555  *
556  *      Create a new framebuffer backed by a userspace GEM object
557  */
558 static struct drm_framebuffer *psb_user_framebuffer_create
559                         (struct drm_device *dev, struct drm_file *filp,
560                          struct drm_mode_fb_cmd *cmd)
561 {
562         struct gtt_range *r;
563         struct drm_gem_object *obj;
564
565         /* Find the GEM object and thus the gtt range object that is
566            to back this space */
567         obj = drm_gem_object_lookup(dev, filp, cmd->handle);
568         if (obj == NULL)
569                 return ERR_PTR(-ENOENT);
570
571         /* Let the core code do all the work */
572         r = container_of(obj, struct gtt_range, gem);
573         return psb_framebuffer_create(dev, cmd, r);
574 }
575
576 static void psbfb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
577                                                         u16 blue, int regno)
578 {
579         DRM_DEBUG("%s\n", __func__);
580 }
581
582 static void psbfb_gamma_get(struct drm_crtc *crtc, u16 *red,
583                                         u16 *green, u16 *blue, int regno)
584 {
585         DRM_DEBUG("%s\n", __func__);
586 }
587
588 static int psbfb_probe(struct drm_fb_helper *helper,
589                                 struct drm_fb_helper_surface_size *sizes)
590 {
591         struct psb_fbdev *psb_fbdev = (struct psb_fbdev *)helper;
592         int new_fb = 0;
593         int ret;
594
595         DRM_DEBUG("%s\n", __func__);
596
597         if (!helper->fb) {
598                 ret = psbfb_create(psb_fbdev, sizes);
599                 if (ret)
600                         return ret;
601                 new_fb = 1;
602         }
603         return new_fb;
604 }
605
606 struct drm_fb_helper_funcs psb_fb_helper_funcs = {
607         .gamma_set = psbfb_gamma_set,
608         .gamma_get = psbfb_gamma_get,
609         .fb_probe = psbfb_probe,
610 };
611
612 int psb_fbdev_destroy(struct drm_device *dev, struct psb_fbdev *fbdev)
613 {
614         struct fb_info *info;
615         struct psb_framebuffer *psbfb = &fbdev->pfb;
616
617         if (fbdev->psb_fb_helper.fbdev) {
618                 info = fbdev->psb_fb_helper.fbdev;
619
620                 /* If this is our base framebuffer then kill any virtual map
621                    for the framebuffer layer and unpin it */
622                 if (psbfb->vm_map) {
623                         vm_unmap_ram(info->screen_base, psbfb->gtt->npage);
624                         psb_gtt_unpin(psbfb->gtt);
625                 }
626                 unregister_framebuffer(info);
627                 if (info->cmap.len)
628                         fb_dealloc_cmap(&info->cmap);
629                 framebuffer_release(info);
630         }
631         drm_fb_helper_fini(&fbdev->psb_fb_helper);
632         drm_framebuffer_cleanup(&psbfb->base);
633         
634         if (psbfb->gtt) {
635                 /* FIXME: this is a bit more inside knowledge than I'd like
636                    but I don't see how to make a fake GEM object of the
637                    stolen space nicely */
638                 if (psbfb->gtt->stolen)
639                         psb_gtt_free_range(dev, psbfb->gtt);
640                 else
641                         drm_gem_object_unreference(&psbfb->gtt->gem);
642         }
643         return 0;
644 }
645
646 int psb_fbdev_init(struct drm_device *dev)
647 {
648         struct psb_fbdev *fbdev;
649         struct drm_psb_private *dev_priv = dev->dev_private;
650         int num_crtc;
651
652         fbdev = kzalloc(sizeof(struct psb_fbdev), GFP_KERNEL);
653         if (!fbdev) {
654                 DRM_ERROR("no memory\n");
655                 return -ENOMEM;
656         }
657
658         dev_priv->fbdev = fbdev;
659         fbdev->psb_fb_helper.funcs = &psb_fb_helper_funcs;
660
661         num_crtc = 2;
662
663         drm_fb_helper_init(dev, &fbdev->psb_fb_helper, num_crtc,
664                                                         INTELFB_CONN_LIMIT);
665
666         drm_fb_helper_single_add_all_connectors(&fbdev->psb_fb_helper);
667         drm_fb_helper_initial_config(&fbdev->psb_fb_helper, 32);
668         return 0;
669 }
670
671 void psb_fbdev_fini(struct drm_device *dev)
672 {
673         struct drm_psb_private *dev_priv = dev->dev_private;
674
675         if (!dev_priv->fbdev)
676                 return;
677
678         psb_fbdev_destroy(dev, dev_priv->fbdev);
679         kfree(dev_priv->fbdev);
680         dev_priv->fbdev = NULL;
681 }
682
683 static void psbfb_output_poll_changed(struct drm_device *dev)
684 {
685         struct drm_psb_private *dev_priv = dev->dev_private;
686         struct psb_fbdev *fbdev = (struct psb_fbdev *)dev_priv->fbdev;
687         drm_fb_helper_hotplug_event(&fbdev->psb_fb_helper);
688 }
689
690 /**
691  *      psb_user_framebuffer_create_handle - add hamdle to a framebuffer
692  *      @fb: framebuffer
693  *      @file_priv: our DRM file
694  *      @handle: returned handle
695  *
696  *      Our framebuffer object is a GTT range which also contains a GEM
697  *      object. We need to turn it into a handle for userspace. GEM will do
698  *      the work for us
699  */
700 static int psb_user_framebuffer_create_handle(struct drm_framebuffer *fb,
701                                               struct drm_file *file_priv,
702                                               unsigned int *handle)
703 {
704         struct psb_framebuffer *psbfb = to_psb_fb(fb);
705         struct gtt_range *r = psbfb->gtt;
706         if (r->stolen)
707                 return -EOPNOTSUPP;
708         return drm_gem_handle_create(file_priv, &r->gem, handle);
709 }
710
711 /**
712  *      psb_user_framebuffer_destroy    -       destruct user created fb
713  *      @fb: framebuffer
714  *
715  *      User framebuffers are backed by GEM objects so all we have to do is
716  *      clean up a bit and drop the reference, GEM will handle the fallout
717  */
718 static void psb_user_framebuffer_destroy(struct drm_framebuffer *fb)
719 {
720         struct psb_framebuffer *psbfb = to_psb_fb(fb);
721         struct gtt_range *r = psbfb->gtt;
722         struct drm_device *dev = fb->dev;
723         struct drm_psb_private *dev_priv = dev->dev_private;
724         struct psb_fbdev *fbdev = dev_priv->fbdev;
725         struct drm_crtc *crtc;
726         int reset = 0;
727
728         /* Should never get stolen memory for a user fb */
729         WARN_ON(r->stolen);
730
731         /* Check if we are erroneously live */
732         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
733                 if (crtc->fb == fb)
734                         reset = 1;
735
736         if (reset)
737                 /* 
738                  * Now force a sane response before we permit the DRM crc layer to
739                  * do stupid things like blank the display. Instead we reset this
740                  * framebuffer as if the user had forced a reset. We must do this
741                  * before the cleanup so that the DRM layer doesn't get a chance
742                  * to stick its oar in where it isn't wanted.
743                  */
744                 drm_fb_helper_restore_fbdev_mode(&fbdev->psb_fb_helper);
745
746         /* Let DRM do its clean up */
747         drm_framebuffer_cleanup(fb);
748         /*  We are no longer using the resource in GEM */
749         drm_gem_object_unreference_unlocked(&r->gem);
750         kfree(fb);
751 }
752
753 static const struct drm_mode_config_funcs psb_mode_funcs = {
754         .fb_create = psb_user_framebuffer_create,
755         .output_poll_changed = psbfb_output_poll_changed,
756 };
757
758 static int psb_create_backlight_property(struct drm_device *dev)
759 {
760         struct drm_psb_private *dev_priv
761                                 = (struct drm_psb_private *) dev->dev_private;
762         struct drm_property *backlight;
763
764         if (dev_priv->backlight_property)
765                 return 0;
766
767         backlight = drm_property_create(dev,
768                                         DRM_MODE_PROP_RANGE,
769                                         "backlight",
770                                         2);
771         backlight->values[0] = 0;
772         backlight->values[1] = 100;
773
774         dev_priv->backlight_property = backlight;
775
776         return 0;
777 }
778
779 static void psb_setup_outputs(struct drm_device *dev)
780 {
781         struct drm_psb_private *dev_priv =
782             (struct drm_psb_private *) dev->dev_private;
783         struct drm_connector *connector;
784
785         PSB_DEBUG_ENTRY("\n");
786
787         drm_mode_create_scaling_mode_property(dev);
788
789         psb_create_backlight_property(dev);
790
791         if (IS_MRST(dev)) {
792                 if (dev_priv->iLVDS_enable)
793                         mrst_lvds_init(dev, &dev_priv->mode_dev);
794                 else
795                         DRM_ERROR("DSI is not supported\n");
796         } else {
797                 psb_intel_lvds_init(dev, &dev_priv->mode_dev);
798                 psb_intel_sdvo_init(dev, SDVOB);
799         }
800
801         list_for_each_entry(connector, &dev->mode_config.connector_list,
802                             head) {
803                 struct psb_intel_output *psb_intel_output =
804                     to_psb_intel_output(connector);
805                 struct drm_encoder *encoder = &psb_intel_output->enc;
806                 int crtc_mask = 0, clone_mask = 0;
807
808                 /* valid crtcs */
809                 switch (psb_intel_output->type) {
810                 case INTEL_OUTPUT_SDVO:
811                         crtc_mask = ((1 << 0) | (1 << 1));
812                         clone_mask = (1 << INTEL_OUTPUT_SDVO);
813                         break;
814                 case INTEL_OUTPUT_LVDS:
815                         PSB_DEBUG_ENTRY("LVDS.\n");
816                         if (IS_MRST(dev))
817                                 crtc_mask = (1 << 0);
818                         else
819                                 crtc_mask = (1 << 1);
820                         clone_mask = (1 << INTEL_OUTPUT_LVDS);
821                         break;
822                 case INTEL_OUTPUT_MIPI:
823                         PSB_DEBUG_ENTRY("MIPI.\n");
824                         crtc_mask = (1 << 0);
825                         clone_mask = (1 << INTEL_OUTPUT_MIPI);
826                         break;
827                 case INTEL_OUTPUT_MIPI2:
828                         PSB_DEBUG_ENTRY("MIPI2.\n");
829                         crtc_mask = (1 << 2);
830                         clone_mask = (1 << INTEL_OUTPUT_MIPI2);
831                         break;
832                 case INTEL_OUTPUT_HDMI:
833                         PSB_DEBUG_ENTRY("HDMI.\n");
834                         crtc_mask = (1 << 1);
835                         clone_mask = (1 << INTEL_OUTPUT_HDMI);
836                         break;
837                 }
838
839                 encoder->possible_crtcs = crtc_mask;
840                 encoder->possible_clones =
841                     psb_intel_connector_clones(dev, clone_mask);
842
843         }
844 }
845
846 void psb_modeset_init(struct drm_device *dev)
847 {
848         struct drm_psb_private *dev_priv =
849             (struct drm_psb_private *) dev->dev_private;
850         struct psb_intel_mode_device *mode_dev = &dev_priv->mode_dev;
851         int i;
852
853         PSB_DEBUG_ENTRY("\n");
854
855         drm_mode_config_init(dev);
856
857         dev->mode_config.min_width = 0;
858         dev->mode_config.min_height = 0;
859
860         dev->mode_config.funcs = (void *) &psb_mode_funcs;
861
862         /* set memory base */
863         /* MRST and PSB should use BAR 2*/
864         pci_read_config_dword(dev->pdev, PSB_BSM, (u32 *)
865                                         &(dev->mode_config.fb_base));
866
867         /* num pipes is 2 for PSB but 1 for Mrst */
868         for (i = 0; i < dev_priv->num_pipe; i++)
869                 psb_intel_crtc_init(dev, i, mode_dev);
870
871         dev->mode_config.max_width = 2048;
872         dev->mode_config.max_height = 2048;
873
874         psb_setup_outputs(dev);
875
876         /* setup fbs */
877         /* drm_initial_config(dev); */
878 }
879
880 void psb_modeset_cleanup(struct drm_device *dev)
881 {
882         mutex_lock(&dev->struct_mutex);
883
884         drm_kms_helper_poll_fini(dev);
885         psb_fbdev_fini(dev);
886
887         drm_mode_config_cleanup(dev);
888
889         mutex_unlock(&dev->struct_mutex);
890 }