14d8d096eSAlan Cox /**************************************************************************
24d8d096eSAlan Cox  * Copyright (c) 2007-2011, Intel Corporation.
34d8d096eSAlan Cox  * All Rights Reserved.
44d8d096eSAlan Cox  *
54d8d096eSAlan Cox  * This program is free software; you can redistribute it and/or modify it
64d8d096eSAlan Cox  * under the terms and conditions of the GNU General Public License,
74d8d096eSAlan Cox  * version 2, as published by the Free Software Foundation.
84d8d096eSAlan Cox  *
94d8d096eSAlan Cox  * This program is distributed in the hope it will be useful, but WITHOUT
104d8d096eSAlan Cox  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
114d8d096eSAlan Cox  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
124d8d096eSAlan Cox  * more details.
134d8d096eSAlan Cox  *
144d8d096eSAlan Cox  * You should have received a copy of the GNU General Public License along with
154d8d096eSAlan Cox  * this program; if not, write to the Free Software Foundation, Inc.,
164d8d096eSAlan Cox  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
174d8d096eSAlan Cox  *
184d8d096eSAlan Cox  **************************************************************************/
194d8d096eSAlan Cox 
204d8d096eSAlan Cox #include <linux/module.h>
214d8d096eSAlan Cox #include <linux/kernel.h>
224d8d096eSAlan Cox #include <linux/errno.h>
234d8d096eSAlan Cox #include <linux/string.h>
2401c8f1c4SDan Williams #include <linux/pfn_t.h>
254d8d096eSAlan Cox #include <linux/mm.h>
264d8d096eSAlan Cox #include <linux/tty.h>
274d8d096eSAlan Cox #include <linux/slab.h>
284d8d096eSAlan Cox #include <linux/delay.h>
294d8d096eSAlan Cox #include <linux/fb.h>
304d8d096eSAlan Cox #include <linux/init.h>
314d8d096eSAlan Cox #include <linux/console.h>
324d8d096eSAlan Cox 
334d8d096eSAlan Cox #include <drm/drmP.h>
344d8d096eSAlan Cox #include <drm/drm.h>
354d8d096eSAlan Cox #include <drm/drm_crtc.h>
36a9a644acSDave Airlie #include <drm/drm_fb_helper.h>
374d8d096eSAlan Cox 
384d8d096eSAlan Cox #include "psb_drv.h"
394d8d096eSAlan Cox #include "psb_intel_reg.h"
404d8d096eSAlan Cox #include "psb_intel_drv.h"
414d8d096eSAlan Cox #include "framebuffer.h"
42a6ba582dSAlan Cox #include "gtt.h"
434d8d096eSAlan Cox 
444d8d096eSAlan Cox static void psb_user_framebuffer_destroy(struct drm_framebuffer *fb);
454d8d096eSAlan Cox static int psb_user_framebuffer_create_handle(struct drm_framebuffer *fb,
464d8d096eSAlan Cox 					      struct drm_file *file_priv,
474d8d096eSAlan Cox 					      unsigned int *handle);
484d8d096eSAlan Cox 
494d8d096eSAlan Cox static const struct drm_framebuffer_funcs psb_fb_funcs = {
504d8d096eSAlan Cox 	.destroy = psb_user_framebuffer_destroy,
514d8d096eSAlan Cox 	.create_handle = psb_user_framebuffer_create_handle,
524d8d096eSAlan Cox };
534d8d096eSAlan Cox 
544d8d096eSAlan Cox #define CMAP_TOHW(_val, _width) ((((_val) << (_width)) + 0x7FFF - (_val)) >> 16)
554d8d096eSAlan Cox 
564d8d096eSAlan Cox static int psbfb_setcolreg(unsigned regno, unsigned red, unsigned green,
574d8d096eSAlan Cox 			   unsigned blue, unsigned transp,
584d8d096eSAlan Cox 			   struct fb_info *info)
594d8d096eSAlan Cox {
604d8d096eSAlan Cox 	struct psb_fbdev *fbdev = info->par;
614d8d096eSAlan Cox 	struct drm_framebuffer *fb = fbdev->psb_fb_helper.fb;
624d8d096eSAlan Cox 	uint32_t v;
634d8d096eSAlan Cox 
644d8d096eSAlan Cox 	if (!fb)
654d8d096eSAlan Cox 		return -ENOMEM;
664d8d096eSAlan Cox 
674d8d096eSAlan Cox 	if (regno > 255)
684d8d096eSAlan Cox 		return 1;
694d8d096eSAlan Cox 
704d8d096eSAlan Cox 	red = CMAP_TOHW(red, info->var.red.length);
714d8d096eSAlan Cox 	blue = CMAP_TOHW(blue, info->var.blue.length);
724d8d096eSAlan Cox 	green = CMAP_TOHW(green, info->var.green.length);
734d8d096eSAlan Cox 	transp = CMAP_TOHW(transp, info->var.transp.length);
744d8d096eSAlan Cox 
754d8d096eSAlan Cox 	v = (red << info->var.red.offset) |
764d8d096eSAlan Cox 	    (green << info->var.green.offset) |
774d8d096eSAlan Cox 	    (blue << info->var.blue.offset) |
784d8d096eSAlan Cox 	    (transp << info->var.transp.offset);
794d8d096eSAlan Cox 
804d8d096eSAlan Cox 	if (regno < 16) {
814d8d096eSAlan Cox 		switch (fb->bits_per_pixel) {
824d8d096eSAlan Cox 		case 16:
834d8d096eSAlan Cox 			((uint32_t *) info->pseudo_palette)[regno] = v;
844d8d096eSAlan Cox 			break;
854d8d096eSAlan Cox 		case 24:
864d8d096eSAlan Cox 		case 32:
874d8d096eSAlan Cox 			((uint32_t *) info->pseudo_palette)[regno] = v;
884d8d096eSAlan Cox 			break;
894d8d096eSAlan Cox 		}
904d8d096eSAlan Cox 	}
914d8d096eSAlan Cox 
924d8d096eSAlan Cox 	return 0;
934d8d096eSAlan Cox }
944d8d096eSAlan Cox 
95a6ba582dSAlan Cox static int psbfb_pan(struct fb_var_screeninfo *var, struct fb_info *info)
96a6ba582dSAlan Cox {
97a6ba582dSAlan Cox 	struct psb_fbdev *fbdev = info->par;
98a6ba582dSAlan Cox 	struct psb_framebuffer *psbfb = &fbdev->pfb;
99a6ba582dSAlan Cox 	struct drm_device *dev = psbfb->base.dev;
100a6ba582dSAlan Cox 
101a6ba582dSAlan Cox 	/*
102a6ba582dSAlan Cox 	 *	We have to poke our nose in here. The core fb code assumes
103a6ba582dSAlan Cox 	 *	panning is part of the hardware that can be invoked before
104a6ba582dSAlan Cox 	 *	the actual fb is mapped. In our case that isn't quite true.
105a6ba582dSAlan Cox 	 */
106a6ba582dSAlan Cox 	if (psbfb->gtt->npage) {
107a6ba582dSAlan Cox 		/* GTT roll shifts in 4K pages, we need to shift the right
108a6ba582dSAlan Cox 		   number of pages */
109a6ba582dSAlan Cox 		int pages = info->fix.line_length >> 12;
110a6ba582dSAlan Cox 		psb_gtt_roll(dev, psbfb->gtt, var->yoffset * pages);
111a6ba582dSAlan Cox 	}
112a6ba582dSAlan Cox         return 0;
113a6ba582dSAlan Cox }
1144d8d096eSAlan Cox 
1154d8d096eSAlan Cox static int psbfb_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1164d8d096eSAlan Cox {
1174d8d096eSAlan Cox 	struct psb_framebuffer *psbfb = vma->vm_private_data;
1184d8d096eSAlan Cox 	struct drm_device *dev = psbfb->base.dev;
1194d8d096eSAlan Cox 	struct drm_psb_private *dev_priv = dev->dev_private;
1204d8d096eSAlan Cox 	int page_num;
1214d8d096eSAlan Cox 	int i;
1224d8d096eSAlan Cox 	unsigned long address;
1234d8d096eSAlan Cox 	int ret;
1244d8d096eSAlan Cox 	unsigned long pfn;
12561bb3feaSPatrik Jakobsson 	unsigned long phys_addr = (unsigned long)dev_priv->stolen_base +
12661bb3feaSPatrik Jakobsson 				  psbfb->gtt->offset;
1274d8d096eSAlan Cox 
1284d8d096eSAlan Cox 	page_num = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
1291278f7deSYoichi Yuasa 	address = (unsigned long)vmf->virtual_address - (vmf->pgoff << PAGE_SHIFT);
1304d8d096eSAlan Cox 
1314d8d096eSAlan Cox 	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1324d8d096eSAlan Cox 
1334d8d096eSAlan Cox 	for (i = 0; i < page_num; i++) {
1344d8d096eSAlan Cox 		pfn = (phys_addr >> PAGE_SHIFT);
1354d8d096eSAlan Cox 
13601c8f1c4SDan Williams 		ret = vm_insert_mixed(vma, address,
13701c8f1c4SDan Williams 				__pfn_to_pfn_t(pfn, PFN_DEV));
1384d8d096eSAlan Cox 		if (unlikely((ret == -EBUSY) || (ret != 0 && i > 0)))
1394d8d096eSAlan Cox 			break;
1404d8d096eSAlan Cox 		else if (unlikely(ret != 0)) {
1414d8d096eSAlan Cox 			ret = (ret == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS;
1424d8d096eSAlan Cox 			return ret;
1434d8d096eSAlan Cox 		}
1444d8d096eSAlan Cox 		address += PAGE_SIZE;
1454d8d096eSAlan Cox 		phys_addr += PAGE_SIZE;
1464d8d096eSAlan Cox 	}
1474d8d096eSAlan Cox 	return VM_FAULT_NOPAGE;
1484d8d096eSAlan Cox }
1494d8d096eSAlan Cox 
1504d8d096eSAlan Cox static void psbfb_vm_open(struct vm_area_struct *vma)
1514d8d096eSAlan Cox {
1524d8d096eSAlan Cox }
1534d8d096eSAlan Cox 
1544d8d096eSAlan Cox static void psbfb_vm_close(struct vm_area_struct *vma)
1554d8d096eSAlan Cox {
1564d8d096eSAlan Cox }
1574d8d096eSAlan Cox 
15878b68556SLaurent Pinchart static const struct vm_operations_struct psbfb_vm_ops = {
1594d8d096eSAlan Cox 	.fault	= psbfb_vm_fault,
1604d8d096eSAlan Cox 	.open	= psbfb_vm_open,
1614d8d096eSAlan Cox 	.close	= psbfb_vm_close
1624d8d096eSAlan Cox };
1634d8d096eSAlan Cox 
1644d8d096eSAlan Cox static int psbfb_mmap(struct fb_info *info, struct vm_area_struct *vma)
1654d8d096eSAlan Cox {
1664d8d096eSAlan Cox 	struct psb_fbdev *fbdev = info->par;
1674d8d096eSAlan Cox 	struct psb_framebuffer *psbfb = &fbdev->pfb;
1684d8d096eSAlan Cox 
1694d8d096eSAlan Cox 	if (vma->vm_pgoff != 0)
1704d8d096eSAlan Cox 		return -EINVAL;
1714d8d096eSAlan Cox 	if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
1724d8d096eSAlan Cox 		return -EINVAL;
1734d8d096eSAlan Cox 
1744d8d096eSAlan Cox 	if (!psbfb->addr_space)
1754d8d096eSAlan Cox 		psbfb->addr_space = vma->vm_file->f_mapping;
1764d8d096eSAlan Cox 	/*
1774d8d096eSAlan Cox 	 * If this is a GEM object then info->screen_base is the virtual
1784d8d096eSAlan Cox 	 * kernel remapping of the object. FIXME: Review if this is
1794d8d096eSAlan Cox 	 * suitable for our mmap work
1804d8d096eSAlan Cox 	 */
1814d8d096eSAlan Cox 	vma->vm_ops = &psbfb_vm_ops;
1824d8d096eSAlan Cox 	vma->vm_private_data = (void *)psbfb;
183314e51b9SKonstantin Khlebnikov 	vma->vm_flags |= VM_IO | VM_MIXEDMAP | VM_DONTEXPAND | VM_DONTDUMP;
1844d8d096eSAlan Cox 	return 0;
1854d8d096eSAlan Cox }
1864d8d096eSAlan Cox 
1874d8d096eSAlan Cox static int psbfb_ioctl(struct fb_info *info, unsigned int cmd,
1884d8d096eSAlan Cox 						unsigned long arg)
1894d8d096eSAlan Cox {
1904d8d096eSAlan Cox 	return -ENOTTY;
1914d8d096eSAlan Cox }
1924d8d096eSAlan Cox 
1934d8d096eSAlan Cox static struct fb_ops psbfb_ops = {
1944d8d096eSAlan Cox 	.owner = THIS_MODULE,
1954d8d096eSAlan Cox 	.fb_check_var = drm_fb_helper_check_var,
1964d8d096eSAlan Cox 	.fb_set_par = drm_fb_helper_set_par,
1974d8d096eSAlan Cox 	.fb_blank = drm_fb_helper_blank,
1984d8d096eSAlan Cox 	.fb_setcolreg = psbfb_setcolreg,
199546187c8SArchit Taneja 	.fb_fillrect = drm_fb_helper_cfb_fillrect,
2004d8d096eSAlan Cox 	.fb_copyarea = psbfb_copyarea,
201546187c8SArchit Taneja 	.fb_imageblit = drm_fb_helper_cfb_imageblit,
2024d8d096eSAlan Cox 	.fb_mmap = psbfb_mmap,
2034d8d096eSAlan Cox 	.fb_sync = psbfb_sync,
2044d8d096eSAlan Cox 	.fb_ioctl = psbfb_ioctl,
2054d8d096eSAlan Cox };
2064d8d096eSAlan Cox 
207a6ba582dSAlan Cox static struct fb_ops psbfb_roll_ops = {
208a6ba582dSAlan Cox 	.owner = THIS_MODULE,
209a6ba582dSAlan Cox 	.fb_check_var = drm_fb_helper_check_var,
210a6ba582dSAlan Cox 	.fb_set_par = drm_fb_helper_set_par,
211a6ba582dSAlan Cox 	.fb_blank = drm_fb_helper_blank,
212a6ba582dSAlan Cox 	.fb_setcolreg = psbfb_setcolreg,
213546187c8SArchit Taneja 	.fb_fillrect = drm_fb_helper_cfb_fillrect,
214546187c8SArchit Taneja 	.fb_copyarea = drm_fb_helper_cfb_copyarea,
215546187c8SArchit Taneja 	.fb_imageblit = drm_fb_helper_cfb_imageblit,
216a6ba582dSAlan Cox 	.fb_pan_display = psbfb_pan,
217a6ba582dSAlan Cox 	.fb_mmap = psbfb_mmap,
218a6ba582dSAlan Cox 	.fb_ioctl = psbfb_ioctl,
219a6ba582dSAlan Cox };
220a6ba582dSAlan Cox 
2214d8d096eSAlan Cox static struct fb_ops psbfb_unaccel_ops = {
2224d8d096eSAlan Cox 	.owner = THIS_MODULE,
2234d8d096eSAlan Cox 	.fb_check_var = drm_fb_helper_check_var,
2244d8d096eSAlan Cox 	.fb_set_par = drm_fb_helper_set_par,
2254d8d096eSAlan Cox 	.fb_blank = drm_fb_helper_blank,
2264d8d096eSAlan Cox 	.fb_setcolreg = psbfb_setcolreg,
227546187c8SArchit Taneja 	.fb_fillrect = drm_fb_helper_cfb_fillrect,
228546187c8SArchit Taneja 	.fb_copyarea = drm_fb_helper_cfb_copyarea,
229546187c8SArchit Taneja 	.fb_imageblit = drm_fb_helper_cfb_imageblit,
2304d8d096eSAlan Cox 	.fb_mmap = psbfb_mmap,
2314d8d096eSAlan Cox 	.fb_ioctl = psbfb_ioctl,
2324d8d096eSAlan Cox };
2334d8d096eSAlan Cox 
2344d8d096eSAlan Cox /**
2354d8d096eSAlan Cox  *	psb_framebuffer_init	-	initialize a framebuffer
2364d8d096eSAlan Cox  *	@dev: our DRM device
2374d8d096eSAlan Cox  *	@fb: framebuffer to set up
2384d8d096eSAlan Cox  *	@mode_cmd: mode description
2394d8d096eSAlan Cox  *	@gt: backing object
2404d8d096eSAlan Cox  *
2414d8d096eSAlan Cox  *	Configure and fill in the boilerplate for our frame buffer. Return
2424d8d096eSAlan Cox  *	0 on success or an error code if we fail.
2434d8d096eSAlan Cox  */
2444d8d096eSAlan Cox static int psb_framebuffer_init(struct drm_device *dev,
2454d8d096eSAlan Cox 					struct psb_framebuffer *fb,
2461eb83451SVille Syrjälä 					const struct drm_mode_fb_cmd2 *mode_cmd,
2474d8d096eSAlan Cox 					struct gtt_range *gt)
2484d8d096eSAlan Cox {
249a9a644acSDave Airlie 	u32 bpp, depth;
2504d8d096eSAlan Cox 	int ret;
2514d8d096eSAlan Cox 
252248dbc23SDave Airlie 	drm_fb_get_bpp_depth(mode_cmd->pixel_format, &depth, &bpp);
253a9a644acSDave Airlie 
254a9a644acSDave Airlie 	if (mode_cmd->pitches[0] & 63)
2554d8d096eSAlan Cox 		return -EINVAL;
256a9a644acSDave Airlie 	switch (bpp) {
2574d8d096eSAlan Cox 	case 8:
2584d8d096eSAlan Cox 	case 16:
2594d8d096eSAlan Cox 	case 24:
2604d8d096eSAlan Cox 	case 32:
2614d8d096eSAlan Cox 		break;
2624d8d096eSAlan Cox 	default:
2634d8d096eSAlan Cox 		return -EINVAL;
2644d8d096eSAlan Cox 	}
265c7d73f6aSDaniel Vetter 	drm_helper_mode_fill_fb_struct(&fb->base, mode_cmd);
266c7d73f6aSDaniel Vetter 	fb->gtt = gt;
2674d8d096eSAlan Cox 	ret = drm_framebuffer_init(dev, &fb->base, &psb_fb_funcs);
2684d8d096eSAlan Cox 	if (ret) {
2694d8d096eSAlan Cox 		dev_err(dev->dev, "framebuffer init failed: %d\n", ret);
2704d8d096eSAlan Cox 		return ret;
2714d8d096eSAlan Cox 	}
2724d8d096eSAlan Cox 	return 0;
2734d8d096eSAlan Cox }
2744d8d096eSAlan Cox 
2754d8d096eSAlan Cox /**
2764d8d096eSAlan Cox  *	psb_framebuffer_create	-	create a framebuffer backed by gt
2774d8d096eSAlan Cox  *	@dev: our DRM device
2784d8d096eSAlan Cox  *	@mode_cmd: the description of the requested mode
2794d8d096eSAlan Cox  *	@gt: the backing object
2804d8d096eSAlan Cox  *
2814d8d096eSAlan Cox  *	Create a framebuffer object backed by the gt, and fill in the
2824d8d096eSAlan Cox  *	boilerplate required
2834d8d096eSAlan Cox  *
2844d8d096eSAlan Cox  *	TODO: review object references
2854d8d096eSAlan Cox  */
2864d8d096eSAlan Cox 
2874d8d096eSAlan Cox static struct drm_framebuffer *psb_framebuffer_create
2884d8d096eSAlan Cox 			(struct drm_device *dev,
2891eb83451SVille Syrjälä 			 const struct drm_mode_fb_cmd2 *mode_cmd,
2904d8d096eSAlan Cox 			 struct gtt_range *gt)
2914d8d096eSAlan Cox {
2924d8d096eSAlan Cox 	struct psb_framebuffer *fb;
2934d8d096eSAlan Cox 	int ret;
2944d8d096eSAlan Cox 
2954d8d096eSAlan Cox 	fb = kzalloc(sizeof(*fb), GFP_KERNEL);
2964d8d096eSAlan Cox 	if (!fb)
2974d8d096eSAlan Cox 		return ERR_PTR(-ENOMEM);
2984d8d096eSAlan Cox 
2994d8d096eSAlan Cox 	ret = psb_framebuffer_init(dev, fb, mode_cmd, gt);
3004d8d096eSAlan Cox 	if (ret) {
3014d8d096eSAlan Cox 		kfree(fb);
3024d8d096eSAlan Cox 		return ERR_PTR(ret);
3034d8d096eSAlan Cox 	}
3044d8d096eSAlan Cox 	return &fb->base;
3054d8d096eSAlan Cox }
3064d8d096eSAlan Cox 
3074d8d096eSAlan Cox /**
3084d8d096eSAlan Cox  *	psbfb_alloc		-	allocate frame buffer memory
3094d8d096eSAlan Cox  *	@dev: the DRM device
3104d8d096eSAlan Cox  *	@aligned_size: space needed
311a6ba582dSAlan Cox  *	@force: fall back to GEM buffers if need be
3124d8d096eSAlan Cox  *
3134d8d096eSAlan Cox  *	Allocate the frame buffer. In the usual case we get a GTT range that
3144d8d096eSAlan Cox  *	is stolen memory backed and life is simple. If there isn't sufficient
315dffc9cebSAlan Cox  *	we fail as we don't have the virtual mapping space to really vmap it
316dffc9cebSAlan Cox  *	and the kernel console code can't handle non linear framebuffers.
3174d8d096eSAlan Cox  *
318dffc9cebSAlan Cox  *	Re-address this as and if the framebuffer layer grows this ability.
3194d8d096eSAlan Cox  */
3204d8d096eSAlan Cox static struct gtt_range *psbfb_alloc(struct drm_device *dev, int aligned_size)
3214d8d096eSAlan Cox {
3224d8d096eSAlan Cox 	struct gtt_range *backing;
3234d8d096eSAlan Cox 	/* Begin by trying to use stolen memory backing */
324c269c685SPatrik Jakobsson 	backing = psb_gtt_alloc_range(dev, aligned_size, "fb", 1, PAGE_SIZE);
3254d8d096eSAlan Cox 	if (backing) {
32689c8233fSDavid Herrmann 		drm_gem_private_object_init(dev, &backing->gem, aligned_size);
3274d8d096eSAlan Cox 		return backing;
3284d8d096eSAlan Cox 	}
3294d8d096eSAlan Cox 	return NULL;
3304d8d096eSAlan Cox }
3314d8d096eSAlan Cox 
3324d8d096eSAlan Cox /**
3334d8d096eSAlan Cox  *	psbfb_create		-	create a framebuffer
3344d8d096eSAlan Cox  *	@fbdev: the framebuffer device
3354d8d096eSAlan Cox  *	@sizes: specification of the layout
3364d8d096eSAlan Cox  *
3374d8d096eSAlan Cox  *	Create a framebuffer to the specifications provided
3384d8d096eSAlan Cox  */
3394d8d096eSAlan Cox static int psbfb_create(struct psb_fbdev *fbdev,
3404d8d096eSAlan Cox 				struct drm_fb_helper_surface_size *sizes)
3414d8d096eSAlan Cox {
3424d8d096eSAlan Cox 	struct drm_device *dev = fbdev->psb_fb_helper.dev;
3434d8d096eSAlan Cox 	struct drm_psb_private *dev_priv = dev->dev_private;
3444d8d096eSAlan Cox 	struct fb_info *info;
3454d8d096eSAlan Cox 	struct drm_framebuffer *fb;
3464d8d096eSAlan Cox 	struct psb_framebuffer *psbfb = &fbdev->pfb;
347a9a644acSDave Airlie 	struct drm_mode_fb_cmd2 mode_cmd;
3484d8d096eSAlan Cox 	int size;
3494d8d096eSAlan Cox 	int ret;
3504d8d096eSAlan Cox 	struct gtt_range *backing;
351a9a644acSDave Airlie 	u32 bpp, depth;
3521b223c9eSAlan Cox 	int gtt_roll = 0;
3531b223c9eSAlan Cox 	int pitch_lines = 0;
3544d8d096eSAlan Cox 
3554d8d096eSAlan Cox 	mode_cmd.width = sizes->surface_width;
3564d8d096eSAlan Cox 	mode_cmd.height = sizes->surface_height;
357a9a644acSDave Airlie 	bpp = sizes->surface_bpp;
3586aa1ead1SKirill A. Shutemov 	depth = sizes->surface_depth;
3594d8d096eSAlan Cox 
3604d8d096eSAlan Cox 	/* No 24bit packed */
361a9a644acSDave Airlie 	if (bpp == 24)
362a9a644acSDave Airlie 		bpp = 32;
3634d8d096eSAlan Cox 
3641b223c9eSAlan Cox 	do {
3651b223c9eSAlan Cox 		/*
3661b223c9eSAlan Cox 		 * Acceleration via the GTT requires pitch to be
3671b223c9eSAlan Cox 		 * power of two aligned. Preferably page but less
3681b223c9eSAlan Cox 		 * is ok with some fonts
3691b223c9eSAlan Cox 		 */
3701b223c9eSAlan Cox         	mode_cmd.pitches[0] =  ALIGN(mode_cmd.width * ((bpp + 7) / 8), 4096 >> pitch_lines);
3714d8d096eSAlan Cox 
372a9a644acSDave Airlie         	size = mode_cmd.pitches[0] * mode_cmd.height;
3734d8d096eSAlan Cox         	size = ALIGN(size, PAGE_SIZE);
3744d8d096eSAlan Cox 
3751b223c9eSAlan Cox 		/* Allocate the fb in the GTT with stolen page backing */
376a6ba582dSAlan Cox 		backing = psbfb_alloc(dev, size);
3771b223c9eSAlan Cox 
3781b223c9eSAlan Cox 		if (pitch_lines)
3791b223c9eSAlan Cox 			pitch_lines *= 2;
3801b223c9eSAlan Cox 		else
3811b223c9eSAlan Cox 			pitch_lines = 1;
3821b223c9eSAlan Cox 		gtt_roll++;
3831b223c9eSAlan Cox 	} while (backing == NULL && pitch_lines <= 16);
3841b223c9eSAlan Cox 
3851b223c9eSAlan Cox 	/* The final pitch we accepted if we succeeded */
3861b223c9eSAlan Cox 	pitch_lines /= 2;
3871b223c9eSAlan Cox 
388a6ba582dSAlan Cox 	if (backing == NULL) {
389a6ba582dSAlan Cox 		/*
390a6ba582dSAlan Cox 		 *	We couldn't get the space we wanted, fall back to the
391a6ba582dSAlan Cox 		 *	display engine requirement instead.  The HW requires
392a6ba582dSAlan Cox 		 *	the pitch to be 64 byte aligned
393a6ba582dSAlan Cox 		 */
394a6ba582dSAlan Cox 
395a6ba582dSAlan Cox 		gtt_roll = 0;	/* Don't use GTT accelerated scrolling */
3961b223c9eSAlan Cox 		pitch_lines = 64;
397a6ba582dSAlan Cox 
398a6ba582dSAlan Cox 		mode_cmd.pitches[0] =  ALIGN(mode_cmd.width * ((bpp + 7) / 8), 64);
399a6ba582dSAlan Cox 
400a6ba582dSAlan Cox 		size = mode_cmd.pitches[0] * mode_cmd.height;
401a6ba582dSAlan Cox 		size = ALIGN(size, PAGE_SIZE);
402a6ba582dSAlan Cox 
4034d8d096eSAlan Cox 		/* Allocate the framebuffer in the GTT with stolen page backing */
4044d8d096eSAlan Cox 		backing = psbfb_alloc(dev, size);
4054d8d096eSAlan Cox 		if (backing == NULL)
4064d8d096eSAlan Cox 			return -ENOMEM;
407a6ba582dSAlan Cox 	}
4084d8d096eSAlan Cox 
409bb849779SAlan Cox 	memset(dev_priv->vram_addr + backing->offset, 0, size);
410bb849779SAlan Cox 
411546187c8SArchit Taneja 	info = drm_fb_helper_alloc_fbi(&fbdev->psb_fb_helper);
412546187c8SArchit Taneja 	if (IS_ERR(info)) {
413546187c8SArchit Taneja 		ret = PTR_ERR(info);
4144cd54d98SSudip Mukherjee 		goto err_free_range;
4154d8d096eSAlan Cox 	}
4164d8d096eSAlan Cox 	info->par = fbdev;
4174d8d096eSAlan Cox 
418a9a644acSDave Airlie 	mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
419a9a644acSDave Airlie 
4204d8d096eSAlan Cox 	ret = psb_framebuffer_init(dev, psbfb, &mode_cmd, backing);
4214d8d096eSAlan Cox 	if (ret)
4224cd54d98SSudip Mukherjee 		goto err_release;
4234d8d096eSAlan Cox 
4244d8d096eSAlan Cox 	fb = &psbfb->base;
4254d8d096eSAlan Cox 	psbfb->fbdev = info;
4264d8d096eSAlan Cox 
4274d8d096eSAlan Cox 	fbdev->psb_fb_helper.fb = fb;
4284d8d096eSAlan Cox 
4294578240bSAlan Cox 	drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
430f9d8149aSPatrik Jakobsson 	strcpy(info->fix.id, "psbdrmfb");
4314d8d096eSAlan Cox 
4324d8d096eSAlan Cox 	info->flags = FBINFO_DEFAULT;
4331b223c9eSAlan Cox 	if (dev_priv->ops->accel_2d && pitch_lines > 8)	/* 2D engine */
4341b223c9eSAlan Cox 		info->fbops = &psbfb_ops;
4351b223c9eSAlan Cox 	else if (gtt_roll) {	/* GTT rolling seems best */
436a6ba582dSAlan Cox 		info->fbops = &psbfb_roll_ops;
437a6ba582dSAlan Cox 		info->flags |= FBINFO_HWACCEL_YPAN;
4381b223c9eSAlan Cox 	} else	/* Software */
439a6ba582dSAlan Cox 		info->fbops = &psbfb_unaccel_ops;
4404d8d096eSAlan Cox 
4414d8d096eSAlan Cox 	info->fix.smem_start = dev->mode_config.fb_base;
4424d8d096eSAlan Cox 	info->fix.smem_len = size;
443a6ba582dSAlan Cox 	info->fix.ywrapstep = gtt_roll;
444a6ba582dSAlan Cox 	info->fix.ypanstep = 0;
4454d8d096eSAlan Cox 
4464d8d096eSAlan Cox 	/* Accessed stolen memory directly */
44737214ca0SKirill A. Shutemov 	info->screen_base = dev_priv->vram_addr + backing->offset;
4484d8d096eSAlan Cox 	info->screen_size = size;
4494d8d096eSAlan Cox 
4504d8d096eSAlan Cox 	if (dev_priv->gtt.stolen_size) {
4514d8d096eSAlan Cox 		info->apertures->ranges[0].base = dev->mode_config.fb_base;
4524d8d096eSAlan Cox 		info->apertures->ranges[0].size = dev_priv->gtt.stolen_size;
4534d8d096eSAlan Cox 	}
4544d8d096eSAlan Cox 
4554d8d096eSAlan Cox 	drm_fb_helper_fill_var(info, &fbdev->psb_fb_helper,
4564d8d096eSAlan Cox 				sizes->fb_width, sizes->fb_height);
4574d8d096eSAlan Cox 
4584d8d096eSAlan Cox 	info->fix.mmio_start = pci_resource_start(dev->pdev, 0);
4594d8d096eSAlan Cox 	info->fix.mmio_len = pci_resource_len(dev->pdev, 0);
4604d8d096eSAlan Cox 
461fb2a99e1SSascha Hauer 	/* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
4624d8d096eSAlan Cox 
46331a0685aSAlan Cox 	dev_dbg(dev->dev, "allocated %dx%d fb\n",
4644d8d096eSAlan Cox 					psbfb->base.width, psbfb->base.height);
4654d8d096eSAlan Cox 
4664d8d096eSAlan Cox 	return 0;
4674cd54d98SSudip Mukherjee err_release:
468546187c8SArchit Taneja 	drm_fb_helper_release_fbi(&fbdev->psb_fb_helper);
4694cd54d98SSudip Mukherjee err_free_range:
4704d8d096eSAlan Cox 	psb_gtt_free_range(dev, backing);
4714d8d096eSAlan Cox 	return ret;
4724d8d096eSAlan Cox }
4734d8d096eSAlan Cox 
4744d8d096eSAlan Cox /**
4754d8d096eSAlan Cox  *	psb_user_framebuffer_create	-	create framebuffer
4764d8d096eSAlan Cox  *	@dev: our DRM device
4774d8d096eSAlan Cox  *	@filp: client file
4784d8d096eSAlan Cox  *	@cmd: mode request
4794d8d096eSAlan Cox  *
4804d8d096eSAlan Cox  *	Create a new framebuffer backed by a userspace GEM object
4814d8d096eSAlan Cox  */
4824d8d096eSAlan Cox static struct drm_framebuffer *psb_user_framebuffer_create
4834d8d096eSAlan Cox 			(struct drm_device *dev, struct drm_file *filp,
4841eb83451SVille Syrjälä 			 const struct drm_mode_fb_cmd2 *cmd)
4854d8d096eSAlan Cox {
4864d8d096eSAlan Cox 	struct gtt_range *r;
4874d8d096eSAlan Cox 	struct drm_gem_object *obj;
4884d8d096eSAlan Cox 
4894d8d096eSAlan Cox 	/*
4904d8d096eSAlan Cox 	 *	Find the GEM object and thus the gtt range object that is
4914d8d096eSAlan Cox 	 *	to back this space
4924d8d096eSAlan Cox 	 */
493a8ad0bd8SChris Wilson 	obj = drm_gem_object_lookup(filp, cmd->handles[0]);
4944d8d096eSAlan Cox 	if (obj == NULL)
4954d8d096eSAlan Cox 		return ERR_PTR(-ENOENT);
4964d8d096eSAlan Cox 
4974d8d096eSAlan Cox 	/* Let the core code do all the work */
4984d8d096eSAlan Cox 	r = container_of(obj, struct gtt_range, gem);
4994d8d096eSAlan Cox 	return psb_framebuffer_create(dev, cmd, r);
5004d8d096eSAlan Cox }
5014d8d096eSAlan Cox 
5024d8d096eSAlan Cox static void psbfb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
5034d8d096eSAlan Cox 							u16 blue, int regno)
5044d8d096eSAlan Cox {
5056306865dSPatrik Jakobsson 	struct gma_crtc *gma_crtc = to_gma_crtc(crtc);
5063df546beSAlan Cox 
5076306865dSPatrik Jakobsson 	gma_crtc->lut_r[regno] = red >> 8;
5086306865dSPatrik Jakobsson 	gma_crtc->lut_g[regno] = green >> 8;
5096306865dSPatrik Jakobsson 	gma_crtc->lut_b[regno] = blue >> 8;
5104d8d096eSAlan Cox }
5114d8d096eSAlan Cox 
5124d8d096eSAlan Cox static void psbfb_gamma_get(struct drm_crtc *crtc, u16 *red,
5134d8d096eSAlan Cox 					u16 *green, u16 *blue, int regno)
5144d8d096eSAlan Cox {
5156306865dSPatrik Jakobsson 	struct gma_crtc *gma_crtc = to_gma_crtc(crtc);
5163df546beSAlan Cox 
5176306865dSPatrik Jakobsson 	*red = gma_crtc->lut_r[regno] << 8;
5186306865dSPatrik Jakobsson 	*green = gma_crtc->lut_g[regno] << 8;
5196306865dSPatrik Jakobsson 	*blue = gma_crtc->lut_b[regno] << 8;
5204d8d096eSAlan Cox }
5214d8d096eSAlan Cox 
5224d8d096eSAlan Cox static int psbfb_probe(struct drm_fb_helper *helper,
5234d8d096eSAlan Cox 				struct drm_fb_helper_surface_size *sizes)
5244d8d096eSAlan Cox {
525c39aa6a1SFabian Frederick 	struct psb_fbdev *psb_fbdev =
526c39aa6a1SFabian Frederick 		container_of(helper, struct psb_fbdev, psb_fb_helper);
5273aad16d2SAlan Cox 	struct drm_device *dev = psb_fbdev->psb_fb_helper.dev;
5283aad16d2SAlan Cox 	struct drm_psb_private *dev_priv = dev->dev_private;
5293aad16d2SAlan Cox 	int bytespp;
5304d8d096eSAlan Cox 
5313aad16d2SAlan Cox 	bytespp = sizes->surface_bpp / 8;
5323aad16d2SAlan Cox 	if (bytespp == 3)	/* no 24bit packed */
5333aad16d2SAlan Cox 		bytespp = 4;
5343aad16d2SAlan Cox 
5353aad16d2SAlan Cox 	/* If the mode will not fit in 32bit then switch to 16bit to get
5363aad16d2SAlan Cox 	   a console on full resolution. The X mode setting server will
5373aad16d2SAlan Cox 	   allocate its own 32bit GEM framebuffer */
5383aad16d2SAlan Cox 	if (ALIGN(sizes->fb_width * bytespp, 64) * sizes->fb_height >
5393aad16d2SAlan Cox 	                dev_priv->vram_stolen_size) {
5403aad16d2SAlan Cox                 sizes->surface_bpp = 16;
5413aad16d2SAlan Cox                 sizes->surface_depth = 16;
5423aad16d2SAlan Cox         }
5433aad16d2SAlan Cox 
544cd5428a5SDaniel Vetter 	return psbfb_create(psb_fbdev, sizes);
5454d8d096eSAlan Cox }
5464d8d096eSAlan Cox 
5473a493879SThierry Reding static const struct drm_fb_helper_funcs psb_fb_helper_funcs = {
5484d8d096eSAlan Cox 	.gamma_set = psbfb_gamma_set,
5494d8d096eSAlan Cox 	.gamma_get = psbfb_gamma_get,
5504d8d096eSAlan Cox 	.fb_probe = psbfb_probe,
5514d8d096eSAlan Cox };
5524d8d096eSAlan Cox 
553bc7f2b08SKirill A. Shutemov static int psb_fbdev_destroy(struct drm_device *dev, struct psb_fbdev *fbdev)
5544d8d096eSAlan Cox {
5554d8d096eSAlan Cox 	struct psb_framebuffer *psbfb = &fbdev->pfb;
5564d8d096eSAlan Cox 
557546187c8SArchit Taneja 	drm_fb_helper_unregister_fbi(&fbdev->psb_fb_helper);
558546187c8SArchit Taneja 	drm_fb_helper_release_fbi(&fbdev->psb_fb_helper);
559546187c8SArchit Taneja 
5604d8d096eSAlan Cox 	drm_fb_helper_fini(&fbdev->psb_fb_helper);
56136206361SDaniel Vetter 	drm_framebuffer_unregister_private(&psbfb->base);
5624d8d096eSAlan Cox 	drm_framebuffer_cleanup(&psbfb->base);
5634d8d096eSAlan Cox 
5644d8d096eSAlan Cox 	if (psbfb->gtt)
56546a0f223SDaniel Vetter 		drm_gem_object_unreference_unlocked(&psbfb->gtt->gem);
5664d8d096eSAlan Cox 	return 0;
5674d8d096eSAlan Cox }
5684d8d096eSAlan Cox 
5694d8d096eSAlan Cox int psb_fbdev_init(struct drm_device *dev)
5704d8d096eSAlan Cox {
5714d8d096eSAlan Cox 	struct psb_fbdev *fbdev;
5724d8d096eSAlan Cox 	struct drm_psb_private *dev_priv = dev->dev_private;
57301934c2aSThierry Reding 	int ret;
5744d8d096eSAlan Cox 
5754d8d096eSAlan Cox 	fbdev = kzalloc(sizeof(struct psb_fbdev), GFP_KERNEL);
5764d8d096eSAlan Cox 	if (!fbdev) {
5774d8d096eSAlan Cox 		dev_err(dev->dev, "no memory\n");
5784d8d096eSAlan Cox 		return -ENOMEM;
5794d8d096eSAlan Cox 	}
5804d8d096eSAlan Cox 
5814d8d096eSAlan Cox 	dev_priv->fbdev = fbdev;
58210a23102SThierry Reding 
58310a23102SThierry Reding 	drm_fb_helper_prepare(dev, &fbdev->psb_fb_helper, &psb_fb_helper_funcs);
5844d8d096eSAlan Cox 
58501934c2aSThierry Reding 	ret = drm_fb_helper_init(dev, &fbdev->psb_fb_helper,
58601934c2aSThierry Reding 				 dev_priv->ops->crtcs, INTELFB_CONN_LIMIT);
58701934c2aSThierry Reding 	if (ret)
58801934c2aSThierry Reding 		goto free;
5894d8d096eSAlan Cox 
59001934c2aSThierry Reding 	ret = drm_fb_helper_single_add_all_connectors(&fbdev->psb_fb_helper);
59101934c2aSThierry Reding 	if (ret)
59201934c2aSThierry Reding 		goto fini;
59376a39dbfSDaniel Vetter 
59476a39dbfSDaniel Vetter 	/* disable all the possible outputs/crtcs before entering KMS mode */
59576a39dbfSDaniel Vetter 	drm_helper_disable_unused_functions(dev);
59676a39dbfSDaniel Vetter 
59701934c2aSThierry Reding 	ret = drm_fb_helper_initial_config(&fbdev->psb_fb_helper, 32);
59801934c2aSThierry Reding 	if (ret)
59901934c2aSThierry Reding 		goto fini;
60001934c2aSThierry Reding 
6014d8d096eSAlan Cox 	return 0;
60201934c2aSThierry Reding 
60301934c2aSThierry Reding fini:
60401934c2aSThierry Reding 	drm_fb_helper_fini(&fbdev->psb_fb_helper);
60501934c2aSThierry Reding free:
60601934c2aSThierry Reding 	kfree(fbdev);
60701934c2aSThierry Reding 	return ret;
6084d8d096eSAlan Cox }
6094d8d096eSAlan Cox 
610bc7f2b08SKirill A. Shutemov static void psb_fbdev_fini(struct drm_device *dev)
6114d8d096eSAlan Cox {
6124d8d096eSAlan Cox 	struct drm_psb_private *dev_priv = dev->dev_private;
6134d8d096eSAlan Cox 
6144d8d096eSAlan Cox 	if (!dev_priv->fbdev)
6154d8d096eSAlan Cox 		return;
6164d8d096eSAlan Cox 
6174d8d096eSAlan Cox 	psb_fbdev_destroy(dev, dev_priv->fbdev);
6184d8d096eSAlan Cox 	kfree(dev_priv->fbdev);
6194d8d096eSAlan Cox 	dev_priv->fbdev = NULL;
6204d8d096eSAlan Cox }
6214d8d096eSAlan Cox 
6224d8d096eSAlan Cox static void psbfb_output_poll_changed(struct drm_device *dev)
6234d8d096eSAlan Cox {
6244d8d096eSAlan Cox 	struct drm_psb_private *dev_priv = dev->dev_private;
6254d8d096eSAlan Cox 	struct psb_fbdev *fbdev = (struct psb_fbdev *)dev_priv->fbdev;
6264d8d096eSAlan Cox 	drm_fb_helper_hotplug_event(&fbdev->psb_fb_helper);
6274d8d096eSAlan Cox }
6284d8d096eSAlan Cox 
6294d8d096eSAlan Cox /**
6304d8d096eSAlan Cox  *	psb_user_framebuffer_create_handle - add hamdle to a framebuffer
6314d8d096eSAlan Cox  *	@fb: framebuffer
6324d8d096eSAlan Cox  *	@file_priv: our DRM file
6334d8d096eSAlan Cox  *	@handle: returned handle
6344d8d096eSAlan Cox  *
6354d8d096eSAlan Cox  *	Our framebuffer object is a GTT range which also contains a GEM
6364d8d096eSAlan Cox  *	object. We need to turn it into a handle for userspace. GEM will do
6374d8d096eSAlan Cox  *	the work for us
6384d8d096eSAlan Cox  */
6394d8d096eSAlan Cox static int psb_user_framebuffer_create_handle(struct drm_framebuffer *fb,
6404d8d096eSAlan Cox 					      struct drm_file *file_priv,
6414d8d096eSAlan Cox 					      unsigned int *handle)
6424d8d096eSAlan Cox {
6434d8d096eSAlan Cox 	struct psb_framebuffer *psbfb = to_psb_fb(fb);
6444d8d096eSAlan Cox 	struct gtt_range *r = psbfb->gtt;
6454d8d096eSAlan Cox 	return drm_gem_handle_create(file_priv, &r->gem, handle);
6464d8d096eSAlan Cox }
6474d8d096eSAlan Cox 
6484d8d096eSAlan Cox /**
6494d8d096eSAlan Cox  *	psb_user_framebuffer_destroy	-	destruct user created fb
6504d8d096eSAlan Cox  *	@fb: framebuffer
6514d8d096eSAlan Cox  *
6524d8d096eSAlan Cox  *	User framebuffers are backed by GEM objects so all we have to do is
6534d8d096eSAlan Cox  *	clean up a bit and drop the reference, GEM will handle the fallout
6544d8d096eSAlan Cox  */
6554d8d096eSAlan Cox static void psb_user_framebuffer_destroy(struct drm_framebuffer *fb)
6564d8d096eSAlan Cox {
6574d8d096eSAlan Cox 	struct psb_framebuffer *psbfb = to_psb_fb(fb);
6584d8d096eSAlan Cox 	struct gtt_range *r = psbfb->gtt;
6594d8d096eSAlan Cox 
6604d8d096eSAlan Cox 	/* Let DRM do its clean up */
6614d8d096eSAlan Cox 	drm_framebuffer_cleanup(fb);
6624d8d096eSAlan Cox 	/*  We are no longer using the resource in GEM */
6634d8d096eSAlan Cox 	drm_gem_object_unreference_unlocked(&r->gem);
6644d8d096eSAlan Cox 	kfree(fb);
6654d8d096eSAlan Cox }
6664d8d096eSAlan Cox 
6674d8d096eSAlan Cox static const struct drm_mode_config_funcs psb_mode_funcs = {
6684d8d096eSAlan Cox 	.fb_create = psb_user_framebuffer_create,
6694d8d096eSAlan Cox 	.output_poll_changed = psbfb_output_poll_changed,
6704d8d096eSAlan Cox };
6714d8d096eSAlan Cox 
6724d8d096eSAlan Cox static void psb_setup_outputs(struct drm_device *dev)
6734d8d096eSAlan Cox {
6744d8d096eSAlan Cox 	struct drm_psb_private *dev_priv = dev->dev_private;
6754d8d096eSAlan Cox 	struct drm_connector *connector;
6764d8d096eSAlan Cox 
6774d8d096eSAlan Cox 	drm_mode_create_scaling_mode_property(dev);
6784d8d096eSAlan Cox 
67913619ce5SAlan Cox 	/* It is ok for this to fail - we just don't get backlight control */
68013619ce5SAlan Cox 	if (!dev_priv->backlight_property)
68113619ce5SAlan Cox 		dev_priv->backlight_property = drm_property_create_range(dev, 0,
68213619ce5SAlan Cox 							"backlight", 0, 100);
6834d8d096eSAlan Cox 	dev_priv->ops->output_init(dev);
6844d8d096eSAlan Cox 
6854d8d096eSAlan Cox 	list_for_each_entry(connector, &dev->mode_config.connector_list,
6864d8d096eSAlan Cox 			    head) {
687367e4408SPatrik Jakobsson 		struct gma_encoder *gma_encoder = gma_attached_encoder(connector);
688367e4408SPatrik Jakobsson 		struct drm_encoder *encoder = &gma_encoder->base;
6894d8d096eSAlan Cox 		int crtc_mask = 0, clone_mask = 0;
6904d8d096eSAlan Cox 
6914d8d096eSAlan Cox 		/* valid crtcs */
692367e4408SPatrik Jakobsson 		switch (gma_encoder->type) {
6934d8d096eSAlan Cox 		case INTEL_OUTPUT_ANALOG:
6944d8d096eSAlan Cox 			crtc_mask = (1 << 0);
6954d8d096eSAlan Cox 			clone_mask = (1 << INTEL_OUTPUT_ANALOG);
6964d8d096eSAlan Cox 			break;
6974d8d096eSAlan Cox 		case INTEL_OUTPUT_SDVO:
698cf8efd3aSPatrik Jakobsson 			crtc_mask = dev_priv->ops->sdvo_mask;
6994d8d096eSAlan Cox 			clone_mask = (1 << INTEL_OUTPUT_SDVO);
7004d8d096eSAlan Cox 			break;
7014d8d096eSAlan Cox 		case INTEL_OUTPUT_LVDS:
702d235e64aSAlan Cox 		        crtc_mask = dev_priv->ops->lvds_mask;
7034d8d096eSAlan Cox 			clone_mask = (1 << INTEL_OUTPUT_LVDS);
7044d8d096eSAlan Cox 			break;
7054d8d096eSAlan Cox 		case INTEL_OUTPUT_MIPI:
7064d8d096eSAlan Cox 			crtc_mask = (1 << 0);
7074d8d096eSAlan Cox 			clone_mask = (1 << INTEL_OUTPUT_MIPI);
7084d8d096eSAlan Cox 			break;
7094d8d096eSAlan Cox 		case INTEL_OUTPUT_MIPI2:
7104d8d096eSAlan Cox 			crtc_mask = (1 << 2);
7114d8d096eSAlan Cox 			clone_mask = (1 << INTEL_OUTPUT_MIPI2);
7124d8d096eSAlan Cox 			break;
7134d8d096eSAlan Cox 		case INTEL_OUTPUT_HDMI:
714d235e64aSAlan Cox 		        crtc_mask = dev_priv->ops->hdmi_mask;
7154d8d096eSAlan Cox 			clone_mask = (1 << INTEL_OUTPUT_HDMI);
7164d8d096eSAlan Cox 			break;
717220801bdSAlan Cox 		case INTEL_OUTPUT_DISPLAYPORT:
718220801bdSAlan Cox 			crtc_mask = (1 << 0) | (1 << 1);
719220801bdSAlan Cox 			clone_mask = (1 << INTEL_OUTPUT_DISPLAYPORT);
720220801bdSAlan Cox 			break;
721d112a816SZhao Yakui 		case INTEL_OUTPUT_EDP:
722d112a816SZhao Yakui 			crtc_mask = (1 << 1);
723d112a816SZhao Yakui 			clone_mask = (1 << INTEL_OUTPUT_EDP);
7244d8d096eSAlan Cox 		}
7254d8d096eSAlan Cox 		encoder->possible_crtcs = crtc_mask;
7264d8d096eSAlan Cox 		encoder->possible_clones =
727a3d5d75fSPatrik Jakobsson 		    gma_connector_clones(dev, clone_mask);
7284d8d096eSAlan Cox 	}
7294d8d096eSAlan Cox }
7304d8d096eSAlan Cox 
7314d8d096eSAlan Cox void psb_modeset_init(struct drm_device *dev)
7324d8d096eSAlan Cox {
7334d8d096eSAlan Cox 	struct drm_psb_private *dev_priv = dev->dev_private;
7344d8d096eSAlan Cox 	struct psb_intel_mode_device *mode_dev = &dev_priv->mode_dev;
7354d8d096eSAlan Cox 	int i;
7364d8d096eSAlan Cox 
7374d8d096eSAlan Cox 	drm_mode_config_init(dev);
7384d8d096eSAlan Cox 
7394d8d096eSAlan Cox 	dev->mode_config.min_width = 0;
7404d8d096eSAlan Cox 	dev->mode_config.min_height = 0;
7414d8d096eSAlan Cox 
742e6ecefaaSLaurent Pinchart 	dev->mode_config.funcs = &psb_mode_funcs;
7434d8d096eSAlan Cox 
7444d8d096eSAlan Cox 	/* set memory base */
745dffc9cebSAlan Cox 	/* Oaktrail and Poulsbo should use BAR 2*/
7464d8d096eSAlan Cox 	pci_read_config_dword(dev->pdev, PSB_BSM, (u32 *)
7474d8d096eSAlan Cox 					&(dev->mode_config.fb_base));
7484d8d096eSAlan Cox 
7494d8d096eSAlan Cox 	/* num pipes is 2 for PSB but 1 for Mrst */
7504d8d096eSAlan Cox 	for (i = 0; i < dev_priv->num_pipe; i++)
7514d8d096eSAlan Cox 		psb_intel_crtc_init(dev, i, mode_dev);
7524d8d096eSAlan Cox 
753cbbd379aSPatrik Jakobsson 	dev->mode_config.max_width = 4096;
754cbbd379aSPatrik Jakobsson 	dev->mode_config.max_height = 4096;
7554d8d096eSAlan Cox 
7564d8d096eSAlan Cox 	psb_setup_outputs(dev);
757d235e64aSAlan Cox 
758d235e64aSAlan Cox 	if (dev_priv->ops->errata)
759d235e64aSAlan Cox 	        dev_priv->ops->errata(dev);
7604ab2c7f1SAlan Cox 
7614ab2c7f1SAlan Cox         dev_priv->modeset = true;
7624d8d096eSAlan Cox }
7634d8d096eSAlan Cox 
7644d8d096eSAlan Cox void psb_modeset_cleanup(struct drm_device *dev)
7654d8d096eSAlan Cox {
7664ab2c7f1SAlan Cox 	struct drm_psb_private *dev_priv = dev->dev_private;
7674ab2c7f1SAlan Cox 	if (dev_priv->modeset) {
7684d8d096eSAlan Cox 		drm_kms_helper_poll_fini(dev);
7694d8d096eSAlan Cox 		psb_fbdev_fini(dev);
7704d8d096eSAlan Cox 		drm_mode_config_cleanup(dev);
7714d8d096eSAlan Cox 	}
7724ab2c7f1SAlan Cox }
773