1a61127c2SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
24d8d096eSAlan Cox /**************************************************************************
34d8d096eSAlan Cox  * Copyright (c) 2007-2011, Intel Corporation.
44d8d096eSAlan Cox  * All Rights Reserved.
54d8d096eSAlan Cox  *
64d8d096eSAlan Cox  **************************************************************************/
74d8d096eSAlan Cox 
84d8d096eSAlan Cox #include <linux/console.h>
90c7b178aSSam Ravnborg #include <linux/delay.h>
100c7b178aSSam Ravnborg #include <linux/errno.h>
110c7b178aSSam Ravnborg #include <linux/init.h>
120c7b178aSSam Ravnborg #include <linux/kernel.h>
130c7b178aSSam Ravnborg #include <linux/mm.h>
140c7b178aSSam Ravnborg #include <linux/module.h>
150c7b178aSSam Ravnborg #include <linux/pfn_t.h>
160c7b178aSSam Ravnborg #include <linux/slab.h>
170c7b178aSSam Ravnborg #include <linux/string.h>
180c7b178aSSam Ravnborg #include <linux/tty.h>
194d8d096eSAlan Cox 
204d8d096eSAlan Cox #include <drm/drm.h>
214d8d096eSAlan Cox #include <drm/drm_crtc.h>
22a9a644acSDave Airlie #include <drm/drm_fb_helper.h>
230c7b178aSSam Ravnborg #include <drm/drm_fourcc.h>
24bc61c975SDaniel Stone #include <drm/drm_gem_framebuffer_helper.h>
254d8d096eSAlan Cox 
264d8d096eSAlan Cox #include "framebuffer.h"
27*7cd467d0SThomas Zimmermann #include "gem.h"
28a6ba582dSAlan Cox #include "gtt.h"
290c7b178aSSam Ravnborg #include "psb_drv.h"
300c7b178aSSam Ravnborg #include "psb_intel_drv.h"
310c7b178aSSam Ravnborg #include "psb_intel_reg.h"
324d8d096eSAlan Cox 
334d8d096eSAlan Cox static const struct drm_framebuffer_funcs psb_fb_funcs = {
34bc61c975SDaniel Stone 	.destroy = drm_gem_fb_destroy,
35bc61c975SDaniel Stone 	.create_handle = drm_gem_fb_create_handle,
364d8d096eSAlan Cox };
374d8d096eSAlan Cox 
384d8d096eSAlan Cox #define CMAP_TOHW(_val, _width) ((((_val) << (_width)) + 0x7FFF - (_val)) >> 16)
394d8d096eSAlan Cox 
404d8d096eSAlan Cox static int psbfb_setcolreg(unsigned regno, unsigned red, unsigned green,
414d8d096eSAlan Cox 			   unsigned blue, unsigned transp,
424d8d096eSAlan Cox 			   struct fb_info *info)
434d8d096eSAlan Cox {
446b7ce2c4SThomas Zimmermann 	struct drm_fb_helper *fb_helper = info->par;
456b7ce2c4SThomas Zimmermann 	struct drm_framebuffer *fb = fb_helper->fb;
464d8d096eSAlan Cox 	uint32_t v;
474d8d096eSAlan Cox 
484d8d096eSAlan Cox 	if (!fb)
494d8d096eSAlan Cox 		return -ENOMEM;
504d8d096eSAlan Cox 
514d8d096eSAlan Cox 	if (regno > 255)
524d8d096eSAlan Cox 		return 1;
534d8d096eSAlan Cox 
544d8d096eSAlan Cox 	red = CMAP_TOHW(red, info->var.red.length);
554d8d096eSAlan Cox 	blue = CMAP_TOHW(blue, info->var.blue.length);
564d8d096eSAlan Cox 	green = CMAP_TOHW(green, info->var.green.length);
574d8d096eSAlan Cox 	transp = CMAP_TOHW(transp, info->var.transp.length);
584d8d096eSAlan Cox 
594d8d096eSAlan Cox 	v = (red << info->var.red.offset) |
604d8d096eSAlan Cox 	    (green << info->var.green.offset) |
614d8d096eSAlan Cox 	    (blue << info->var.blue.offset) |
624d8d096eSAlan Cox 	    (transp << info->var.transp.offset);
634d8d096eSAlan Cox 
644d8d096eSAlan Cox 	if (regno < 16) {
65272725c7SVille Syrjälä 		switch (fb->format->cpp[0] * 8) {
664d8d096eSAlan Cox 		case 16:
674d8d096eSAlan Cox 			((uint32_t *) info->pseudo_palette)[regno] = v;
684d8d096eSAlan Cox 			break;
694d8d096eSAlan Cox 		case 24:
704d8d096eSAlan Cox 		case 32:
714d8d096eSAlan Cox 			((uint32_t *) info->pseudo_palette)[regno] = v;
724d8d096eSAlan Cox 			break;
734d8d096eSAlan Cox 		}
744d8d096eSAlan Cox 	}
754d8d096eSAlan Cox 
764d8d096eSAlan Cox 	return 0;
774d8d096eSAlan Cox }
784d8d096eSAlan Cox 
79a6ba582dSAlan Cox static int psbfb_pan(struct fb_var_screeninfo *var, struct fb_info *info)
80a6ba582dSAlan Cox {
816b7ce2c4SThomas Zimmermann 	struct drm_fb_helper *fb_helper = info->par;
826b7ce2c4SThomas Zimmermann 	struct drm_framebuffer *fb = fb_helper->fb;
83e18da8ceSThomas Zimmermann 	struct drm_device *dev = fb->dev;
84e18da8ceSThomas Zimmermann 	struct gtt_range *gtt = to_gtt_range(fb->obj[0]);
85a6ba582dSAlan Cox 
86a6ba582dSAlan Cox 	/*
87a6ba582dSAlan Cox 	 *	We have to poke our nose in here. The core fb code assumes
88a6ba582dSAlan Cox 	 *	panning is part of the hardware that can be invoked before
89a6ba582dSAlan Cox 	 *	the actual fb is mapped. In our case that isn't quite true.
90a6ba582dSAlan Cox 	 */
91bc61c975SDaniel Stone 	if (gtt->npage) {
92a6ba582dSAlan Cox 		/* GTT roll shifts in 4K pages, we need to shift the right
93a6ba582dSAlan Cox 		   number of pages */
94a6ba582dSAlan Cox 		int pages = info->fix.line_length >> 12;
95bc61c975SDaniel Stone 		psb_gtt_roll(dev, gtt, var->yoffset * pages);
96a6ba582dSAlan Cox 	}
97a6ba582dSAlan Cox         return 0;
98a6ba582dSAlan Cox }
994d8d096eSAlan Cox 
1000edf6813SSouptick Joarder static vm_fault_t psbfb_vm_fault(struct vm_fault *vmf)
1014d8d096eSAlan Cox {
10211bac800SDave Jiang 	struct vm_area_struct *vma = vmf->vma;
103e18da8ceSThomas Zimmermann 	struct drm_framebuffer *fb = vma->vm_private_data;
104e18da8ceSThomas Zimmermann 	struct drm_device *dev = fb->dev;
1054d8d096eSAlan Cox 	struct drm_psb_private *dev_priv = dev->dev_private;
106e18da8ceSThomas Zimmermann 	struct gtt_range *gtt = to_gtt_range(fb->obj[0]);
1074d8d096eSAlan Cox 	int page_num;
1084d8d096eSAlan Cox 	int i;
1094d8d096eSAlan Cox 	unsigned long address;
1100edf6813SSouptick Joarder 	vm_fault_t ret = VM_FAULT_SIGBUS;
1114d8d096eSAlan Cox 	unsigned long pfn;
11261bb3feaSPatrik Jakobsson 	unsigned long phys_addr = (unsigned long)dev_priv->stolen_base +
113bc61c975SDaniel Stone 				  gtt->offset;
1144d8d096eSAlan Cox 
115024b6a63SShyam Saini 	page_num = vma_pages(vma);
1161a29d85eSJan Kara 	address = vmf->address - (vmf->pgoff << PAGE_SHIFT);
1174d8d096eSAlan Cox 
1184d8d096eSAlan Cox 	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1194d8d096eSAlan Cox 
1204d8d096eSAlan Cox 	for (i = 0; i < page_num; i++) {
1214d8d096eSAlan Cox 		pfn = (phys_addr >> PAGE_SHIFT);
1224d8d096eSAlan Cox 
1230edf6813SSouptick Joarder 		ret = vmf_insert_mixed(vma, address,
12401c8f1c4SDan Williams 				__pfn_to_pfn_t(pfn, PFN_DEV));
1250edf6813SSouptick Joarder 		if (unlikely(ret & VM_FAULT_ERROR))
1264d8d096eSAlan Cox 			break;
1274d8d096eSAlan Cox 		address += PAGE_SIZE;
1284d8d096eSAlan Cox 		phys_addr += PAGE_SIZE;
1294d8d096eSAlan Cox 	}
1300edf6813SSouptick Joarder 	return ret;
1314d8d096eSAlan Cox }
1324d8d096eSAlan Cox 
1334d8d096eSAlan Cox static void psbfb_vm_open(struct vm_area_struct *vma)
1344d8d096eSAlan Cox {
1354d8d096eSAlan Cox }
1364d8d096eSAlan Cox 
1374d8d096eSAlan Cox static void psbfb_vm_close(struct vm_area_struct *vma)
1384d8d096eSAlan Cox {
1394d8d096eSAlan Cox }
1404d8d096eSAlan Cox 
14178b68556SLaurent Pinchart static const struct vm_operations_struct psbfb_vm_ops = {
1424d8d096eSAlan Cox 	.fault	= psbfb_vm_fault,
1434d8d096eSAlan Cox 	.open	= psbfb_vm_open,
1444d8d096eSAlan Cox 	.close	= psbfb_vm_close
1454d8d096eSAlan Cox };
1464d8d096eSAlan Cox 
1474d8d096eSAlan Cox static int psbfb_mmap(struct fb_info *info, struct vm_area_struct *vma)
1484d8d096eSAlan Cox {
1496b7ce2c4SThomas Zimmermann 	struct drm_fb_helper *fb_helper = info->par;
1506b7ce2c4SThomas Zimmermann 	struct drm_framebuffer *fb = fb_helper->fb;
1514d8d096eSAlan Cox 
1524d8d096eSAlan Cox 	if (vma->vm_pgoff != 0)
1534d8d096eSAlan Cox 		return -EINVAL;
1544d8d096eSAlan Cox 	if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
1554d8d096eSAlan Cox 		return -EINVAL;
1564d8d096eSAlan Cox 
1574d8d096eSAlan Cox 	/*
1584d8d096eSAlan Cox 	 * If this is a GEM object then info->screen_base is the virtual
1594d8d096eSAlan Cox 	 * kernel remapping of the object. FIXME: Review if this is
1604d8d096eSAlan Cox 	 * suitable for our mmap work
1614d8d096eSAlan Cox 	 */
1624d8d096eSAlan Cox 	vma->vm_ops = &psbfb_vm_ops;
163e18da8ceSThomas Zimmermann 	vma->vm_private_data = (void *)fb;
164314e51b9SKonstantin Khlebnikov 	vma->vm_flags |= VM_IO | VM_MIXEDMAP | VM_DONTEXPAND | VM_DONTDUMP;
1654d8d096eSAlan Cox 	return 0;
1664d8d096eSAlan Cox }
1674d8d096eSAlan Cox 
168c08503ecSRikard Falkeborn static const struct fb_ops psbfb_ops = {
1694d8d096eSAlan Cox 	.owner = THIS_MODULE,
1703da6c2f3SStefan Christ 	DRM_FB_HELPER_DEFAULT_OPS,
1714d8d096eSAlan Cox 	.fb_setcolreg = psbfb_setcolreg,
172546187c8SArchit Taneja 	.fb_fillrect = drm_fb_helper_cfb_fillrect,
1734d8d096eSAlan Cox 	.fb_copyarea = psbfb_copyarea,
174546187c8SArchit Taneja 	.fb_imageblit = drm_fb_helper_cfb_imageblit,
1754d8d096eSAlan Cox 	.fb_mmap = psbfb_mmap,
1764d8d096eSAlan Cox 	.fb_sync = psbfb_sync,
1774d8d096eSAlan Cox };
1784d8d096eSAlan Cox 
179c08503ecSRikard Falkeborn static const struct fb_ops psbfb_roll_ops = {
180a6ba582dSAlan Cox 	.owner = THIS_MODULE,
1813da6c2f3SStefan Christ 	DRM_FB_HELPER_DEFAULT_OPS,
182a6ba582dSAlan Cox 	.fb_setcolreg = psbfb_setcolreg,
183546187c8SArchit Taneja 	.fb_fillrect = drm_fb_helper_cfb_fillrect,
184546187c8SArchit Taneja 	.fb_copyarea = drm_fb_helper_cfb_copyarea,
185546187c8SArchit Taneja 	.fb_imageblit = drm_fb_helper_cfb_imageblit,
186a6ba582dSAlan Cox 	.fb_pan_display = psbfb_pan,
187a6ba582dSAlan Cox 	.fb_mmap = psbfb_mmap,
188a6ba582dSAlan Cox };
189a6ba582dSAlan Cox 
190c08503ecSRikard Falkeborn static const struct fb_ops psbfb_unaccel_ops = {
1914d8d096eSAlan Cox 	.owner = THIS_MODULE,
1923da6c2f3SStefan Christ 	DRM_FB_HELPER_DEFAULT_OPS,
1934d8d096eSAlan Cox 	.fb_setcolreg = psbfb_setcolreg,
194546187c8SArchit Taneja 	.fb_fillrect = drm_fb_helper_cfb_fillrect,
195546187c8SArchit Taneja 	.fb_copyarea = drm_fb_helper_cfb_copyarea,
196546187c8SArchit Taneja 	.fb_imageblit = drm_fb_helper_cfb_imageblit,
1974d8d096eSAlan Cox 	.fb_mmap = psbfb_mmap,
1984d8d096eSAlan Cox };
1994d8d096eSAlan Cox 
2004d8d096eSAlan Cox /**
2014d8d096eSAlan Cox  *	psb_framebuffer_init	-	initialize a framebuffer
2024d8d096eSAlan Cox  *	@dev: our DRM device
2034d8d096eSAlan Cox  *	@fb: framebuffer to set up
2044d8d096eSAlan Cox  *	@mode_cmd: mode description
2054d8d096eSAlan Cox  *	@gt: backing object
2064d8d096eSAlan Cox  *
2074d8d096eSAlan Cox  *	Configure and fill in the boilerplate for our frame buffer. Return
2084d8d096eSAlan Cox  *	0 on success or an error code if we fail.
2094d8d096eSAlan Cox  */
2104d8d096eSAlan Cox static int psb_framebuffer_init(struct drm_device *dev,
211e18da8ceSThomas Zimmermann 					struct drm_framebuffer *fb,
2121eb83451SVille Syrjälä 					const struct drm_mode_fb_cmd2 *mode_cmd,
2130471c9f5SThomas Zimmermann 					struct drm_gem_object *obj)
2144d8d096eSAlan Cox {
215e0f9a4abSLaurent Pinchart 	const struct drm_format_info *info;
2164d8d096eSAlan Cox 	int ret;
2174d8d096eSAlan Cox 
218e0f9a4abSLaurent Pinchart 	/*
219e0f9a4abSLaurent Pinchart 	 * Reject unknown formats, YUV formats, and formats with more than
220e0f9a4abSLaurent Pinchart 	 * 4 bytes per pixel.
221e0f9a4abSLaurent Pinchart 	 */
22292f08076SMaxime Ripard 	info = drm_get_format_info(dev, mode_cmd);
223e0f9a4abSLaurent Pinchart 	if (!info || !info->depth || info->cpp[0] > 4)
224e0f9a4abSLaurent Pinchart 		return -EINVAL;
225a9a644acSDave Airlie 
226a9a644acSDave Airlie 	if (mode_cmd->pitches[0] & 63)
2274d8d096eSAlan Cox 		return -EINVAL;
228e0f9a4abSLaurent Pinchart 
229e18da8ceSThomas Zimmermann 	drm_helper_mode_fill_fb_struct(dev, fb, mode_cmd);
2300471c9f5SThomas Zimmermann 	fb->obj[0] = obj;
231e18da8ceSThomas Zimmermann 	ret = drm_framebuffer_init(dev, fb, &psb_fb_funcs);
2324d8d096eSAlan Cox 	if (ret) {
2334d8d096eSAlan Cox 		dev_err(dev->dev, "framebuffer init failed: %d\n", ret);
2344d8d096eSAlan Cox 		return ret;
2354d8d096eSAlan Cox 	}
2364d8d096eSAlan Cox 	return 0;
2374d8d096eSAlan Cox }
2384d8d096eSAlan Cox 
2394d8d096eSAlan Cox /**
2404d8d096eSAlan Cox  *	psb_framebuffer_create	-	create a framebuffer backed by gt
2414d8d096eSAlan Cox  *	@dev: our DRM device
2424d8d096eSAlan Cox  *	@mode_cmd: the description of the requested mode
2434d8d096eSAlan Cox  *	@gt: the backing object
2444d8d096eSAlan Cox  *
2454d8d096eSAlan Cox  *	Create a framebuffer object backed by the gt, and fill in the
2464d8d096eSAlan Cox  *	boilerplate required
2474d8d096eSAlan Cox  *
2484d8d096eSAlan Cox  *	TODO: review object references
2494d8d096eSAlan Cox  */
2504d8d096eSAlan Cox 
2514d8d096eSAlan Cox static struct drm_framebuffer *psb_framebuffer_create
2524d8d096eSAlan Cox 			(struct drm_device *dev,
2531eb83451SVille Syrjälä 			 const struct drm_mode_fb_cmd2 *mode_cmd,
2540471c9f5SThomas Zimmermann 			 struct drm_gem_object *obj)
2554d8d096eSAlan Cox {
256e18da8ceSThomas Zimmermann 	struct drm_framebuffer *fb;
2574d8d096eSAlan Cox 	int ret;
2584d8d096eSAlan Cox 
2594d8d096eSAlan Cox 	fb = kzalloc(sizeof(*fb), GFP_KERNEL);
2604d8d096eSAlan Cox 	if (!fb)
2614d8d096eSAlan Cox 		return ERR_PTR(-ENOMEM);
2624d8d096eSAlan Cox 
2630471c9f5SThomas Zimmermann 	ret = psb_framebuffer_init(dev, fb, mode_cmd, obj);
2644d8d096eSAlan Cox 	if (ret) {
2654d8d096eSAlan Cox 		kfree(fb);
2664d8d096eSAlan Cox 		return ERR_PTR(ret);
2674d8d096eSAlan Cox 	}
268e18da8ceSThomas Zimmermann 	return fb;
2694d8d096eSAlan Cox }
2704d8d096eSAlan Cox 
2714d8d096eSAlan Cox /**
2724d8d096eSAlan Cox  *	psbfb_alloc		-	allocate frame buffer memory
2734d8d096eSAlan Cox  *	@dev: the DRM device
2744d8d096eSAlan Cox  *	@aligned_size: space needed
2754d8d096eSAlan Cox  *
2764d8d096eSAlan Cox  *	Allocate the frame buffer. In the usual case we get a GTT range that
2774d8d096eSAlan Cox  *	is stolen memory backed and life is simple. If there isn't sufficient
278dffc9cebSAlan Cox  *	we fail as we don't have the virtual mapping space to really vmap it
279dffc9cebSAlan Cox  *	and the kernel console code can't handle non linear framebuffers.
2804d8d096eSAlan Cox  *
281dffc9cebSAlan Cox  *	Re-address this as and if the framebuffer layer grows this ability.
2824d8d096eSAlan Cox  */
2834d8d096eSAlan Cox static struct gtt_range *psbfb_alloc(struct drm_device *dev, int aligned_size)
2844d8d096eSAlan Cox {
2854d8d096eSAlan Cox 	struct gtt_range *backing;
2864d8d096eSAlan Cox 	/* Begin by trying to use stolen memory backing */
287c269c685SPatrik Jakobsson 	backing = psb_gtt_alloc_range(dev, aligned_size, "fb", 1, PAGE_SIZE);
2884d8d096eSAlan Cox 	if (backing) {
289*7cd467d0SThomas Zimmermann 		backing->gem.funcs = &psb_gem_object_funcs;
29089c8233fSDavid Herrmann 		drm_gem_private_object_init(dev, &backing->gem, aligned_size);
2914d8d096eSAlan Cox 		return backing;
2924d8d096eSAlan Cox 	}
2934d8d096eSAlan Cox 	return NULL;
2944d8d096eSAlan Cox }
2954d8d096eSAlan Cox 
2964d8d096eSAlan Cox /**
2974d8d096eSAlan Cox  *	psbfb_create		-	create a framebuffer
2984d8d096eSAlan Cox  *	@fbdev: the framebuffer device
2994d8d096eSAlan Cox  *	@sizes: specification of the layout
3004d8d096eSAlan Cox  *
3014d8d096eSAlan Cox  *	Create a framebuffer to the specifications provided
3024d8d096eSAlan Cox  */
3036b7ce2c4SThomas Zimmermann static int psbfb_create(struct drm_fb_helper *fb_helper,
3044d8d096eSAlan Cox 				struct drm_fb_helper_surface_size *sizes)
3054d8d096eSAlan Cox {
3066b7ce2c4SThomas Zimmermann 	struct drm_device *dev = fb_helper->dev;
3074d8d096eSAlan Cox 	struct drm_psb_private *dev_priv = dev->dev_private;
3084d8d096eSAlan Cox 	struct fb_info *info;
3090a8ec2edSThomas Zimmermann 	struct drm_framebuffer *fb;
310a9a644acSDave Airlie 	struct drm_mode_fb_cmd2 mode_cmd;
3114d8d096eSAlan Cox 	int size;
3124d8d096eSAlan Cox 	int ret;
3134d8d096eSAlan Cox 	struct gtt_range *backing;
314a9a644acSDave Airlie 	u32 bpp, depth;
3151b223c9eSAlan Cox 	int gtt_roll = 0;
3161b223c9eSAlan Cox 	int pitch_lines = 0;
3174d8d096eSAlan Cox 
3184d8d096eSAlan Cox 	mode_cmd.width = sizes->surface_width;
3194d8d096eSAlan Cox 	mode_cmd.height = sizes->surface_height;
320a9a644acSDave Airlie 	bpp = sizes->surface_bpp;
3216aa1ead1SKirill A. Shutemov 	depth = sizes->surface_depth;
3224d8d096eSAlan Cox 
3234d8d096eSAlan Cox 	/* No 24bit packed */
324a9a644acSDave Airlie 	if (bpp == 24)
325a9a644acSDave Airlie 		bpp = 32;
3264d8d096eSAlan Cox 
3271b223c9eSAlan Cox 	do {
3281b223c9eSAlan Cox 		/*
3291b223c9eSAlan Cox 		 * Acceleration via the GTT requires pitch to be
3301b223c9eSAlan Cox 		 * power of two aligned. Preferably page but less
3311b223c9eSAlan Cox 		 * is ok with some fonts
3321b223c9eSAlan Cox 		 */
3331b223c9eSAlan Cox         	mode_cmd.pitches[0] =  ALIGN(mode_cmd.width * ((bpp + 7) / 8), 4096 >> pitch_lines);
3344d8d096eSAlan Cox 
335a9a644acSDave Airlie         	size = mode_cmd.pitches[0] * mode_cmd.height;
3364d8d096eSAlan Cox         	size = ALIGN(size, PAGE_SIZE);
3374d8d096eSAlan Cox 
3381b223c9eSAlan Cox 		/* Allocate the fb in the GTT with stolen page backing */
339a6ba582dSAlan Cox 		backing = psbfb_alloc(dev, size);
3401b223c9eSAlan Cox 
3411b223c9eSAlan Cox 		if (pitch_lines)
3421b223c9eSAlan Cox 			pitch_lines *= 2;
3431b223c9eSAlan Cox 		else
3441b223c9eSAlan Cox 			pitch_lines = 1;
3451b223c9eSAlan Cox 		gtt_roll++;
3461b223c9eSAlan Cox 	} while (backing == NULL && pitch_lines <= 16);
3471b223c9eSAlan Cox 
3481b223c9eSAlan Cox 	/* The final pitch we accepted if we succeeded */
3491b223c9eSAlan Cox 	pitch_lines /= 2;
3501b223c9eSAlan Cox 
351a6ba582dSAlan Cox 	if (backing == NULL) {
352a6ba582dSAlan Cox 		/*
353a6ba582dSAlan Cox 		 *	We couldn't get the space we wanted, fall back to the
354a6ba582dSAlan Cox 		 *	display engine requirement instead.  The HW requires
355a6ba582dSAlan Cox 		 *	the pitch to be 64 byte aligned
356a6ba582dSAlan Cox 		 */
357a6ba582dSAlan Cox 
358a6ba582dSAlan Cox 		gtt_roll = 0;	/* Don't use GTT accelerated scrolling */
3591b223c9eSAlan Cox 		pitch_lines = 64;
360a6ba582dSAlan Cox 
361a6ba582dSAlan Cox 		mode_cmd.pitches[0] =  ALIGN(mode_cmd.width * ((bpp + 7) / 8), 64);
362a6ba582dSAlan Cox 
363a6ba582dSAlan Cox 		size = mode_cmd.pitches[0] * mode_cmd.height;
364a6ba582dSAlan Cox 		size = ALIGN(size, PAGE_SIZE);
365a6ba582dSAlan Cox 
3664d8d096eSAlan Cox 		/* Allocate the framebuffer in the GTT with stolen page backing */
3674d8d096eSAlan Cox 		backing = psbfb_alloc(dev, size);
3684d8d096eSAlan Cox 		if (backing == NULL)
3694d8d096eSAlan Cox 			return -ENOMEM;
370a6ba582dSAlan Cox 	}
3714d8d096eSAlan Cox 
372bb849779SAlan Cox 	memset(dev_priv->vram_addr + backing->offset, 0, size);
373bb849779SAlan Cox 
3746b7ce2c4SThomas Zimmermann 	info = drm_fb_helper_alloc_fbi(fb_helper);
375546187c8SArchit Taneja 	if (IS_ERR(info)) {
376546187c8SArchit Taneja 		ret = PTR_ERR(info);
377da7bdda2SDaniel Vetter 		goto out;
3784d8d096eSAlan Cox 	}
3794d8d096eSAlan Cox 
380a9a644acSDave Airlie 	mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
381a9a644acSDave Airlie 
3820a8ec2edSThomas Zimmermann 	fb = psb_framebuffer_create(dev, &mode_cmd, &backing->gem);
3830a8ec2edSThomas Zimmermann 	if (IS_ERR(fb)) {
3840a8ec2edSThomas Zimmermann 		ret = PTR_ERR(fb);
385da7bdda2SDaniel Vetter 		goto out;
3860a8ec2edSThomas Zimmermann 	}
3874d8d096eSAlan Cox 
3886b7ce2c4SThomas Zimmermann 	fb_helper->fb = fb;
3894d8d096eSAlan Cox 
3901b223c9eSAlan Cox 	if (dev_priv->ops->accel_2d && pitch_lines > 8)	/* 2D engine */
3911b223c9eSAlan Cox 		info->fbops = &psbfb_ops;
3921b223c9eSAlan Cox 	else if (gtt_roll) {	/* GTT rolling seems best */
393a6ba582dSAlan Cox 		info->fbops = &psbfb_roll_ops;
394a6ba582dSAlan Cox 		info->flags |= FBINFO_HWACCEL_YPAN;
3951b223c9eSAlan Cox 	} else	/* Software */
396a6ba582dSAlan Cox 		info->fbops = &psbfb_unaccel_ops;
3974d8d096eSAlan Cox 
3984d8d096eSAlan Cox 	info->fix.smem_start = dev->mode_config.fb_base;
3994d8d096eSAlan Cox 	info->fix.smem_len = size;
400a6ba582dSAlan Cox 	info->fix.ywrapstep = gtt_roll;
401a6ba582dSAlan Cox 	info->fix.ypanstep = 0;
4024d8d096eSAlan Cox 
4034d8d096eSAlan Cox 	/* Accessed stolen memory directly */
40437214ca0SKirill A. Shutemov 	info->screen_base = dev_priv->vram_addr + backing->offset;
4054d8d096eSAlan Cox 	info->screen_size = size;
4064d8d096eSAlan Cox 
4074d8d096eSAlan Cox 	if (dev_priv->gtt.stolen_size) {
4084d8d096eSAlan Cox 		info->apertures->ranges[0].base = dev->mode_config.fb_base;
4094d8d096eSAlan Cox 		info->apertures->ranges[0].size = dev_priv->gtt.stolen_size;
4104d8d096eSAlan Cox 	}
4114d8d096eSAlan Cox 
4126b7ce2c4SThomas Zimmermann 	drm_fb_helper_fill_info(info, fb_helper, sizes);
4134d8d096eSAlan Cox 
4144d8d096eSAlan Cox 	info->fix.mmio_start = pci_resource_start(dev->pdev, 0);
4154d8d096eSAlan Cox 	info->fix.mmio_len = pci_resource_len(dev->pdev, 0);
4164d8d096eSAlan Cox 
417fb2a99e1SSascha Hauer 	/* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
4184d8d096eSAlan Cox 
419e18da8ceSThomas Zimmermann 	dev_dbg(dev->dev, "allocated %dx%d fb\n", fb->width, fb->height);
4204d8d096eSAlan Cox 
4214d8d096eSAlan Cox 	return 0;
422da7bdda2SDaniel Vetter out:
4234d8d096eSAlan Cox 	psb_gtt_free_range(dev, backing);
4244d8d096eSAlan Cox 	return ret;
4254d8d096eSAlan Cox }
4264d8d096eSAlan Cox 
4274d8d096eSAlan Cox /**
4284d8d096eSAlan Cox  *	psb_user_framebuffer_create	-	create framebuffer
4294d8d096eSAlan Cox  *	@dev: our DRM device
4304d8d096eSAlan Cox  *	@filp: client file
4314d8d096eSAlan Cox  *	@cmd: mode request
4324d8d096eSAlan Cox  *
4334d8d096eSAlan Cox  *	Create a new framebuffer backed by a userspace GEM object
4344d8d096eSAlan Cox  */
4354d8d096eSAlan Cox static struct drm_framebuffer *psb_user_framebuffer_create
4364d8d096eSAlan Cox 			(struct drm_device *dev, struct drm_file *filp,
4371eb83451SVille Syrjälä 			 const struct drm_mode_fb_cmd2 *cmd)
4384d8d096eSAlan Cox {
4394d8d096eSAlan Cox 	struct drm_gem_object *obj;
4404d8d096eSAlan Cox 
4414d8d096eSAlan Cox 	/*
4424d8d096eSAlan Cox 	 *	Find the GEM object and thus the gtt range object that is
4434d8d096eSAlan Cox 	 *	to back this space
4444d8d096eSAlan Cox 	 */
445a8ad0bd8SChris Wilson 	obj = drm_gem_object_lookup(filp, cmd->handles[0]);
4464d8d096eSAlan Cox 	if (obj == NULL)
4474d8d096eSAlan Cox 		return ERR_PTR(-ENOENT);
4484d8d096eSAlan Cox 
4494d8d096eSAlan Cox 	/* Let the core code do all the work */
4500471c9f5SThomas Zimmermann 	return psb_framebuffer_create(dev, cmd, obj);
4514d8d096eSAlan Cox }
4524d8d096eSAlan Cox 
4536b7ce2c4SThomas Zimmermann static int psbfb_probe(struct drm_fb_helper *fb_helper,
4544d8d096eSAlan Cox 				struct drm_fb_helper_surface_size *sizes)
4554d8d096eSAlan Cox {
4566b7ce2c4SThomas Zimmermann 	struct drm_device *dev = fb_helper->dev;
4573aad16d2SAlan Cox 	struct drm_psb_private *dev_priv = dev->dev_private;
458fd1a5e52SPaul Kocialkowski 	unsigned int fb_size;
4593aad16d2SAlan Cox 	int bytespp;
4604d8d096eSAlan Cox 
4613aad16d2SAlan Cox 	bytespp = sizes->surface_bpp / 8;
4623aad16d2SAlan Cox 	if (bytespp == 3)	/* no 24bit packed */
4633aad16d2SAlan Cox 		bytespp = 4;
4643aad16d2SAlan Cox 
4653aad16d2SAlan Cox 	/* If the mode will not fit in 32bit then switch to 16bit to get
4663aad16d2SAlan Cox 	   a console on full resolution. The X mode setting server will
4673aad16d2SAlan Cox 	   allocate its own 32bit GEM framebuffer */
468fd1a5e52SPaul Kocialkowski 	fb_size = ALIGN(sizes->surface_width * bytespp, 64) *
469fd1a5e52SPaul Kocialkowski 		  sizes->surface_height;
470fd1a5e52SPaul Kocialkowski 	fb_size = ALIGN(fb_size, PAGE_SIZE);
471fd1a5e52SPaul Kocialkowski 
472fd1a5e52SPaul Kocialkowski 	if (fb_size > dev_priv->vram_stolen_size) {
4733aad16d2SAlan Cox                 sizes->surface_bpp = 16;
4743aad16d2SAlan Cox                 sizes->surface_depth = 16;
4753aad16d2SAlan Cox         }
4763aad16d2SAlan Cox 
4776b7ce2c4SThomas Zimmermann 	return psbfb_create(fb_helper, sizes);
4784d8d096eSAlan Cox }
4794d8d096eSAlan Cox 
4803a493879SThierry Reding static const struct drm_fb_helper_funcs psb_fb_helper_funcs = {
4814d8d096eSAlan Cox 	.fb_probe = psbfb_probe,
4824d8d096eSAlan Cox };
4834d8d096eSAlan Cox 
4846b7ce2c4SThomas Zimmermann static int psb_fbdev_destroy(struct drm_device *dev,
4856b7ce2c4SThomas Zimmermann 			     struct drm_fb_helper *fb_helper)
4864d8d096eSAlan Cox {
4876b7ce2c4SThomas Zimmermann 	struct drm_framebuffer *fb = fb_helper->fb;
4884d8d096eSAlan Cox 
4896b7ce2c4SThomas Zimmermann 	drm_fb_helper_unregister_fbi(fb_helper);
490546187c8SArchit Taneja 
4916b7ce2c4SThomas Zimmermann 	drm_fb_helper_fini(fb_helper);
492e18da8ceSThomas Zimmermann 	drm_framebuffer_unregister_private(fb);
493e18da8ceSThomas Zimmermann 	drm_framebuffer_cleanup(fb);
4944d8d096eSAlan Cox 
495e18da8ceSThomas Zimmermann 	if (fb->obj[0])
496e19baf70SEmil Velikov 		drm_gem_object_put(fb->obj[0]);
4970a8ec2edSThomas Zimmermann 	kfree(fb);
4980a8ec2edSThomas Zimmermann 
4994d8d096eSAlan Cox 	return 0;
5004d8d096eSAlan Cox }
5014d8d096eSAlan Cox 
5024d8d096eSAlan Cox int psb_fbdev_init(struct drm_device *dev)
5034d8d096eSAlan Cox {
5046b7ce2c4SThomas Zimmermann 	struct drm_fb_helper *fb_helper;
5054d8d096eSAlan Cox 	struct drm_psb_private *dev_priv = dev->dev_private;
50601934c2aSThierry Reding 	int ret;
5074d8d096eSAlan Cox 
5086b7ce2c4SThomas Zimmermann 	fb_helper = kzalloc(sizeof(*fb_helper), GFP_KERNEL);
5096b7ce2c4SThomas Zimmermann 	if (!fb_helper) {
5104d8d096eSAlan Cox 		dev_err(dev->dev, "no memory\n");
5114d8d096eSAlan Cox 		return -ENOMEM;
5124d8d096eSAlan Cox 	}
5134d8d096eSAlan Cox 
5146b7ce2c4SThomas Zimmermann 	dev_priv->fb_helper = fb_helper;
51510a23102SThierry Reding 
5166b7ce2c4SThomas Zimmermann 	drm_fb_helper_prepare(dev, fb_helper, &psb_fb_helper_funcs);
5174d8d096eSAlan Cox 
5182dea2d11SPankaj Bharadiya 	ret = drm_fb_helper_init(dev, fb_helper);
51901934c2aSThierry Reding 	if (ret)
52001934c2aSThierry Reding 		goto free;
5214d8d096eSAlan Cox 
52276a39dbfSDaniel Vetter 	/* disable all the possible outputs/crtcs before entering KMS mode */
52376a39dbfSDaniel Vetter 	drm_helper_disable_unused_functions(dev);
52476a39dbfSDaniel Vetter 
5256b7ce2c4SThomas Zimmermann 	ret = drm_fb_helper_initial_config(fb_helper, 32);
52601934c2aSThierry Reding 	if (ret)
52701934c2aSThierry Reding 		goto fini;
52801934c2aSThierry Reding 
5294d8d096eSAlan Cox 	return 0;
53001934c2aSThierry Reding 
53101934c2aSThierry Reding fini:
5326b7ce2c4SThomas Zimmermann 	drm_fb_helper_fini(fb_helper);
53301934c2aSThierry Reding free:
5346b7ce2c4SThomas Zimmermann 	kfree(fb_helper);
53501934c2aSThierry Reding 	return ret;
5364d8d096eSAlan Cox }
5374d8d096eSAlan Cox 
538bc7f2b08SKirill A. Shutemov static void psb_fbdev_fini(struct drm_device *dev)
5394d8d096eSAlan Cox {
5404d8d096eSAlan Cox 	struct drm_psb_private *dev_priv = dev->dev_private;
5414d8d096eSAlan Cox 
5426b7ce2c4SThomas Zimmermann 	if (!dev_priv->fb_helper)
5434d8d096eSAlan Cox 		return;
5444d8d096eSAlan Cox 
5456b7ce2c4SThomas Zimmermann 	psb_fbdev_destroy(dev, dev_priv->fb_helper);
5466b7ce2c4SThomas Zimmermann 	kfree(dev_priv->fb_helper);
5476b7ce2c4SThomas Zimmermann 	dev_priv->fb_helper = NULL;
5484d8d096eSAlan Cox }
5494d8d096eSAlan Cox 
5504d8d096eSAlan Cox static const struct drm_mode_config_funcs psb_mode_funcs = {
5514d8d096eSAlan Cox 	.fb_create = psb_user_framebuffer_create,
552cdc13f68SNoralf Trønnes 	.output_poll_changed = drm_fb_helper_output_poll_changed,
5534d8d096eSAlan Cox };
5544d8d096eSAlan Cox 
5554d8d096eSAlan Cox static void psb_setup_outputs(struct drm_device *dev)
5564d8d096eSAlan Cox {
5574d8d096eSAlan Cox 	struct drm_psb_private *dev_priv = dev->dev_private;
5584d8d096eSAlan Cox 	struct drm_connector *connector;
5594d8d096eSAlan Cox 
5604d8d096eSAlan Cox 	drm_mode_create_scaling_mode_property(dev);
5614d8d096eSAlan Cox 
56213619ce5SAlan Cox 	/* It is ok for this to fail - we just don't get backlight control */
56313619ce5SAlan Cox 	if (!dev_priv->backlight_property)
56413619ce5SAlan Cox 		dev_priv->backlight_property = drm_property_create_range(dev, 0,
56513619ce5SAlan Cox 							"backlight", 0, 100);
5664d8d096eSAlan Cox 	dev_priv->ops->output_init(dev);
5674d8d096eSAlan Cox 
5684d8d096eSAlan Cox 	list_for_each_entry(connector, &dev->mode_config.connector_list,
5694d8d096eSAlan Cox 			    head) {
570367e4408SPatrik Jakobsson 		struct gma_encoder *gma_encoder = gma_attached_encoder(connector);
571367e4408SPatrik Jakobsson 		struct drm_encoder *encoder = &gma_encoder->base;
5724d8d096eSAlan Cox 		int crtc_mask = 0, clone_mask = 0;
5734d8d096eSAlan Cox 
5744d8d096eSAlan Cox 		/* valid crtcs */
575367e4408SPatrik Jakobsson 		switch (gma_encoder->type) {
5764d8d096eSAlan Cox 		case INTEL_OUTPUT_ANALOG:
5774d8d096eSAlan Cox 			crtc_mask = (1 << 0);
5784d8d096eSAlan Cox 			clone_mask = (1 << INTEL_OUTPUT_ANALOG);
5794d8d096eSAlan Cox 			break;
5804d8d096eSAlan Cox 		case INTEL_OUTPUT_SDVO:
581cf8efd3aSPatrik Jakobsson 			crtc_mask = dev_priv->ops->sdvo_mask;
5828a7827eeSVille Syrjälä 			clone_mask = 0;
5834d8d096eSAlan Cox 			break;
5844d8d096eSAlan Cox 		case INTEL_OUTPUT_LVDS:
585d235e64aSAlan Cox 			crtc_mask = dev_priv->ops->lvds_mask;
5868a7827eeSVille Syrjälä 			clone_mask = 0;
5874d8d096eSAlan Cox 			break;
5884d8d096eSAlan Cox 		case INTEL_OUTPUT_MIPI:
5894d8d096eSAlan Cox 			crtc_mask = (1 << 0);
5908a7827eeSVille Syrjälä 			clone_mask = 0;
5914d8d096eSAlan Cox 			break;
5924d8d096eSAlan Cox 		case INTEL_OUTPUT_MIPI2:
5934d8d096eSAlan Cox 			crtc_mask = (1 << 2);
5948a7827eeSVille Syrjälä 			clone_mask = 0;
5954d8d096eSAlan Cox 			break;
5964d8d096eSAlan Cox 		case INTEL_OUTPUT_HDMI:
597d235e64aSAlan Cox 			crtc_mask = dev_priv->ops->hdmi_mask;
5984d8d096eSAlan Cox 			clone_mask = (1 << INTEL_OUTPUT_HDMI);
5994d8d096eSAlan Cox 			break;
600220801bdSAlan Cox 		case INTEL_OUTPUT_DISPLAYPORT:
601220801bdSAlan Cox 			crtc_mask = (1 << 0) | (1 << 1);
6028a7827eeSVille Syrjälä 			clone_mask = 0;
603220801bdSAlan Cox 			break;
604d112a816SZhao Yakui 		case INTEL_OUTPUT_EDP:
605d112a816SZhao Yakui 			crtc_mask = (1 << 1);
6068a7827eeSVille Syrjälä 			clone_mask = 0;
6074d8d096eSAlan Cox 		}
6084d8d096eSAlan Cox 		encoder->possible_crtcs = crtc_mask;
6094d8d096eSAlan Cox 		encoder->possible_clones =
610a3d5d75fSPatrik Jakobsson 		    gma_connector_clones(dev, clone_mask);
6114d8d096eSAlan Cox 	}
6124d8d096eSAlan Cox }
6134d8d096eSAlan Cox 
6144d8d096eSAlan Cox void psb_modeset_init(struct drm_device *dev)
6154d8d096eSAlan Cox {
6164d8d096eSAlan Cox 	struct drm_psb_private *dev_priv = dev->dev_private;
6174d8d096eSAlan Cox 	struct psb_intel_mode_device *mode_dev = &dev_priv->mode_dev;
6184d8d096eSAlan Cox 	int i;
6194d8d096eSAlan Cox 
6204d8d096eSAlan Cox 	drm_mode_config_init(dev);
6214d8d096eSAlan Cox 
6224d8d096eSAlan Cox 	dev->mode_config.min_width = 0;
6234d8d096eSAlan Cox 	dev->mode_config.min_height = 0;
6244d8d096eSAlan Cox 
625e6ecefaaSLaurent Pinchart 	dev->mode_config.funcs = &psb_mode_funcs;
6264d8d096eSAlan Cox 
6274d8d096eSAlan Cox 	/* set memory base */
628dffc9cebSAlan Cox 	/* Oaktrail and Poulsbo should use BAR 2*/
6294d8d096eSAlan Cox 	pci_read_config_dword(dev->pdev, PSB_BSM, (u32 *)
6304d8d096eSAlan Cox 					&(dev->mode_config.fb_base));
6314d8d096eSAlan Cox 
6324d8d096eSAlan Cox 	/* num pipes is 2 for PSB but 1 for Mrst */
6334d8d096eSAlan Cox 	for (i = 0; i < dev_priv->num_pipe; i++)
6344d8d096eSAlan Cox 		psb_intel_crtc_init(dev, i, mode_dev);
6354d8d096eSAlan Cox 
636cbbd379aSPatrik Jakobsson 	dev->mode_config.max_width = 4096;
637cbbd379aSPatrik Jakobsson 	dev->mode_config.max_height = 4096;
6384d8d096eSAlan Cox 
6394d8d096eSAlan Cox 	psb_setup_outputs(dev);
640d235e64aSAlan Cox 
641d235e64aSAlan Cox 	if (dev_priv->ops->errata)
642d235e64aSAlan Cox 	        dev_priv->ops->errata(dev);
6434ab2c7f1SAlan Cox 
6444ab2c7f1SAlan Cox         dev_priv->modeset = true;
6454d8d096eSAlan Cox }
6464d8d096eSAlan Cox 
6474d8d096eSAlan Cox void psb_modeset_cleanup(struct drm_device *dev)
6484d8d096eSAlan Cox {
6494ab2c7f1SAlan Cox 	struct drm_psb_private *dev_priv = dev->dev_private;
6504ab2c7f1SAlan Cox 	if (dev_priv->modeset) {
6514d8d096eSAlan Cox 		drm_kms_helper_poll_fini(dev);
6524d8d096eSAlan Cox 		psb_fbdev_fini(dev);
6534d8d096eSAlan Cox 		drm_mode_config_cleanup(dev);
6544d8d096eSAlan Cox 	}
6554ab2c7f1SAlan Cox }
656