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>
244d8d096eSAlan Cox #include <linux/mm.h>
254d8d096eSAlan Cox #include <linux/tty.h>
264d8d096eSAlan Cox #include <linux/slab.h>
274d8d096eSAlan Cox #include <linux/delay.h>
284d8d096eSAlan Cox #include <linux/fb.h>
294d8d096eSAlan Cox #include <linux/init.h>
304d8d096eSAlan Cox #include <linux/console.h>
314d8d096eSAlan Cox 
324d8d096eSAlan Cox #include <drm/drmP.h>
334d8d096eSAlan Cox #include <drm/drm.h>
344d8d096eSAlan Cox #include <drm/drm_crtc.h>
35a9a644acSDave Airlie #include <drm/drm_fb_helper.h>
364d8d096eSAlan Cox 
374d8d096eSAlan Cox #include "psb_drv.h"
384d8d096eSAlan Cox #include "psb_intel_reg.h"
394d8d096eSAlan Cox #include "psb_intel_drv.h"
404d8d096eSAlan Cox #include "framebuffer.h"
41a6ba582dSAlan Cox #include "gtt.h"
424d8d096eSAlan Cox 
434d8d096eSAlan Cox static void psb_user_framebuffer_destroy(struct drm_framebuffer *fb);
444d8d096eSAlan Cox static int psb_user_framebuffer_create_handle(struct drm_framebuffer *fb,
454d8d096eSAlan Cox 					      struct drm_file *file_priv,
464d8d096eSAlan Cox 					      unsigned int *handle);
474d8d096eSAlan Cox 
484d8d096eSAlan Cox static const struct drm_framebuffer_funcs psb_fb_funcs = {
494d8d096eSAlan Cox 	.destroy = psb_user_framebuffer_destroy,
504d8d096eSAlan Cox 	.create_handle = psb_user_framebuffer_create_handle,
514d8d096eSAlan Cox };
524d8d096eSAlan Cox 
534d8d096eSAlan Cox #define CMAP_TOHW(_val, _width) ((((_val) << (_width)) + 0x7FFF - (_val)) >> 16)
544d8d096eSAlan Cox 
554d8d096eSAlan Cox static int psbfb_setcolreg(unsigned regno, unsigned red, unsigned green,
564d8d096eSAlan Cox 			   unsigned blue, unsigned transp,
574d8d096eSAlan Cox 			   struct fb_info *info)
584d8d096eSAlan Cox {
594d8d096eSAlan Cox 	struct psb_fbdev *fbdev = info->par;
604d8d096eSAlan Cox 	struct drm_framebuffer *fb = fbdev->psb_fb_helper.fb;
614d8d096eSAlan Cox 	uint32_t v;
624d8d096eSAlan Cox 
634d8d096eSAlan Cox 	if (!fb)
644d8d096eSAlan Cox 		return -ENOMEM;
654d8d096eSAlan Cox 
664d8d096eSAlan Cox 	if (regno > 255)
674d8d096eSAlan Cox 		return 1;
684d8d096eSAlan Cox 
694d8d096eSAlan Cox 	red = CMAP_TOHW(red, info->var.red.length);
704d8d096eSAlan Cox 	blue = CMAP_TOHW(blue, info->var.blue.length);
714d8d096eSAlan Cox 	green = CMAP_TOHW(green, info->var.green.length);
724d8d096eSAlan Cox 	transp = CMAP_TOHW(transp, info->var.transp.length);
734d8d096eSAlan Cox 
744d8d096eSAlan Cox 	v = (red << info->var.red.offset) |
754d8d096eSAlan Cox 	    (green << info->var.green.offset) |
764d8d096eSAlan Cox 	    (blue << info->var.blue.offset) |
774d8d096eSAlan Cox 	    (transp << info->var.transp.offset);
784d8d096eSAlan Cox 
794d8d096eSAlan Cox 	if (regno < 16) {
804d8d096eSAlan Cox 		switch (fb->bits_per_pixel) {
814d8d096eSAlan Cox 		case 16:
824d8d096eSAlan Cox 			((uint32_t *) info->pseudo_palette)[regno] = v;
834d8d096eSAlan Cox 			break;
844d8d096eSAlan Cox 		case 24:
854d8d096eSAlan Cox 		case 32:
864d8d096eSAlan Cox 			((uint32_t *) info->pseudo_palette)[regno] = v;
874d8d096eSAlan Cox 			break;
884d8d096eSAlan Cox 		}
894d8d096eSAlan Cox 	}
904d8d096eSAlan Cox 
914d8d096eSAlan Cox 	return 0;
924d8d096eSAlan Cox }
934d8d096eSAlan Cox 
94a6ba582dSAlan Cox static int psbfb_pan(struct fb_var_screeninfo *var, struct fb_info *info)
95a6ba582dSAlan Cox {
96a6ba582dSAlan Cox 	struct psb_fbdev *fbdev = info->par;
97a6ba582dSAlan Cox 	struct psb_framebuffer *psbfb = &fbdev->pfb;
98a6ba582dSAlan Cox 	struct drm_device *dev = psbfb->base.dev;
99a6ba582dSAlan Cox 
100a6ba582dSAlan Cox 	/*
101a6ba582dSAlan Cox 	 *	We have to poke our nose in here. The core fb code assumes
102a6ba582dSAlan Cox 	 *	panning is part of the hardware that can be invoked before
103a6ba582dSAlan Cox 	 *	the actual fb is mapped. In our case that isn't quite true.
104a6ba582dSAlan Cox 	 */
105a6ba582dSAlan Cox 	if (psbfb->gtt->npage) {
106a6ba582dSAlan Cox 		/* GTT roll shifts in 4K pages, we need to shift the right
107a6ba582dSAlan Cox 		   number of pages */
108a6ba582dSAlan Cox 		int pages = info->fix.line_length >> 12;
109a6ba582dSAlan Cox 		psb_gtt_roll(dev, psbfb->gtt, var->yoffset * pages);
110a6ba582dSAlan Cox 	}
111a6ba582dSAlan Cox         return 0;
112a6ba582dSAlan Cox }
1134d8d096eSAlan Cox 
1144d8d096eSAlan Cox static int psbfb_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1154d8d096eSAlan Cox {
1164d8d096eSAlan Cox 	struct psb_framebuffer *psbfb = vma->vm_private_data;
1174d8d096eSAlan Cox 	struct drm_device *dev = psbfb->base.dev;
1184d8d096eSAlan Cox 	struct drm_psb_private *dev_priv = dev->dev_private;
1194d8d096eSAlan Cox 	int page_num;
1204d8d096eSAlan Cox 	int i;
1214d8d096eSAlan Cox 	unsigned long address;
1224d8d096eSAlan Cox 	int ret;
1234d8d096eSAlan Cox 	unsigned long pfn;
12461bb3feaSPatrik Jakobsson 	unsigned long phys_addr = (unsigned long)dev_priv->stolen_base +
12561bb3feaSPatrik Jakobsson 				  psbfb->gtt->offset;
1264d8d096eSAlan Cox 
1274d8d096eSAlan Cox 	page_num = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
1281278f7deSYoichi Yuasa 	address = (unsigned long)vmf->virtual_address - (vmf->pgoff << PAGE_SHIFT);
1294d8d096eSAlan Cox 
1304d8d096eSAlan Cox 	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1314d8d096eSAlan Cox 
1324d8d096eSAlan Cox 	for (i = 0; i < page_num; i++) {
1334d8d096eSAlan Cox 		pfn = (phys_addr >> PAGE_SHIFT);
1344d8d096eSAlan Cox 
1354d8d096eSAlan Cox 		ret = vm_insert_mixed(vma, address, pfn);
1364d8d096eSAlan Cox 		if (unlikely((ret == -EBUSY) || (ret != 0 && i > 0)))
1374d8d096eSAlan Cox 			break;
1384d8d096eSAlan Cox 		else if (unlikely(ret != 0)) {
1394d8d096eSAlan Cox 			ret = (ret == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS;
1404d8d096eSAlan Cox 			return ret;
1414d8d096eSAlan Cox 		}
1424d8d096eSAlan Cox 		address += PAGE_SIZE;
1434d8d096eSAlan Cox 		phys_addr += PAGE_SIZE;
1444d8d096eSAlan Cox 	}
1454d8d096eSAlan Cox 	return VM_FAULT_NOPAGE;
1464d8d096eSAlan Cox }
1474d8d096eSAlan Cox 
1484d8d096eSAlan Cox static void psbfb_vm_open(struct vm_area_struct *vma)
1494d8d096eSAlan Cox {
1504d8d096eSAlan Cox }
1514d8d096eSAlan Cox 
1524d8d096eSAlan Cox static void psbfb_vm_close(struct vm_area_struct *vma)
1534d8d096eSAlan Cox {
1544d8d096eSAlan Cox }
1554d8d096eSAlan Cox 
15678b68556SLaurent Pinchart static const struct vm_operations_struct psbfb_vm_ops = {
1574d8d096eSAlan Cox 	.fault	= psbfb_vm_fault,
1584d8d096eSAlan Cox 	.open	= psbfb_vm_open,
1594d8d096eSAlan Cox 	.close	= psbfb_vm_close
1604d8d096eSAlan Cox };
1614d8d096eSAlan Cox 
1624d8d096eSAlan Cox static int psbfb_mmap(struct fb_info *info, struct vm_area_struct *vma)
1634d8d096eSAlan Cox {
1644d8d096eSAlan Cox 	struct psb_fbdev *fbdev = info->par;
1654d8d096eSAlan Cox 	struct psb_framebuffer *psbfb = &fbdev->pfb;
1664d8d096eSAlan Cox 
1674d8d096eSAlan Cox 	if (vma->vm_pgoff != 0)
1684d8d096eSAlan Cox 		return -EINVAL;
1694d8d096eSAlan Cox 	if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
1704d8d096eSAlan Cox 		return -EINVAL;
1714d8d096eSAlan Cox 
1724d8d096eSAlan Cox 	if (!psbfb->addr_space)
1734d8d096eSAlan Cox 		psbfb->addr_space = vma->vm_file->f_mapping;
1744d8d096eSAlan Cox 	/*
1754d8d096eSAlan Cox 	 * If this is a GEM object then info->screen_base is the virtual
1764d8d096eSAlan Cox 	 * kernel remapping of the object. FIXME: Review if this is
1774d8d096eSAlan Cox 	 * suitable for our mmap work
1784d8d096eSAlan Cox 	 */
1794d8d096eSAlan Cox 	vma->vm_ops = &psbfb_vm_ops;
1804d8d096eSAlan Cox 	vma->vm_private_data = (void *)psbfb;
181314e51b9SKonstantin Khlebnikov 	vma->vm_flags |= VM_IO | VM_MIXEDMAP | VM_DONTEXPAND | VM_DONTDUMP;
1824d8d096eSAlan Cox 	return 0;
1834d8d096eSAlan Cox }
1844d8d096eSAlan Cox 
1854d8d096eSAlan Cox static int psbfb_ioctl(struct fb_info *info, unsigned int cmd,
1864d8d096eSAlan Cox 						unsigned long arg)
1874d8d096eSAlan Cox {
1884d8d096eSAlan Cox 	return -ENOTTY;
1894d8d096eSAlan Cox }
1904d8d096eSAlan Cox 
1914d8d096eSAlan Cox static struct fb_ops psbfb_ops = {
1924d8d096eSAlan Cox 	.owner = THIS_MODULE,
1934d8d096eSAlan Cox 	.fb_check_var = drm_fb_helper_check_var,
1944d8d096eSAlan Cox 	.fb_set_par = drm_fb_helper_set_par,
1954d8d096eSAlan Cox 	.fb_blank = drm_fb_helper_blank,
1964d8d096eSAlan Cox 	.fb_setcolreg = psbfb_setcolreg,
197546187c8SArchit Taneja 	.fb_fillrect = drm_fb_helper_cfb_fillrect,
1984d8d096eSAlan Cox 	.fb_copyarea = psbfb_copyarea,
199546187c8SArchit Taneja 	.fb_imageblit = drm_fb_helper_cfb_imageblit,
2004d8d096eSAlan Cox 	.fb_mmap = psbfb_mmap,
2014d8d096eSAlan Cox 	.fb_sync = psbfb_sync,
2024d8d096eSAlan Cox 	.fb_ioctl = psbfb_ioctl,
2034d8d096eSAlan Cox };
2044d8d096eSAlan Cox 
205a6ba582dSAlan Cox static struct fb_ops psbfb_roll_ops = {
206a6ba582dSAlan Cox 	.owner = THIS_MODULE,
207a6ba582dSAlan Cox 	.fb_check_var = drm_fb_helper_check_var,
208a6ba582dSAlan Cox 	.fb_set_par = drm_fb_helper_set_par,
209a6ba582dSAlan Cox 	.fb_blank = drm_fb_helper_blank,
210a6ba582dSAlan Cox 	.fb_setcolreg = psbfb_setcolreg,
211546187c8SArchit Taneja 	.fb_fillrect = drm_fb_helper_cfb_fillrect,
212546187c8SArchit Taneja 	.fb_copyarea = drm_fb_helper_cfb_copyarea,
213546187c8SArchit Taneja 	.fb_imageblit = drm_fb_helper_cfb_imageblit,
214a6ba582dSAlan Cox 	.fb_pan_display = psbfb_pan,
215a6ba582dSAlan Cox 	.fb_mmap = psbfb_mmap,
216a6ba582dSAlan Cox 	.fb_ioctl = psbfb_ioctl,
217a6ba582dSAlan Cox };
218a6ba582dSAlan Cox 
2194d8d096eSAlan Cox static struct fb_ops psbfb_unaccel_ops = {
2204d8d096eSAlan Cox 	.owner = THIS_MODULE,
2214d8d096eSAlan Cox 	.fb_check_var = drm_fb_helper_check_var,
2224d8d096eSAlan Cox 	.fb_set_par = drm_fb_helper_set_par,
2234d8d096eSAlan Cox 	.fb_blank = drm_fb_helper_blank,
2244d8d096eSAlan Cox 	.fb_setcolreg = psbfb_setcolreg,
225546187c8SArchit Taneja 	.fb_fillrect = drm_fb_helper_cfb_fillrect,
226546187c8SArchit Taneja 	.fb_copyarea = drm_fb_helper_cfb_copyarea,
227546187c8SArchit Taneja 	.fb_imageblit = drm_fb_helper_cfb_imageblit,
2284d8d096eSAlan Cox 	.fb_mmap = psbfb_mmap,
2294d8d096eSAlan Cox 	.fb_ioctl = psbfb_ioctl,
2304d8d096eSAlan Cox };
2314d8d096eSAlan Cox 
2324d8d096eSAlan Cox /**
2334d8d096eSAlan Cox  *	psb_framebuffer_init	-	initialize a framebuffer
2344d8d096eSAlan Cox  *	@dev: our DRM device
2354d8d096eSAlan Cox  *	@fb: framebuffer to set up
2364d8d096eSAlan Cox  *	@mode_cmd: mode description
2374d8d096eSAlan Cox  *	@gt: backing object
2384d8d096eSAlan Cox  *
2394d8d096eSAlan Cox  *	Configure and fill in the boilerplate for our frame buffer. Return
2404d8d096eSAlan Cox  *	0 on success or an error code if we fail.
2414d8d096eSAlan Cox  */
2424d8d096eSAlan Cox static int psb_framebuffer_init(struct drm_device *dev,
2434d8d096eSAlan Cox 					struct psb_framebuffer *fb,
2441eb83451SVille Syrjälä 					const struct drm_mode_fb_cmd2 *mode_cmd,
2454d8d096eSAlan Cox 					struct gtt_range *gt)
2464d8d096eSAlan Cox {
247a9a644acSDave Airlie 	u32 bpp, depth;
2484d8d096eSAlan Cox 	int ret;
2494d8d096eSAlan Cox 
250248dbc23SDave Airlie 	drm_fb_get_bpp_depth(mode_cmd->pixel_format, &depth, &bpp);
251a9a644acSDave Airlie 
252a9a644acSDave Airlie 	if (mode_cmd->pitches[0] & 63)
2534d8d096eSAlan Cox 		return -EINVAL;
254a9a644acSDave Airlie 	switch (bpp) {
2554d8d096eSAlan Cox 	case 8:
2564d8d096eSAlan Cox 	case 16:
2574d8d096eSAlan Cox 	case 24:
2584d8d096eSAlan Cox 	case 32:
2594d8d096eSAlan Cox 		break;
2604d8d096eSAlan Cox 	default:
2614d8d096eSAlan Cox 		return -EINVAL;
2624d8d096eSAlan Cox 	}
263c7d73f6aSDaniel Vetter 	drm_helper_mode_fill_fb_struct(&fb->base, mode_cmd);
264c7d73f6aSDaniel Vetter 	fb->gtt = gt;
2654d8d096eSAlan Cox 	ret = drm_framebuffer_init(dev, &fb->base, &psb_fb_funcs);
2664d8d096eSAlan Cox 	if (ret) {
2674d8d096eSAlan Cox 		dev_err(dev->dev, "framebuffer init failed: %d\n", ret);
2684d8d096eSAlan Cox 		return ret;
2694d8d096eSAlan Cox 	}
2704d8d096eSAlan Cox 	return 0;
2714d8d096eSAlan Cox }
2724d8d096eSAlan Cox 
2734d8d096eSAlan Cox /**
2744d8d096eSAlan Cox  *	psb_framebuffer_create	-	create a framebuffer backed by gt
2754d8d096eSAlan Cox  *	@dev: our DRM device
2764d8d096eSAlan Cox  *	@mode_cmd: the description of the requested mode
2774d8d096eSAlan Cox  *	@gt: the backing object
2784d8d096eSAlan Cox  *
2794d8d096eSAlan Cox  *	Create a framebuffer object backed by the gt, and fill in the
2804d8d096eSAlan Cox  *	boilerplate required
2814d8d096eSAlan Cox  *
2824d8d096eSAlan Cox  *	TODO: review object references
2834d8d096eSAlan Cox  */
2844d8d096eSAlan Cox 
2854d8d096eSAlan Cox static struct drm_framebuffer *psb_framebuffer_create
2864d8d096eSAlan Cox 			(struct drm_device *dev,
2871eb83451SVille Syrjälä 			 const struct drm_mode_fb_cmd2 *mode_cmd,
2884d8d096eSAlan Cox 			 struct gtt_range *gt)
2894d8d096eSAlan Cox {
2904d8d096eSAlan Cox 	struct psb_framebuffer *fb;
2914d8d096eSAlan Cox 	int ret;
2924d8d096eSAlan Cox 
2934d8d096eSAlan Cox 	fb = kzalloc(sizeof(*fb), GFP_KERNEL);
2944d8d096eSAlan Cox 	if (!fb)
2954d8d096eSAlan Cox 		return ERR_PTR(-ENOMEM);
2964d8d096eSAlan Cox 
2974d8d096eSAlan Cox 	ret = psb_framebuffer_init(dev, fb, mode_cmd, gt);
2984d8d096eSAlan Cox 	if (ret) {
2994d8d096eSAlan Cox 		kfree(fb);
3004d8d096eSAlan Cox 		return ERR_PTR(ret);
3014d8d096eSAlan Cox 	}
3024d8d096eSAlan Cox 	return &fb->base;
3034d8d096eSAlan Cox }
3044d8d096eSAlan Cox 
3054d8d096eSAlan Cox /**
3064d8d096eSAlan Cox  *	psbfb_alloc		-	allocate frame buffer memory
3074d8d096eSAlan Cox  *	@dev: the DRM device
3084d8d096eSAlan Cox  *	@aligned_size: space needed
309a6ba582dSAlan Cox  *	@force: fall back to GEM buffers if need be
3104d8d096eSAlan Cox  *
3114d8d096eSAlan Cox  *	Allocate the frame buffer. In the usual case we get a GTT range that
3124d8d096eSAlan Cox  *	is stolen memory backed and life is simple. If there isn't sufficient
313dffc9cebSAlan Cox  *	we fail as we don't have the virtual mapping space to really vmap it
314dffc9cebSAlan Cox  *	and the kernel console code can't handle non linear framebuffers.
3154d8d096eSAlan Cox  *
316dffc9cebSAlan Cox  *	Re-address this as and if the framebuffer layer grows this ability.
3174d8d096eSAlan Cox  */
3184d8d096eSAlan Cox static struct gtt_range *psbfb_alloc(struct drm_device *dev, int aligned_size)
3194d8d096eSAlan Cox {
3204d8d096eSAlan Cox 	struct gtt_range *backing;
3214d8d096eSAlan Cox 	/* Begin by trying to use stolen memory backing */
322c269c685SPatrik Jakobsson 	backing = psb_gtt_alloc_range(dev, aligned_size, "fb", 1, PAGE_SIZE);
3234d8d096eSAlan Cox 	if (backing) {
32489c8233fSDavid Herrmann 		drm_gem_private_object_init(dev, &backing->gem, aligned_size);
3254d8d096eSAlan Cox 		return backing;
3264d8d096eSAlan Cox 	}
3274d8d096eSAlan Cox 	return NULL;
3284d8d096eSAlan Cox }
3294d8d096eSAlan Cox 
3304d8d096eSAlan Cox /**
3314d8d096eSAlan Cox  *	psbfb_create		-	create a framebuffer
3324d8d096eSAlan Cox  *	@fbdev: the framebuffer device
3334d8d096eSAlan Cox  *	@sizes: specification of the layout
3344d8d096eSAlan Cox  *
3354d8d096eSAlan Cox  *	Create a framebuffer to the specifications provided
3364d8d096eSAlan Cox  */
3374d8d096eSAlan Cox static int psbfb_create(struct psb_fbdev *fbdev,
3384d8d096eSAlan Cox 				struct drm_fb_helper_surface_size *sizes)
3394d8d096eSAlan Cox {
3404d8d096eSAlan Cox 	struct drm_device *dev = fbdev->psb_fb_helper.dev;
3414d8d096eSAlan Cox 	struct drm_psb_private *dev_priv = dev->dev_private;
3424d8d096eSAlan Cox 	struct fb_info *info;
3434d8d096eSAlan Cox 	struct drm_framebuffer *fb;
3444d8d096eSAlan Cox 	struct psb_framebuffer *psbfb = &fbdev->pfb;
345a9a644acSDave Airlie 	struct drm_mode_fb_cmd2 mode_cmd;
3464d8d096eSAlan Cox 	int size;
3474d8d096eSAlan Cox 	int ret;
3484d8d096eSAlan Cox 	struct gtt_range *backing;
349a9a644acSDave Airlie 	u32 bpp, depth;
3501b223c9eSAlan Cox 	int gtt_roll = 0;
3511b223c9eSAlan Cox 	int pitch_lines = 0;
3524d8d096eSAlan Cox 
3534d8d096eSAlan Cox 	mode_cmd.width = sizes->surface_width;
3544d8d096eSAlan Cox 	mode_cmd.height = sizes->surface_height;
355a9a644acSDave Airlie 	bpp = sizes->surface_bpp;
3566aa1ead1SKirill A. Shutemov 	depth = sizes->surface_depth;
3574d8d096eSAlan Cox 
3584d8d096eSAlan Cox 	/* No 24bit packed */
359a9a644acSDave Airlie 	if (bpp == 24)
360a9a644acSDave Airlie 		bpp = 32;
3614d8d096eSAlan Cox 
3621b223c9eSAlan Cox 	do {
3631b223c9eSAlan Cox 		/*
3641b223c9eSAlan Cox 		 * Acceleration via the GTT requires pitch to be
3651b223c9eSAlan Cox 		 * power of two aligned. Preferably page but less
3661b223c9eSAlan Cox 		 * is ok with some fonts
3671b223c9eSAlan Cox 		 */
3681b223c9eSAlan Cox         	mode_cmd.pitches[0] =  ALIGN(mode_cmd.width * ((bpp + 7) / 8), 4096 >> pitch_lines);
3694d8d096eSAlan Cox 
370a9a644acSDave Airlie         	size = mode_cmd.pitches[0] * mode_cmd.height;
3714d8d096eSAlan Cox         	size = ALIGN(size, PAGE_SIZE);
3724d8d096eSAlan Cox 
3731b223c9eSAlan Cox 		/* Allocate the fb in the GTT with stolen page backing */
374a6ba582dSAlan Cox 		backing = psbfb_alloc(dev, size);
3751b223c9eSAlan Cox 
3761b223c9eSAlan Cox 		if (pitch_lines)
3771b223c9eSAlan Cox 			pitch_lines *= 2;
3781b223c9eSAlan Cox 		else
3791b223c9eSAlan Cox 			pitch_lines = 1;
3801b223c9eSAlan Cox 		gtt_roll++;
3811b223c9eSAlan Cox 	} while (backing == NULL && pitch_lines <= 16);
3821b223c9eSAlan Cox 
3831b223c9eSAlan Cox 	/* The final pitch we accepted if we succeeded */
3841b223c9eSAlan Cox 	pitch_lines /= 2;
3851b223c9eSAlan Cox 
386a6ba582dSAlan Cox 	if (backing == NULL) {
387a6ba582dSAlan Cox 		/*
388a6ba582dSAlan Cox 		 *	We couldn't get the space we wanted, fall back to the
389a6ba582dSAlan Cox 		 *	display engine requirement instead.  The HW requires
390a6ba582dSAlan Cox 		 *	the pitch to be 64 byte aligned
391a6ba582dSAlan Cox 		 */
392a6ba582dSAlan Cox 
393a6ba582dSAlan Cox 		gtt_roll = 0;	/* Don't use GTT accelerated scrolling */
3941b223c9eSAlan Cox 		pitch_lines = 64;
395a6ba582dSAlan Cox 
396a6ba582dSAlan Cox 		mode_cmd.pitches[0] =  ALIGN(mode_cmd.width * ((bpp + 7) / 8), 64);
397a6ba582dSAlan Cox 
398a6ba582dSAlan Cox 		size = mode_cmd.pitches[0] * mode_cmd.height;
399a6ba582dSAlan Cox 		size = ALIGN(size, PAGE_SIZE);
400a6ba582dSAlan Cox 
4014d8d096eSAlan Cox 		/* Allocate the framebuffer in the GTT with stolen page backing */
4024d8d096eSAlan Cox 		backing = psbfb_alloc(dev, size);
4034d8d096eSAlan Cox 		if (backing == NULL)
4044d8d096eSAlan Cox 			return -ENOMEM;
405a6ba582dSAlan Cox 	}
4064d8d096eSAlan Cox 
407bb849779SAlan Cox 	memset(dev_priv->vram_addr + backing->offset, 0, size);
408bb849779SAlan Cox 
409546187c8SArchit Taneja 	info = drm_fb_helper_alloc_fbi(&fbdev->psb_fb_helper);
410546187c8SArchit Taneja 	if (IS_ERR(info)) {
411546187c8SArchit Taneja 		ret = PTR_ERR(info);
4124d8d096eSAlan Cox 		goto out_err1;
4134d8d096eSAlan Cox 	}
4144d8d096eSAlan Cox 	info->par = fbdev;
4154d8d096eSAlan Cox 
416a9a644acSDave Airlie 	mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
417a9a644acSDave Airlie 
4184d8d096eSAlan Cox 	ret = psb_framebuffer_init(dev, psbfb, &mode_cmd, backing);
4194d8d096eSAlan Cox 	if (ret)
4204d8d096eSAlan Cox 		goto out_unref;
4214d8d096eSAlan Cox 
4224d8d096eSAlan Cox 	fb = &psbfb->base;
4234d8d096eSAlan Cox 	psbfb->fbdev = info;
4244d8d096eSAlan Cox 
4254d8d096eSAlan Cox 	fbdev->psb_fb_helper.fb = fb;
4264d8d096eSAlan Cox 
4274578240bSAlan Cox 	drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
428f9d8149aSPatrik Jakobsson 	strcpy(info->fix.id, "psbdrmfb");
4294d8d096eSAlan Cox 
4304d8d096eSAlan Cox 	info->flags = FBINFO_DEFAULT;
4311b223c9eSAlan Cox 	if (dev_priv->ops->accel_2d && pitch_lines > 8)	/* 2D engine */
4321b223c9eSAlan Cox 		info->fbops = &psbfb_ops;
4331b223c9eSAlan Cox 	else if (gtt_roll) {	/* GTT rolling seems best */
434a6ba582dSAlan Cox 		info->fbops = &psbfb_roll_ops;
435a6ba582dSAlan Cox 		info->flags |= FBINFO_HWACCEL_YPAN;
4361b223c9eSAlan Cox 	} else	/* Software */
437a6ba582dSAlan Cox 		info->fbops = &psbfb_unaccel_ops;
4384d8d096eSAlan Cox 
4394d8d096eSAlan Cox 	info->fix.smem_start = dev->mode_config.fb_base;
4404d8d096eSAlan Cox 	info->fix.smem_len = size;
441a6ba582dSAlan Cox 	info->fix.ywrapstep = gtt_roll;
442a6ba582dSAlan Cox 	info->fix.ypanstep = 0;
4434d8d096eSAlan Cox 
4444d8d096eSAlan Cox 	/* Accessed stolen memory directly */
44537214ca0SKirill A. Shutemov 	info->screen_base = dev_priv->vram_addr + backing->offset;
4464d8d096eSAlan Cox 	info->screen_size = size;
4474d8d096eSAlan Cox 
4484d8d096eSAlan Cox 	if (dev_priv->gtt.stolen_size) {
4494d8d096eSAlan Cox 		info->apertures->ranges[0].base = dev->mode_config.fb_base;
4504d8d096eSAlan Cox 		info->apertures->ranges[0].size = dev_priv->gtt.stolen_size;
4514d8d096eSAlan Cox 	}
4524d8d096eSAlan Cox 
4534d8d096eSAlan Cox 	drm_fb_helper_fill_var(info, &fbdev->psb_fb_helper,
4544d8d096eSAlan Cox 				sizes->fb_width, sizes->fb_height);
4554d8d096eSAlan Cox 
4564d8d096eSAlan Cox 	info->fix.mmio_start = pci_resource_start(dev->pdev, 0);
4574d8d096eSAlan Cox 	info->fix.mmio_len = pci_resource_len(dev->pdev, 0);
4584d8d096eSAlan Cox 
459fb2a99e1SSascha Hauer 	/* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
4604d8d096eSAlan Cox 
46131a0685aSAlan Cox 	dev_dbg(dev->dev, "allocated %dx%d fb\n",
4624d8d096eSAlan Cox 					psbfb->base.width, psbfb->base.height);
4634d8d096eSAlan Cox 
4644d8d096eSAlan Cox 	return 0;
4654d8d096eSAlan Cox out_unref:
4664d8d096eSAlan Cox 	if (backing->stolen)
4674d8d096eSAlan Cox 		psb_gtt_free_range(dev, backing);
468dffc9cebSAlan Cox 	else
46946a0f223SDaniel Vetter 		drm_gem_object_unreference_unlocked(&backing->gem);
470546187c8SArchit Taneja 
471546187c8SArchit Taneja 	drm_fb_helper_release_fbi(&fbdev->psb_fb_helper);
4724d8d096eSAlan Cox out_err1:
4734d8d096eSAlan Cox 	psb_gtt_free_range(dev, backing);
4744d8d096eSAlan Cox 	return ret;
4754d8d096eSAlan Cox }
4764d8d096eSAlan Cox 
4774d8d096eSAlan Cox /**
4784d8d096eSAlan Cox  *	psb_user_framebuffer_create	-	create framebuffer
4794d8d096eSAlan Cox  *	@dev: our DRM device
4804d8d096eSAlan Cox  *	@filp: client file
4814d8d096eSAlan Cox  *	@cmd: mode request
4824d8d096eSAlan Cox  *
4834d8d096eSAlan Cox  *	Create a new framebuffer backed by a userspace GEM object
4844d8d096eSAlan Cox  */
4854d8d096eSAlan Cox static struct drm_framebuffer *psb_user_framebuffer_create
4864d8d096eSAlan Cox 			(struct drm_device *dev, struct drm_file *filp,
4871eb83451SVille Syrjälä 			 const struct drm_mode_fb_cmd2 *cmd)
4884d8d096eSAlan Cox {
4894d8d096eSAlan Cox 	struct gtt_range *r;
4904d8d096eSAlan Cox 	struct drm_gem_object *obj;
4914d8d096eSAlan Cox 
4924d8d096eSAlan Cox 	/*
4934d8d096eSAlan Cox 	 *	Find the GEM object and thus the gtt range object that is
4944d8d096eSAlan Cox 	 *	to back this space
4954d8d096eSAlan Cox 	 */
496a9a644acSDave Airlie 	obj = drm_gem_object_lookup(dev, filp, cmd->handles[0]);
4974d8d096eSAlan Cox 	if (obj == NULL)
4984d8d096eSAlan Cox 		return ERR_PTR(-ENOENT);
4994d8d096eSAlan Cox 
5004d8d096eSAlan Cox 	/* Let the core code do all the work */
5014d8d096eSAlan Cox 	r = container_of(obj, struct gtt_range, gem);
5024d8d096eSAlan Cox 	return psb_framebuffer_create(dev, cmd, r);
5034d8d096eSAlan Cox }
5044d8d096eSAlan Cox 
5054d8d096eSAlan Cox static void psbfb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
5064d8d096eSAlan Cox 							u16 blue, int regno)
5074d8d096eSAlan Cox {
5086306865dSPatrik Jakobsson 	struct gma_crtc *gma_crtc = to_gma_crtc(crtc);
5093df546beSAlan Cox 
5106306865dSPatrik Jakobsson 	gma_crtc->lut_r[regno] = red >> 8;
5116306865dSPatrik Jakobsson 	gma_crtc->lut_g[regno] = green >> 8;
5126306865dSPatrik Jakobsson 	gma_crtc->lut_b[regno] = blue >> 8;
5134d8d096eSAlan Cox }
5144d8d096eSAlan Cox 
5154d8d096eSAlan Cox static void psbfb_gamma_get(struct drm_crtc *crtc, u16 *red,
5164d8d096eSAlan Cox 					u16 *green, u16 *blue, int regno)
5174d8d096eSAlan Cox {
5186306865dSPatrik Jakobsson 	struct gma_crtc *gma_crtc = to_gma_crtc(crtc);
5193df546beSAlan Cox 
5206306865dSPatrik Jakobsson 	*red = gma_crtc->lut_r[regno] << 8;
5216306865dSPatrik Jakobsson 	*green = gma_crtc->lut_g[regno] << 8;
5226306865dSPatrik Jakobsson 	*blue = gma_crtc->lut_b[regno] << 8;
5234d8d096eSAlan Cox }
5244d8d096eSAlan Cox 
5254d8d096eSAlan Cox static int psbfb_probe(struct drm_fb_helper *helper,
5264d8d096eSAlan Cox 				struct drm_fb_helper_surface_size *sizes)
5274d8d096eSAlan Cox {
528c39aa6a1SFabian Frederick 	struct psb_fbdev *psb_fbdev =
529c39aa6a1SFabian Frederick 		container_of(helper, struct psb_fbdev, psb_fb_helper);
5303aad16d2SAlan Cox 	struct drm_device *dev = psb_fbdev->psb_fb_helper.dev;
5313aad16d2SAlan Cox 	struct drm_psb_private *dev_priv = dev->dev_private;
5323aad16d2SAlan Cox 	int bytespp;
5334d8d096eSAlan Cox 
5343aad16d2SAlan Cox 	bytespp = sizes->surface_bpp / 8;
5353aad16d2SAlan Cox 	if (bytespp == 3)	/* no 24bit packed */
5363aad16d2SAlan Cox 		bytespp = 4;
5373aad16d2SAlan Cox 
5383aad16d2SAlan Cox 	/* If the mode will not fit in 32bit then switch to 16bit to get
5393aad16d2SAlan Cox 	   a console on full resolution. The X mode setting server will
5403aad16d2SAlan Cox 	   allocate its own 32bit GEM framebuffer */
5413aad16d2SAlan Cox 	if (ALIGN(sizes->fb_width * bytespp, 64) * sizes->fb_height >
5423aad16d2SAlan Cox 	                dev_priv->vram_stolen_size) {
5433aad16d2SAlan Cox                 sizes->surface_bpp = 16;
5443aad16d2SAlan Cox                 sizes->surface_depth = 16;
5453aad16d2SAlan Cox         }
5463aad16d2SAlan Cox 
547cd5428a5SDaniel Vetter 	return psbfb_create(psb_fbdev, sizes);
5484d8d096eSAlan Cox }
5494d8d096eSAlan Cox 
5503a493879SThierry Reding static const struct drm_fb_helper_funcs psb_fb_helper_funcs = {
5514d8d096eSAlan Cox 	.gamma_set = psbfb_gamma_set,
5524d8d096eSAlan Cox 	.gamma_get = psbfb_gamma_get,
5534d8d096eSAlan Cox 	.fb_probe = psbfb_probe,
5544d8d096eSAlan Cox };
5554d8d096eSAlan Cox 
556bc7f2b08SKirill A. Shutemov static int psb_fbdev_destroy(struct drm_device *dev, struct psb_fbdev *fbdev)
5574d8d096eSAlan Cox {
5584d8d096eSAlan Cox 	struct psb_framebuffer *psbfb = &fbdev->pfb;
5594d8d096eSAlan Cox 
560546187c8SArchit Taneja 	drm_fb_helper_unregister_fbi(&fbdev->psb_fb_helper);
561546187c8SArchit Taneja 	drm_fb_helper_release_fbi(&fbdev->psb_fb_helper);
562546187c8SArchit Taneja 
5634d8d096eSAlan Cox 	drm_fb_helper_fini(&fbdev->psb_fb_helper);
56436206361SDaniel Vetter 	drm_framebuffer_unregister_private(&psbfb->base);
5654d8d096eSAlan Cox 	drm_framebuffer_cleanup(&psbfb->base);
5664d8d096eSAlan Cox 
5674d8d096eSAlan Cox 	if (psbfb->gtt)
56846a0f223SDaniel Vetter 		drm_gem_object_unreference_unlocked(&psbfb->gtt->gem);
5694d8d096eSAlan Cox 	return 0;
5704d8d096eSAlan Cox }
5714d8d096eSAlan Cox 
5724d8d096eSAlan Cox int psb_fbdev_init(struct drm_device *dev)
5734d8d096eSAlan Cox {
5744d8d096eSAlan Cox 	struct psb_fbdev *fbdev;
5754d8d096eSAlan Cox 	struct drm_psb_private *dev_priv = dev->dev_private;
57601934c2aSThierry Reding 	int ret;
5774d8d096eSAlan Cox 
5784d8d096eSAlan Cox 	fbdev = kzalloc(sizeof(struct psb_fbdev), GFP_KERNEL);
5794d8d096eSAlan Cox 	if (!fbdev) {
5804d8d096eSAlan Cox 		dev_err(dev->dev, "no memory\n");
5814d8d096eSAlan Cox 		return -ENOMEM;
5824d8d096eSAlan Cox 	}
5834d8d096eSAlan Cox 
5844d8d096eSAlan Cox 	dev_priv->fbdev = fbdev;
58510a23102SThierry Reding 
58610a23102SThierry Reding 	drm_fb_helper_prepare(dev, &fbdev->psb_fb_helper, &psb_fb_helper_funcs);
5874d8d096eSAlan Cox 
58801934c2aSThierry Reding 	ret = drm_fb_helper_init(dev, &fbdev->psb_fb_helper,
58901934c2aSThierry Reding 				 dev_priv->ops->crtcs, INTELFB_CONN_LIMIT);
59001934c2aSThierry Reding 	if (ret)
59101934c2aSThierry Reding 		goto free;
5924d8d096eSAlan Cox 
59301934c2aSThierry Reding 	ret = drm_fb_helper_single_add_all_connectors(&fbdev->psb_fb_helper);
59401934c2aSThierry Reding 	if (ret)
59501934c2aSThierry Reding 		goto fini;
59676a39dbfSDaniel Vetter 
59776a39dbfSDaniel Vetter 	/* disable all the possible outputs/crtcs before entering KMS mode */
59876a39dbfSDaniel Vetter 	drm_helper_disable_unused_functions(dev);
59976a39dbfSDaniel Vetter 
60001934c2aSThierry Reding 	ret = drm_fb_helper_initial_config(&fbdev->psb_fb_helper, 32);
60101934c2aSThierry Reding 	if (ret)
60201934c2aSThierry Reding 		goto fini;
60301934c2aSThierry Reding 
6044d8d096eSAlan Cox 	return 0;
60501934c2aSThierry Reding 
60601934c2aSThierry Reding fini:
60701934c2aSThierry Reding 	drm_fb_helper_fini(&fbdev->psb_fb_helper);
60801934c2aSThierry Reding free:
60901934c2aSThierry Reding 	kfree(fbdev);
61001934c2aSThierry Reding 	return ret;
6114d8d096eSAlan Cox }
6124d8d096eSAlan Cox 
613bc7f2b08SKirill A. Shutemov static void psb_fbdev_fini(struct drm_device *dev)
6144d8d096eSAlan Cox {
6154d8d096eSAlan Cox 	struct drm_psb_private *dev_priv = dev->dev_private;
6164d8d096eSAlan Cox 
6174d8d096eSAlan Cox 	if (!dev_priv->fbdev)
6184d8d096eSAlan Cox 		return;
6194d8d096eSAlan Cox 
6204d8d096eSAlan Cox 	psb_fbdev_destroy(dev, dev_priv->fbdev);
6214d8d096eSAlan Cox 	kfree(dev_priv->fbdev);
6224d8d096eSAlan Cox 	dev_priv->fbdev = NULL;
6234d8d096eSAlan Cox }
6244d8d096eSAlan Cox 
6254d8d096eSAlan Cox static void psbfb_output_poll_changed(struct drm_device *dev)
6264d8d096eSAlan Cox {
6274d8d096eSAlan Cox 	struct drm_psb_private *dev_priv = dev->dev_private;
6284d8d096eSAlan Cox 	struct psb_fbdev *fbdev = (struct psb_fbdev *)dev_priv->fbdev;
6294d8d096eSAlan Cox 	drm_fb_helper_hotplug_event(&fbdev->psb_fb_helper);
6304d8d096eSAlan Cox }
6314d8d096eSAlan Cox 
6324d8d096eSAlan Cox /**
6334d8d096eSAlan Cox  *	psb_user_framebuffer_create_handle - add hamdle to a framebuffer
6344d8d096eSAlan Cox  *	@fb: framebuffer
6354d8d096eSAlan Cox  *	@file_priv: our DRM file
6364d8d096eSAlan Cox  *	@handle: returned handle
6374d8d096eSAlan Cox  *
6384d8d096eSAlan Cox  *	Our framebuffer object is a GTT range which also contains a GEM
6394d8d096eSAlan Cox  *	object. We need to turn it into a handle for userspace. GEM will do
6404d8d096eSAlan Cox  *	the work for us
6414d8d096eSAlan Cox  */
6424d8d096eSAlan Cox static int psb_user_framebuffer_create_handle(struct drm_framebuffer *fb,
6434d8d096eSAlan Cox 					      struct drm_file *file_priv,
6444d8d096eSAlan Cox 					      unsigned int *handle)
6454d8d096eSAlan Cox {
6464d8d096eSAlan Cox 	struct psb_framebuffer *psbfb = to_psb_fb(fb);
6474d8d096eSAlan Cox 	struct gtt_range *r = psbfb->gtt;
6484d8d096eSAlan Cox 	return drm_gem_handle_create(file_priv, &r->gem, handle);
6494d8d096eSAlan Cox }
6504d8d096eSAlan Cox 
6514d8d096eSAlan Cox /**
6524d8d096eSAlan Cox  *	psb_user_framebuffer_destroy	-	destruct user created fb
6534d8d096eSAlan Cox  *	@fb: framebuffer
6544d8d096eSAlan Cox  *
6554d8d096eSAlan Cox  *	User framebuffers are backed by GEM objects so all we have to do is
6564d8d096eSAlan Cox  *	clean up a bit and drop the reference, GEM will handle the fallout
6574d8d096eSAlan Cox  */
6584d8d096eSAlan Cox static void psb_user_framebuffer_destroy(struct drm_framebuffer *fb)
6594d8d096eSAlan Cox {
6604d8d096eSAlan Cox 	struct psb_framebuffer *psbfb = to_psb_fb(fb);
6614d8d096eSAlan Cox 	struct gtt_range *r = psbfb->gtt;
6624d8d096eSAlan Cox 
6634d8d096eSAlan Cox 	/* Let DRM do its clean up */
6644d8d096eSAlan Cox 	drm_framebuffer_cleanup(fb);
6654d8d096eSAlan Cox 	/*  We are no longer using the resource in GEM */
6664d8d096eSAlan Cox 	drm_gem_object_unreference_unlocked(&r->gem);
6674d8d096eSAlan Cox 	kfree(fb);
6684d8d096eSAlan Cox }
6694d8d096eSAlan Cox 
6704d8d096eSAlan Cox static const struct drm_mode_config_funcs psb_mode_funcs = {
6714d8d096eSAlan Cox 	.fb_create = psb_user_framebuffer_create,
6724d8d096eSAlan Cox 	.output_poll_changed = psbfb_output_poll_changed,
6734d8d096eSAlan Cox };
6744d8d096eSAlan Cox 
6754d8d096eSAlan Cox static void psb_setup_outputs(struct drm_device *dev)
6764d8d096eSAlan Cox {
6774d8d096eSAlan Cox 	struct drm_psb_private *dev_priv = dev->dev_private;
6784d8d096eSAlan Cox 	struct drm_connector *connector;
6794d8d096eSAlan Cox 
6804d8d096eSAlan Cox 	drm_mode_create_scaling_mode_property(dev);
6814d8d096eSAlan Cox 
68213619ce5SAlan Cox 	/* It is ok for this to fail - we just don't get backlight control */
68313619ce5SAlan Cox 	if (!dev_priv->backlight_property)
68413619ce5SAlan Cox 		dev_priv->backlight_property = drm_property_create_range(dev, 0,
68513619ce5SAlan Cox 							"backlight", 0, 100);
6864d8d096eSAlan Cox 	dev_priv->ops->output_init(dev);
6874d8d096eSAlan Cox 
6884d8d096eSAlan Cox 	list_for_each_entry(connector, &dev->mode_config.connector_list,
6894d8d096eSAlan Cox 			    head) {
690367e4408SPatrik Jakobsson 		struct gma_encoder *gma_encoder = gma_attached_encoder(connector);
691367e4408SPatrik Jakobsson 		struct drm_encoder *encoder = &gma_encoder->base;
6924d8d096eSAlan Cox 		int crtc_mask = 0, clone_mask = 0;
6934d8d096eSAlan Cox 
6944d8d096eSAlan Cox 		/* valid crtcs */
695367e4408SPatrik Jakobsson 		switch (gma_encoder->type) {
6964d8d096eSAlan Cox 		case INTEL_OUTPUT_ANALOG:
6974d8d096eSAlan Cox 			crtc_mask = (1 << 0);
6984d8d096eSAlan Cox 			clone_mask = (1 << INTEL_OUTPUT_ANALOG);
6994d8d096eSAlan Cox 			break;
7004d8d096eSAlan Cox 		case INTEL_OUTPUT_SDVO:
701cf8efd3aSPatrik Jakobsson 			crtc_mask = dev_priv->ops->sdvo_mask;
7024d8d096eSAlan Cox 			clone_mask = (1 << INTEL_OUTPUT_SDVO);
7034d8d096eSAlan Cox 			break;
7044d8d096eSAlan Cox 		case INTEL_OUTPUT_LVDS:
705d235e64aSAlan Cox 		        crtc_mask = dev_priv->ops->lvds_mask;
7064d8d096eSAlan Cox 			clone_mask = (1 << INTEL_OUTPUT_LVDS);
7074d8d096eSAlan Cox 			break;
7084d8d096eSAlan Cox 		case INTEL_OUTPUT_MIPI:
7094d8d096eSAlan Cox 			crtc_mask = (1 << 0);
7104d8d096eSAlan Cox 			clone_mask = (1 << INTEL_OUTPUT_MIPI);
7114d8d096eSAlan Cox 			break;
7124d8d096eSAlan Cox 		case INTEL_OUTPUT_MIPI2:
7134d8d096eSAlan Cox 			crtc_mask = (1 << 2);
7144d8d096eSAlan Cox 			clone_mask = (1 << INTEL_OUTPUT_MIPI2);
7154d8d096eSAlan Cox 			break;
7164d8d096eSAlan Cox 		case INTEL_OUTPUT_HDMI:
717d235e64aSAlan Cox 		        crtc_mask = dev_priv->ops->hdmi_mask;
7184d8d096eSAlan Cox 			clone_mask = (1 << INTEL_OUTPUT_HDMI);
7194d8d096eSAlan Cox 			break;
720220801bdSAlan Cox 		case INTEL_OUTPUT_DISPLAYPORT:
721220801bdSAlan Cox 			crtc_mask = (1 << 0) | (1 << 1);
722220801bdSAlan Cox 			clone_mask = (1 << INTEL_OUTPUT_DISPLAYPORT);
723220801bdSAlan Cox 			break;
724d112a816SZhao Yakui 		case INTEL_OUTPUT_EDP:
725d112a816SZhao Yakui 			crtc_mask = (1 << 1);
726d112a816SZhao Yakui 			clone_mask = (1 << INTEL_OUTPUT_EDP);
7274d8d096eSAlan Cox 		}
7284d8d096eSAlan Cox 		encoder->possible_crtcs = crtc_mask;
7294d8d096eSAlan Cox 		encoder->possible_clones =
730a3d5d75fSPatrik Jakobsson 		    gma_connector_clones(dev, clone_mask);
7314d8d096eSAlan Cox 	}
7324d8d096eSAlan Cox }
7334d8d096eSAlan Cox 
7344d8d096eSAlan Cox void psb_modeset_init(struct drm_device *dev)
7354d8d096eSAlan Cox {
7364d8d096eSAlan Cox 	struct drm_psb_private *dev_priv = dev->dev_private;
7374d8d096eSAlan Cox 	struct psb_intel_mode_device *mode_dev = &dev_priv->mode_dev;
7384d8d096eSAlan Cox 	int i;
7394d8d096eSAlan Cox 
7404d8d096eSAlan Cox 	drm_mode_config_init(dev);
7414d8d096eSAlan Cox 
7424d8d096eSAlan Cox 	dev->mode_config.min_width = 0;
7434d8d096eSAlan Cox 	dev->mode_config.min_height = 0;
7444d8d096eSAlan Cox 
745e6ecefaaSLaurent Pinchart 	dev->mode_config.funcs = &psb_mode_funcs;
7464d8d096eSAlan Cox 
7474d8d096eSAlan Cox 	/* set memory base */
748dffc9cebSAlan Cox 	/* Oaktrail and Poulsbo should use BAR 2*/
7494d8d096eSAlan Cox 	pci_read_config_dword(dev->pdev, PSB_BSM, (u32 *)
7504d8d096eSAlan Cox 					&(dev->mode_config.fb_base));
7514d8d096eSAlan Cox 
7524d8d096eSAlan Cox 	/* num pipes is 2 for PSB but 1 for Mrst */
7534d8d096eSAlan Cox 	for (i = 0; i < dev_priv->num_pipe; i++)
7544d8d096eSAlan Cox 		psb_intel_crtc_init(dev, i, mode_dev);
7554d8d096eSAlan Cox 
756cbbd379aSPatrik Jakobsson 	dev->mode_config.max_width = 4096;
757cbbd379aSPatrik Jakobsson 	dev->mode_config.max_height = 4096;
7584d8d096eSAlan Cox 
7594d8d096eSAlan Cox 	psb_setup_outputs(dev);
760d235e64aSAlan Cox 
761d235e64aSAlan Cox 	if (dev_priv->ops->errata)
762d235e64aSAlan Cox 	        dev_priv->ops->errata(dev);
7634ab2c7f1SAlan Cox 
7644ab2c7f1SAlan Cox         dev_priv->modeset = true;
7654d8d096eSAlan Cox }
7664d8d096eSAlan Cox 
7674d8d096eSAlan Cox void psb_modeset_cleanup(struct drm_device *dev)
7684d8d096eSAlan Cox {
7694ab2c7f1SAlan Cox 	struct drm_psb_private *dev_priv = dev->dev_private;
7704ab2c7f1SAlan Cox 	if (dev_priv->modeset) {
7714d8d096eSAlan Cox 		drm_kms_helper_poll_fini(dev);
7724d8d096eSAlan Cox 		psb_fbdev_fini(dev);
7734d8d096eSAlan Cox 		drm_mode_config_cleanup(dev);
7744d8d096eSAlan Cox 	}
7754ab2c7f1SAlan Cox }
776