830dfdd6bf154a473e15357654cf4d19be8d6b81
[linux-2.6.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_sync = psbfb_sync,
251         .fb_ioctl = psbfb_ioctl,
252 };
253
254 static struct fb_ops psbfb_unaccel_ops = {
255         .owner = THIS_MODULE,
256         .fb_check_var = drm_fb_helper_check_var,
257         .fb_set_par = drm_fb_helper_set_par,
258         .fb_blank = drm_fb_helper_blank,
259         .fb_setcolreg = psbfb_setcolreg,
260         .fb_fillrect = cfb_fillrect,
261         .fb_copyarea = cfb_copyarea,
262         .fb_imageblit = cfb_imageblit,
263         .fb_mmap = psbfb_mmap,
264         .fb_ioctl = psbfb_ioctl,
265 };
266
267 /**
268  *      psb_framebuffer_init    -       initialize a framebuffer
269  *      @dev: our DRM device
270  *      @fb: framebuffer to set up
271  *      @mode_cmd: mode description
272  *      @gt: backing object
273  *
274  *      Configure and fill in the boilerplate for our frame buffer. Return
275  *      0 on success or an error code if we fail.
276  */
277 static int psb_framebuffer_init(struct drm_device *dev,
278                                         struct psb_framebuffer *fb,
279                                         struct drm_mode_fb_cmd2 *mode_cmd,
280                                         struct gtt_range *gt)
281 {
282         u32 bpp, depth;
283         int ret;
284
285         drm_fb_get_bpp_depth(mode_cmd->pixel_format, &depth, &bpp);
286
287         if (mode_cmd->pitches[0] & 63)
288                 return -EINVAL;
289         switch (bpp) {
290         case 8:
291         case 16:
292         case 24:
293         case 32:
294                 break;
295         default:
296                 return -EINVAL;
297         }
298         ret = drm_framebuffer_init(dev, &fb->base, &psb_fb_funcs);
299         if (ret) {
300                 dev_err(dev->dev, "framebuffer init failed: %d\n", ret);
301                 return ret;
302         }
303         drm_helper_mode_fill_fb_struct(&fb->base, mode_cmd);
304         fb->gtt = gt;
305         return 0;
306 }
307
308 /**
309  *      psb_framebuffer_create  -       create a framebuffer backed by gt
310  *      @dev: our DRM device
311  *      @mode_cmd: the description of the requested mode
312  *      @gt: the backing object
313  *
314  *      Create a framebuffer object backed by the gt, and fill in the
315  *      boilerplate required
316  *
317  *      TODO: review object references
318  */
319
320 static struct drm_framebuffer *psb_framebuffer_create
321                         (struct drm_device *dev,
322                          struct drm_mode_fb_cmd2 *mode_cmd,
323                          struct gtt_range *gt)
324 {
325         struct psb_framebuffer *fb;
326         int ret;
327
328         fb = kzalloc(sizeof(*fb), GFP_KERNEL);
329         if (!fb)
330                 return ERR_PTR(-ENOMEM);
331
332         ret = psb_framebuffer_init(dev, fb, mode_cmd, gt);
333         if (ret) {
334                 kfree(fb);
335                 return ERR_PTR(ret);
336         }
337         return &fb->base;
338 }
339
340 /**
341  *      psbfb_alloc             -       allocate frame buffer memory
342  *      @dev: the DRM device
343  *      @aligned_size: space needed
344  *      @force: fall back to GEM buffers if need be
345  *
346  *      Allocate the frame buffer. In the usual case we get a GTT range that
347  *      is stolen memory backed and life is simple. If there isn't sufficient
348  *      we fail as we don't have the virtual mapping space to really vmap it
349  *      and the kernel console code can't handle non linear framebuffers.
350  *
351  *      Re-address this as and if the framebuffer layer grows this ability.
352  */
353 static struct gtt_range *psbfb_alloc(struct drm_device *dev, int aligned_size)
354 {
355         struct gtt_range *backing;
356         /* Begin by trying to use stolen memory backing */
357         backing = psb_gtt_alloc_range(dev, aligned_size, "fb", 1);
358         if (backing) {
359                 if (drm_gem_private_object_init(dev,
360                                         &backing->gem, aligned_size) == 0)
361                         return backing;
362                 psb_gtt_free_range(dev, backing);
363         }
364         return NULL;
365 }
366
367 /**
368  *      psbfb_create            -       create a framebuffer
369  *      @fbdev: the framebuffer device
370  *      @sizes: specification of the layout
371  *
372  *      Create a framebuffer to the specifications provided
373  */
374 static int psbfb_create(struct psb_fbdev *fbdev,
375                                 struct drm_fb_helper_surface_size *sizes)
376 {
377         struct drm_device *dev = fbdev->psb_fb_helper.dev;
378         struct drm_psb_private *dev_priv = dev->dev_private;
379         struct fb_info *info;
380         struct drm_framebuffer *fb;
381         struct psb_framebuffer *psbfb = &fbdev->pfb;
382         struct drm_mode_fb_cmd2 mode_cmd;
383         struct device *device = &dev->pdev->dev;
384         int size;
385         int ret;
386         struct gtt_range *backing;
387         u32 bpp, depth;
388         int gtt_roll = 0;
389         int pitch_lines = 0;
390
391         mode_cmd.width = sizes->surface_width;
392         mode_cmd.height = sizes->surface_height;
393         bpp = sizes->surface_bpp;
394
395         /* No 24bit packed */
396         if (bpp == 24)
397                 bpp = 32;
398
399         do {
400                 /*
401                  * Acceleration via the GTT requires pitch to be
402                  * power of two aligned. Preferably page but less
403                  * is ok with some fonts
404                  */
405                 mode_cmd.pitches[0] =  ALIGN(mode_cmd.width * ((bpp + 7) / 8), 4096 >> pitch_lines);
406                 depth = sizes->surface_depth;
407
408                 size = mode_cmd.pitches[0] * mode_cmd.height;
409                 size = ALIGN(size, PAGE_SIZE);
410
411                 /* Allocate the fb in the GTT with stolen page backing */
412                 backing = psbfb_alloc(dev, size);
413
414                 if (pitch_lines)
415                         pitch_lines *= 2;
416                 else
417                         pitch_lines = 1;
418                 gtt_roll++;
419         } while (backing == NULL && pitch_lines <= 16);
420
421         /* The final pitch we accepted if we succeeded */
422         pitch_lines /= 2;
423
424         if (backing == NULL) {
425                 /*
426                  *      We couldn't get the space we wanted, fall back to the
427                  *      display engine requirement instead.  The HW requires
428                  *      the pitch to be 64 byte aligned
429                  */
430
431                 gtt_roll = 0;   /* Don't use GTT accelerated scrolling */
432                 pitch_lines = 64;
433
434                 mode_cmd.pitches[0] =  ALIGN(mode_cmd.width * ((bpp + 7) / 8), 64);
435
436                 size = mode_cmd.pitches[0] * mode_cmd.height;
437                 size = ALIGN(size, PAGE_SIZE);
438
439                 /* Allocate the framebuffer in the GTT with stolen page backing */
440                 backing = psbfb_alloc(dev, size);
441                 if (backing == NULL)
442                         return -ENOMEM;
443         }
444
445         mutex_lock(&dev->struct_mutex);
446
447         info = framebuffer_alloc(0, device);
448         if (!info) {
449                 ret = -ENOMEM;
450                 goto out_err1;
451         }
452         info->par = fbdev;
453
454         mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
455
456         ret = psb_framebuffer_init(dev, psbfb, &mode_cmd, backing);
457         if (ret)
458                 goto out_unref;
459
460         fb = &psbfb->base;
461         psbfb->fbdev = info;
462
463         fbdev->psb_fb_helper.fb = fb;
464         fbdev->psb_fb_helper.fbdev = info;
465
466         strcpy(info->fix.id, "psbfb");
467
468         info->flags = FBINFO_DEFAULT;
469         if (dev_priv->ops->accel_2d && pitch_lines > 8) /* 2D engine */
470                 info->fbops = &psbfb_ops;
471         else if (gtt_roll) {    /* GTT rolling seems best */
472                 info->fbops = &psbfb_roll_ops;
473                 info->flags |= FBINFO_HWACCEL_YPAN;
474         } else  /* Software */
475                 info->fbops = &psbfb_unaccel_ops;
476
477         ret = fb_alloc_cmap(&info->cmap, 256, 0);
478         if (ret) {
479                 ret = -ENOMEM;
480                 goto out_unref;
481         }
482
483         info->fix.smem_start = dev->mode_config.fb_base;
484         info->fix.smem_len = size;
485         info->fix.ywrapstep = gtt_roll;
486         info->fix.ypanstep = 0;
487
488         /* Accessed stolen memory directly */
489         info->screen_base = (char *)dev_priv->vram_addr +
490                                                         backing->offset;
491         info->screen_size = size;
492
493         if (dev_priv->gtt.stolen_size) {
494                 info->apertures = alloc_apertures(1);
495                 if (!info->apertures) {
496                         ret = -ENOMEM;
497                         goto out_unref;
498                 }
499                 info->apertures->ranges[0].base = dev->mode_config.fb_base;
500                 info->apertures->ranges[0].size = dev_priv->gtt.stolen_size;
501         }
502
503         drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
504         drm_fb_helper_fill_var(info, &fbdev->psb_fb_helper,
505                                 sizes->fb_width, sizes->fb_height);
506
507         info->fix.mmio_start = pci_resource_start(dev->pdev, 0);
508         info->fix.mmio_len = pci_resource_len(dev->pdev, 0);
509
510         info->pixmap.size = 64 * 1024;
511         info->pixmap.buf_align = 8;
512         info->pixmap.access_align = 32;
513         info->pixmap.flags = FB_PIXMAP_SYSTEM;
514         info->pixmap.scan_align = 1;
515
516         dev_info(dev->dev, "allocated %dx%d fb\n",
517                                         psbfb->base.width, psbfb->base.height);
518
519         mutex_unlock(&dev->struct_mutex);
520         return 0;
521 out_unref:
522         if (backing->stolen)
523                 psb_gtt_free_range(dev, backing);
524         else
525                 drm_gem_object_unreference(&backing->gem);
526 out_err1:
527         mutex_unlock(&dev->struct_mutex);
528         psb_gtt_free_range(dev, backing);
529         return ret;
530 }
531
532 /**
533  *      psb_user_framebuffer_create     -       create framebuffer
534  *      @dev: our DRM device
535  *      @filp: client file
536  *      @cmd: mode request
537  *
538  *      Create a new framebuffer backed by a userspace GEM object
539  */
540 static struct drm_framebuffer *psb_user_framebuffer_create
541                         (struct drm_device *dev, struct drm_file *filp,
542                          struct drm_mode_fb_cmd2 *cmd)
543 {
544         struct gtt_range *r;
545         struct drm_gem_object *obj;
546
547         /*
548          *      Find the GEM object and thus the gtt range object that is
549          *      to back this space
550          */
551         obj = drm_gem_object_lookup(dev, filp, cmd->handles[0]);
552         if (obj == NULL)
553                 return ERR_PTR(-ENOENT);
554
555         /* Let the core code do all the work */
556         r = container_of(obj, struct gtt_range, gem);
557         return psb_framebuffer_create(dev, cmd, r);
558 }
559
560 static void psbfb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
561                                                         u16 blue, int regno)
562 {
563 }
564
565 static void psbfb_gamma_get(struct drm_crtc *crtc, u16 *red,
566                                         u16 *green, u16 *blue, int regno)
567 {
568 }
569
570 static int psbfb_probe(struct drm_fb_helper *helper,
571                                 struct drm_fb_helper_surface_size *sizes)
572 {
573         struct psb_fbdev *psb_fbdev = (struct psb_fbdev *)helper;
574         int new_fb = 0;
575         int ret;
576
577         if (!helper->fb) {
578                 ret = psbfb_create(psb_fbdev, sizes);
579                 if (ret)
580                         return ret;
581                 new_fb = 1;
582         }
583         return new_fb;
584 }
585
586 struct drm_fb_helper_funcs psb_fb_helper_funcs = {
587         .gamma_set = psbfb_gamma_set,
588         .gamma_get = psbfb_gamma_get,
589         .fb_probe = psbfb_probe,
590 };
591
592 int psb_fbdev_destroy(struct drm_device *dev, struct psb_fbdev *fbdev)
593 {
594         struct fb_info *info;
595         struct psb_framebuffer *psbfb = &fbdev->pfb;
596
597         if (fbdev->psb_fb_helper.fbdev) {
598                 info = fbdev->psb_fb_helper.fbdev;
599                 unregister_framebuffer(info);
600                 if (info->cmap.len)
601                         fb_dealloc_cmap(&info->cmap);
602                 framebuffer_release(info);
603         }
604         drm_fb_helper_fini(&fbdev->psb_fb_helper);
605         drm_framebuffer_cleanup(&psbfb->base);
606
607         if (psbfb->gtt)
608                 drm_gem_object_unreference(&psbfb->gtt->gem);
609         return 0;
610 }
611
612 int psb_fbdev_init(struct drm_device *dev)
613 {
614         struct psb_fbdev *fbdev;
615         struct drm_psb_private *dev_priv = dev->dev_private;
616
617         fbdev = kzalloc(sizeof(struct psb_fbdev), GFP_KERNEL);
618         if (!fbdev) {
619                 dev_err(dev->dev, "no memory\n");
620                 return -ENOMEM;
621         }
622
623         dev_priv->fbdev = fbdev;
624         fbdev->psb_fb_helper.funcs = &psb_fb_helper_funcs;
625
626         drm_fb_helper_init(dev, &fbdev->psb_fb_helper, dev_priv->ops->crtcs,
627                                                         INTELFB_CONN_LIMIT);
628
629         drm_fb_helper_single_add_all_connectors(&fbdev->psb_fb_helper);
630         drm_fb_helper_initial_config(&fbdev->psb_fb_helper, 32);
631         return 0;
632 }
633
634 void psb_fbdev_fini(struct drm_device *dev)
635 {
636         struct drm_psb_private *dev_priv = dev->dev_private;
637
638         if (!dev_priv->fbdev)
639                 return;
640
641         psb_fbdev_destroy(dev, dev_priv->fbdev);
642         kfree(dev_priv->fbdev);
643         dev_priv->fbdev = NULL;
644 }
645
646 static void psbfb_output_poll_changed(struct drm_device *dev)
647 {
648         struct drm_psb_private *dev_priv = dev->dev_private;
649         struct psb_fbdev *fbdev = (struct psb_fbdev *)dev_priv->fbdev;
650         drm_fb_helper_hotplug_event(&fbdev->psb_fb_helper);
651 }
652
653 /**
654  *      psb_user_framebuffer_create_handle - add hamdle to a framebuffer
655  *      @fb: framebuffer
656  *      @file_priv: our DRM file
657  *      @handle: returned handle
658  *
659  *      Our framebuffer object is a GTT range which also contains a GEM
660  *      object. We need to turn it into a handle for userspace. GEM will do
661  *      the work for us
662  */
663 static int psb_user_framebuffer_create_handle(struct drm_framebuffer *fb,
664                                               struct drm_file *file_priv,
665                                               unsigned int *handle)
666 {
667         struct psb_framebuffer *psbfb = to_psb_fb(fb);
668         struct gtt_range *r = psbfb->gtt;
669         return drm_gem_handle_create(file_priv, &r->gem, handle);
670 }
671
672 /**
673  *      psb_user_framebuffer_destroy    -       destruct user created fb
674  *      @fb: framebuffer
675  *
676  *      User framebuffers are backed by GEM objects so all we have to do is
677  *      clean up a bit and drop the reference, GEM will handle the fallout
678  */
679 static void psb_user_framebuffer_destroy(struct drm_framebuffer *fb)
680 {
681         struct psb_framebuffer *psbfb = to_psb_fb(fb);
682         struct gtt_range *r = psbfb->gtt;
683         struct drm_device *dev = fb->dev;
684         struct drm_psb_private *dev_priv = dev->dev_private;
685         struct psb_fbdev *fbdev = dev_priv->fbdev;
686         struct drm_crtc *crtc;
687         int reset = 0;
688
689         /* Should never get stolen memory for a user fb */
690         WARN_ON(r->stolen);
691
692         /* Check if we are erroneously live */
693         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
694                 if (crtc->fb == fb)
695                         reset = 1;
696
697         if (reset)
698                 /*
699                  * Now force a sane response before we permit the DRM CRTC
700                  * layer to do stupid things like blank the display. Instead
701                  * we reset this framebuffer as if the user had forced a reset.
702                  * We must do this before the cleanup so that the DRM layer
703                  * doesn't get a chance to stick its oar in where it isn't
704                  * wanted.
705                  */
706                 drm_fb_helper_restore_fbdev_mode(&fbdev->psb_fb_helper);
707
708         /* Let DRM do its clean up */
709         drm_framebuffer_cleanup(fb);
710         /*  We are no longer using the resource in GEM */
711         drm_gem_object_unreference_unlocked(&r->gem);
712         kfree(fb);
713 }
714
715 static const struct drm_mode_config_funcs psb_mode_funcs = {
716         .fb_create = psb_user_framebuffer_create,
717         .output_poll_changed = psbfb_output_poll_changed,
718 };
719
720 static int psb_create_backlight_property(struct drm_device *dev)
721 {
722         struct drm_psb_private *dev_priv = dev->dev_private;
723         struct drm_property *backlight;
724
725         if (dev_priv->backlight_property)
726                 return 0;
727
728         backlight = drm_property_create(dev, DRM_MODE_PROP_RANGE,
729                                                         "backlight", 2);
730         backlight->values[0] = 0;
731         backlight->values[1] = 100;
732
733         dev_priv->backlight_property = backlight;
734
735         return 0;
736 }
737
738 static void psb_setup_outputs(struct drm_device *dev)
739 {
740         struct drm_psb_private *dev_priv = dev->dev_private;
741         struct drm_connector *connector;
742
743         drm_mode_create_scaling_mode_property(dev);
744         psb_create_backlight_property(dev);
745
746         dev_priv->ops->output_init(dev);
747
748         list_for_each_entry(connector, &dev->mode_config.connector_list,
749                             head) {
750                 struct psb_intel_encoder *psb_intel_encoder =
751                         psb_intel_attached_encoder(connector);
752                 struct drm_encoder *encoder = &psb_intel_encoder->base;
753                 int crtc_mask = 0, clone_mask = 0;
754
755                 /* valid crtcs */
756                 switch (psb_intel_encoder->type) {
757                 case INTEL_OUTPUT_ANALOG:
758                         crtc_mask = (1 << 0);
759                         clone_mask = (1 << INTEL_OUTPUT_ANALOG);
760                         break;
761                 case INTEL_OUTPUT_SDVO:
762                         crtc_mask = ((1 << 0) | (1 << 1));
763                         clone_mask = (1 << INTEL_OUTPUT_SDVO);
764                         break;
765                 case INTEL_OUTPUT_LVDS:
766                         if (IS_MRST(dev))
767                                 crtc_mask = (1 << 0);
768                         else
769                                 crtc_mask = (1 << 1);
770                         clone_mask = (1 << INTEL_OUTPUT_LVDS);
771                         break;
772                 case INTEL_OUTPUT_MIPI:
773                         crtc_mask = (1 << 0);
774                         clone_mask = (1 << INTEL_OUTPUT_MIPI);
775                         break;
776                 case INTEL_OUTPUT_MIPI2:
777                         crtc_mask = (1 << 2);
778                         clone_mask = (1 << INTEL_OUTPUT_MIPI2);
779                         break;
780                 case INTEL_OUTPUT_HDMI:
781                         if (IS_MFLD(dev))
782                                 crtc_mask = (1 << 1);
783                         else    
784                                 crtc_mask = (1 << 0);
785                         clone_mask = (1 << INTEL_OUTPUT_HDMI);
786                         break;
787                 }
788                 encoder->possible_crtcs = crtc_mask;
789                 encoder->possible_clones =
790                     psb_intel_connector_clones(dev, clone_mask);
791         }
792 }
793
794 void psb_modeset_init(struct drm_device *dev)
795 {
796         struct drm_psb_private *dev_priv = dev->dev_private;
797         struct psb_intel_mode_device *mode_dev = &dev_priv->mode_dev;
798         int i;
799
800         drm_mode_config_init(dev);
801
802         dev->mode_config.min_width = 0;
803         dev->mode_config.min_height = 0;
804
805         dev->mode_config.funcs = (void *) &psb_mode_funcs;
806
807         /* set memory base */
808         /* Oaktrail and Poulsbo should use BAR 2*/
809         pci_read_config_dword(dev->pdev, PSB_BSM, (u32 *)
810                                         &(dev->mode_config.fb_base));
811
812         /* num pipes is 2 for PSB but 1 for Mrst */
813         for (i = 0; i < dev_priv->num_pipe; i++)
814                 psb_intel_crtc_init(dev, i, mode_dev);
815
816         dev->mode_config.max_width = 2048;
817         dev->mode_config.max_height = 2048;
818
819         psb_setup_outputs(dev);
820 }
821
822 void psb_modeset_cleanup(struct drm_device *dev)
823 {
824         mutex_lock(&dev->struct_mutex);
825
826         drm_kms_helper_poll_fini(dev);
827         psb_fbdev_fini(dev);
828         drm_mode_config_cleanup(dev);
829
830         mutex_unlock(&dev->struct_mutex);
831 }