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