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