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