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