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"
27a6ba582dSAlan Cox #include "gtt.h"
280c7b178aSSam Ravnborg #include "psb_drv.h"
290c7b178aSSam Ravnborg #include "psb_intel_drv.h"
300c7b178aSSam Ravnborg #include "psb_intel_reg.h"
314d8d096eSAlan Cox 
324d8d096eSAlan Cox static const struct drm_framebuffer_funcs psb_fb_funcs = {
33bc61c975SDaniel Stone 	.destroy = drm_gem_fb_destroy,
34bc61c975SDaniel Stone 	.create_handle = drm_gem_fb_create_handle,
354d8d096eSAlan Cox };
364d8d096eSAlan Cox 
374d8d096eSAlan Cox #define CMAP_TOHW(_val, _width) ((((_val) << (_width)) + 0x7FFF - (_val)) >> 16)
384d8d096eSAlan Cox 
394d8d096eSAlan Cox static int psbfb_setcolreg(unsigned regno, unsigned red, unsigned green,
404d8d096eSAlan Cox 			   unsigned blue, unsigned transp,
414d8d096eSAlan Cox 			   struct fb_info *info)
424d8d096eSAlan Cox {
436b7ce2c4SThomas Zimmermann 	struct drm_fb_helper *fb_helper = info->par;
446b7ce2c4SThomas Zimmermann 	struct drm_framebuffer *fb = fb_helper->fb;
454d8d096eSAlan Cox 	uint32_t v;
464d8d096eSAlan Cox 
474d8d096eSAlan Cox 	if (!fb)
484d8d096eSAlan Cox 		return -ENOMEM;
494d8d096eSAlan Cox 
504d8d096eSAlan Cox 	if (regno > 255)
514d8d096eSAlan Cox 		return 1;
524d8d096eSAlan Cox 
534d8d096eSAlan Cox 	red = CMAP_TOHW(red, info->var.red.length);
544d8d096eSAlan Cox 	blue = CMAP_TOHW(blue, info->var.blue.length);
554d8d096eSAlan Cox 	green = CMAP_TOHW(green, info->var.green.length);
564d8d096eSAlan Cox 	transp = CMAP_TOHW(transp, info->var.transp.length);
574d8d096eSAlan Cox 
584d8d096eSAlan Cox 	v = (red << info->var.red.offset) |
594d8d096eSAlan Cox 	    (green << info->var.green.offset) |
604d8d096eSAlan Cox 	    (blue << info->var.blue.offset) |
614d8d096eSAlan Cox 	    (transp << info->var.transp.offset);
624d8d096eSAlan Cox 
634d8d096eSAlan Cox 	if (regno < 16) {
64272725c7SVille Syrjälä 		switch (fb->format->cpp[0] * 8) {
654d8d096eSAlan Cox 		case 16:
664d8d096eSAlan Cox 			((uint32_t *) info->pseudo_palette)[regno] = v;
674d8d096eSAlan Cox 			break;
684d8d096eSAlan Cox 		case 24:
694d8d096eSAlan Cox 		case 32:
704d8d096eSAlan Cox 			((uint32_t *) info->pseudo_palette)[regno] = v;
714d8d096eSAlan Cox 			break;
724d8d096eSAlan Cox 		}
734d8d096eSAlan Cox 	}
744d8d096eSAlan Cox 
754d8d096eSAlan Cox 	return 0;
764d8d096eSAlan Cox }
774d8d096eSAlan Cox 
78a6ba582dSAlan Cox static int psbfb_pan(struct fb_var_screeninfo *var, struct fb_info *info)
79a6ba582dSAlan Cox {
806b7ce2c4SThomas Zimmermann 	struct drm_fb_helper *fb_helper = info->par;
816b7ce2c4SThomas Zimmermann 	struct drm_framebuffer *fb = fb_helper->fb;
82e18da8ceSThomas Zimmermann 	struct drm_device *dev = fb->dev;
83e18da8ceSThomas Zimmermann 	struct gtt_range *gtt = to_gtt_range(fb->obj[0]);
84a6ba582dSAlan Cox 
85a6ba582dSAlan Cox 	/*
86a6ba582dSAlan Cox 	 *	We have to poke our nose in here. The core fb code assumes
87a6ba582dSAlan Cox 	 *	panning is part of the hardware that can be invoked before
88a6ba582dSAlan Cox 	 *	the actual fb is mapped. In our case that isn't quite true.
89a6ba582dSAlan Cox 	 */
90bc61c975SDaniel Stone 	if (gtt->npage) {
91a6ba582dSAlan Cox 		/* GTT roll shifts in 4K pages, we need to shift the right
92a6ba582dSAlan Cox 		   number of pages */
93a6ba582dSAlan Cox 		int pages = info->fix.line_length >> 12;
94bc61c975SDaniel Stone 		psb_gtt_roll(dev, gtt, var->yoffset * pages);
95a6ba582dSAlan Cox 	}
96a6ba582dSAlan Cox         return 0;
97a6ba582dSAlan Cox }
984d8d096eSAlan Cox 
990edf6813SSouptick Joarder static vm_fault_t psbfb_vm_fault(struct vm_fault *vmf)
1004d8d096eSAlan Cox {
10111bac800SDave Jiang 	struct vm_area_struct *vma = vmf->vma;
102e18da8ceSThomas Zimmermann 	struct drm_framebuffer *fb = vma->vm_private_data;
103e18da8ceSThomas Zimmermann 	struct drm_device *dev = fb->dev;
1044d8d096eSAlan Cox 	struct drm_psb_private *dev_priv = dev->dev_private;
105e18da8ceSThomas Zimmermann 	struct gtt_range *gtt = to_gtt_range(fb->obj[0]);
1064d8d096eSAlan Cox 	int page_num;
1074d8d096eSAlan Cox 	int i;
1084d8d096eSAlan Cox 	unsigned long address;
1090edf6813SSouptick Joarder 	vm_fault_t ret = VM_FAULT_SIGBUS;
1104d8d096eSAlan Cox 	unsigned long pfn;
11161bb3feaSPatrik Jakobsson 	unsigned long phys_addr = (unsigned long)dev_priv->stolen_base +
112bc61c975SDaniel Stone 				  gtt->offset;
1134d8d096eSAlan Cox 
114024b6a63SShyam Saini 	page_num = vma_pages(vma);
1151a29d85eSJan Kara 	address = vmf->address - (vmf->pgoff << PAGE_SHIFT);
1164d8d096eSAlan Cox 
1174d8d096eSAlan Cox 	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1184d8d096eSAlan Cox 
1194d8d096eSAlan Cox 	for (i = 0; i < page_num; i++) {
1204d8d096eSAlan Cox 		pfn = (phys_addr >> PAGE_SHIFT);
1214d8d096eSAlan Cox 
1220edf6813SSouptick Joarder 		ret = vmf_insert_mixed(vma, address,
12301c8f1c4SDan Williams 				__pfn_to_pfn_t(pfn, PFN_DEV));
1240edf6813SSouptick Joarder 		if (unlikely(ret & VM_FAULT_ERROR))
1254d8d096eSAlan Cox 			break;
1264d8d096eSAlan Cox 		address += PAGE_SIZE;
1274d8d096eSAlan Cox 		phys_addr += PAGE_SIZE;
1284d8d096eSAlan Cox 	}
1290edf6813SSouptick Joarder 	return ret;
1304d8d096eSAlan Cox }
1314d8d096eSAlan Cox 
1324d8d096eSAlan Cox static void psbfb_vm_open(struct vm_area_struct *vma)
1334d8d096eSAlan Cox {
1344d8d096eSAlan Cox }
1354d8d096eSAlan Cox 
1364d8d096eSAlan Cox static void psbfb_vm_close(struct vm_area_struct *vma)
1374d8d096eSAlan Cox {
1384d8d096eSAlan Cox }
1394d8d096eSAlan Cox 
14078b68556SLaurent Pinchart static const struct vm_operations_struct psbfb_vm_ops = {
1414d8d096eSAlan Cox 	.fault	= psbfb_vm_fault,
1424d8d096eSAlan Cox 	.open	= psbfb_vm_open,
1434d8d096eSAlan Cox 	.close	= psbfb_vm_close
1444d8d096eSAlan Cox };
1454d8d096eSAlan Cox 
1464d8d096eSAlan Cox static int psbfb_mmap(struct fb_info *info, struct vm_area_struct *vma)
1474d8d096eSAlan Cox {
1486b7ce2c4SThomas Zimmermann 	struct drm_fb_helper *fb_helper = info->par;
1496b7ce2c4SThomas Zimmermann 	struct drm_framebuffer *fb = fb_helper->fb;
1504d8d096eSAlan Cox 
1514d8d096eSAlan Cox 	if (vma->vm_pgoff != 0)
1524d8d096eSAlan Cox 		return -EINVAL;
1534d8d096eSAlan Cox 	if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
1544d8d096eSAlan Cox 		return -EINVAL;
1554d8d096eSAlan Cox 
1564d8d096eSAlan Cox 	/*
1574d8d096eSAlan Cox 	 * If this is a GEM object then info->screen_base is the virtual
1584d8d096eSAlan Cox 	 * kernel remapping of the object. FIXME: Review if this is
1594d8d096eSAlan Cox 	 * suitable for our mmap work
1604d8d096eSAlan Cox 	 */
1614d8d096eSAlan Cox 	vma->vm_ops = &psbfb_vm_ops;
162e18da8ceSThomas Zimmermann 	vma->vm_private_data = (void *)fb;
163314e51b9SKonstantin Khlebnikov 	vma->vm_flags |= VM_IO | VM_MIXEDMAP | VM_DONTEXPAND | VM_DONTDUMP;
1644d8d096eSAlan Cox 	return 0;
1654d8d096eSAlan Cox }
1664d8d096eSAlan Cox 
1674d8d096eSAlan Cox static struct fb_ops psbfb_ops = {
1684d8d096eSAlan Cox 	.owner = THIS_MODULE,
1693da6c2f3SStefan Christ 	DRM_FB_HELPER_DEFAULT_OPS,
1704d8d096eSAlan Cox 	.fb_setcolreg = psbfb_setcolreg,
171546187c8SArchit Taneja 	.fb_fillrect = drm_fb_helper_cfb_fillrect,
1724d8d096eSAlan Cox 	.fb_copyarea = psbfb_copyarea,
173546187c8SArchit Taneja 	.fb_imageblit = drm_fb_helper_cfb_imageblit,
1744d8d096eSAlan Cox 	.fb_mmap = psbfb_mmap,
1754d8d096eSAlan Cox 	.fb_sync = psbfb_sync,
1764d8d096eSAlan Cox };
1774d8d096eSAlan Cox 
178a6ba582dSAlan Cox static struct fb_ops psbfb_roll_ops = {
179a6ba582dSAlan Cox 	.owner = THIS_MODULE,
1803da6c2f3SStefan Christ 	DRM_FB_HELPER_DEFAULT_OPS,
181a6ba582dSAlan Cox 	.fb_setcolreg = psbfb_setcolreg,
182546187c8SArchit Taneja 	.fb_fillrect = drm_fb_helper_cfb_fillrect,
183546187c8SArchit Taneja 	.fb_copyarea = drm_fb_helper_cfb_copyarea,
184546187c8SArchit Taneja 	.fb_imageblit = drm_fb_helper_cfb_imageblit,
185a6ba582dSAlan Cox 	.fb_pan_display = psbfb_pan,
186a6ba582dSAlan Cox 	.fb_mmap = psbfb_mmap,
187a6ba582dSAlan Cox };
188a6ba582dSAlan Cox 
1894d8d096eSAlan Cox static struct fb_ops psbfb_unaccel_ops = {
1904d8d096eSAlan Cox 	.owner = THIS_MODULE,
1913da6c2f3SStefan Christ 	DRM_FB_HELPER_DEFAULT_OPS,
1924d8d096eSAlan Cox 	.fb_setcolreg = psbfb_setcolreg,
193546187c8SArchit Taneja 	.fb_fillrect = drm_fb_helper_cfb_fillrect,
194546187c8SArchit Taneja 	.fb_copyarea = drm_fb_helper_cfb_copyarea,
195546187c8SArchit Taneja 	.fb_imageblit = drm_fb_helper_cfb_imageblit,
1964d8d096eSAlan Cox 	.fb_mmap = psbfb_mmap,
1974d8d096eSAlan Cox };
1984d8d096eSAlan Cox 
1994d8d096eSAlan Cox /**
2004d8d096eSAlan Cox  *	psb_framebuffer_init	-	initialize a framebuffer
2014d8d096eSAlan Cox  *	@dev: our DRM device
2024d8d096eSAlan Cox  *	@fb: framebuffer to set up
2034d8d096eSAlan Cox  *	@mode_cmd: mode description
2044d8d096eSAlan Cox  *	@gt: backing object
2054d8d096eSAlan Cox  *
2064d8d096eSAlan Cox  *	Configure and fill in the boilerplate for our frame buffer. Return
2074d8d096eSAlan Cox  *	0 on success or an error code if we fail.
2084d8d096eSAlan Cox  */
2094d8d096eSAlan Cox static int psb_framebuffer_init(struct drm_device *dev,
210e18da8ceSThomas Zimmermann 					struct drm_framebuffer *fb,
2111eb83451SVille Syrjälä 					const struct drm_mode_fb_cmd2 *mode_cmd,
2120471c9f5SThomas Zimmermann 					struct drm_gem_object *obj)
2134d8d096eSAlan Cox {
214e0f9a4abSLaurent Pinchart 	const struct drm_format_info *info;
2154d8d096eSAlan Cox 	int ret;
2164d8d096eSAlan Cox 
217e0f9a4abSLaurent Pinchart 	/*
218e0f9a4abSLaurent Pinchart 	 * Reject unknown formats, YUV formats, and formats with more than
219e0f9a4abSLaurent Pinchart 	 * 4 bytes per pixel.
220e0f9a4abSLaurent Pinchart 	 */
22192f08076SMaxime Ripard 	info = drm_get_format_info(dev, mode_cmd);
222e0f9a4abSLaurent Pinchart 	if (!info || !info->depth || info->cpp[0] > 4)
223e0f9a4abSLaurent Pinchart 		return -EINVAL;
224a9a644acSDave Airlie 
225a9a644acSDave Airlie 	if (mode_cmd->pitches[0] & 63)
2264d8d096eSAlan Cox 		return -EINVAL;
227e0f9a4abSLaurent Pinchart 
228e18da8ceSThomas Zimmermann 	drm_helper_mode_fill_fb_struct(dev, fb, mode_cmd);
2290471c9f5SThomas Zimmermann 	fb->obj[0] = obj;
230e18da8ceSThomas Zimmermann 	ret = drm_framebuffer_init(dev, fb, &psb_fb_funcs);
2314d8d096eSAlan Cox 	if (ret) {
2324d8d096eSAlan Cox 		dev_err(dev->dev, "framebuffer init failed: %d\n", ret);
2334d8d096eSAlan Cox 		return ret;
2344d8d096eSAlan Cox 	}
2354d8d096eSAlan Cox 	return 0;
2364d8d096eSAlan Cox }
2374d8d096eSAlan Cox 
2384d8d096eSAlan Cox /**
2394d8d096eSAlan Cox  *	psb_framebuffer_create	-	create a framebuffer backed by gt
2404d8d096eSAlan Cox  *	@dev: our DRM device
2414d8d096eSAlan Cox  *	@mode_cmd: the description of the requested mode
2424d8d096eSAlan Cox  *	@gt: the backing object
2434d8d096eSAlan Cox  *
2444d8d096eSAlan Cox  *	Create a framebuffer object backed by the gt, and fill in the
2454d8d096eSAlan Cox  *	boilerplate required
2464d8d096eSAlan Cox  *
2474d8d096eSAlan Cox  *	TODO: review object references
2484d8d096eSAlan Cox  */
2494d8d096eSAlan Cox 
2504d8d096eSAlan Cox static struct drm_framebuffer *psb_framebuffer_create
2514d8d096eSAlan Cox 			(struct drm_device *dev,
2521eb83451SVille Syrjälä 			 const struct drm_mode_fb_cmd2 *mode_cmd,
2530471c9f5SThomas Zimmermann 			 struct drm_gem_object *obj)
2544d8d096eSAlan Cox {
255e18da8ceSThomas Zimmermann 	struct drm_framebuffer *fb;
2564d8d096eSAlan Cox 	int ret;
2574d8d096eSAlan Cox 
2584d8d096eSAlan Cox 	fb = kzalloc(sizeof(*fb), GFP_KERNEL);
2594d8d096eSAlan Cox 	if (!fb)
2604d8d096eSAlan Cox 		return ERR_PTR(-ENOMEM);
2614d8d096eSAlan Cox 
2620471c9f5SThomas Zimmermann 	ret = psb_framebuffer_init(dev, fb, mode_cmd, obj);
2634d8d096eSAlan Cox 	if (ret) {
2644d8d096eSAlan Cox 		kfree(fb);
2654d8d096eSAlan Cox 		return ERR_PTR(ret);
2664d8d096eSAlan Cox 	}
267e18da8ceSThomas Zimmermann 	return fb;
2684d8d096eSAlan Cox }
2694d8d096eSAlan Cox 
2704d8d096eSAlan Cox /**
2714d8d096eSAlan Cox  *	psbfb_alloc		-	allocate frame buffer memory
2724d8d096eSAlan Cox  *	@dev: the DRM device
2734d8d096eSAlan Cox  *	@aligned_size: space needed
2744d8d096eSAlan Cox  *
2754d8d096eSAlan Cox  *	Allocate the frame buffer. In the usual case we get a GTT range that
2764d8d096eSAlan Cox  *	is stolen memory backed and life is simple. If there isn't sufficient
277dffc9cebSAlan Cox  *	we fail as we don't have the virtual mapping space to really vmap it
278dffc9cebSAlan Cox  *	and the kernel console code can't handle non linear framebuffers.
2794d8d096eSAlan Cox  *
280dffc9cebSAlan Cox  *	Re-address this as and if the framebuffer layer grows this ability.
2814d8d096eSAlan Cox  */
2824d8d096eSAlan Cox static struct gtt_range *psbfb_alloc(struct drm_device *dev, int aligned_size)
2834d8d096eSAlan Cox {
2844d8d096eSAlan Cox 	struct gtt_range *backing;
2854d8d096eSAlan Cox 	/* Begin by trying to use stolen memory backing */
286c269c685SPatrik Jakobsson 	backing = psb_gtt_alloc_range(dev, aligned_size, "fb", 1, PAGE_SIZE);
2874d8d096eSAlan Cox 	if (backing) {
28889c8233fSDavid Herrmann 		drm_gem_private_object_init(dev, &backing->gem, aligned_size);
2894d8d096eSAlan Cox 		return backing;
2904d8d096eSAlan Cox 	}
2914d8d096eSAlan Cox 	return NULL;
2924d8d096eSAlan Cox }
2934d8d096eSAlan Cox 
2944d8d096eSAlan Cox /**
2954d8d096eSAlan Cox  *	psbfb_create		-	create a framebuffer
2964d8d096eSAlan Cox  *	@fbdev: the framebuffer device
2974d8d096eSAlan Cox  *	@sizes: specification of the layout
2984d8d096eSAlan Cox  *
2994d8d096eSAlan Cox  *	Create a framebuffer to the specifications provided
3004d8d096eSAlan Cox  */
3016b7ce2c4SThomas Zimmermann static int psbfb_create(struct drm_fb_helper *fb_helper,
3024d8d096eSAlan Cox 				struct drm_fb_helper_surface_size *sizes)
3034d8d096eSAlan Cox {
3046b7ce2c4SThomas Zimmermann 	struct drm_device *dev = fb_helper->dev;
3054d8d096eSAlan Cox 	struct drm_psb_private *dev_priv = dev->dev_private;
3064d8d096eSAlan Cox 	struct fb_info *info;
3070a8ec2edSThomas Zimmermann 	struct drm_framebuffer *fb;
308a9a644acSDave Airlie 	struct drm_mode_fb_cmd2 mode_cmd;
3094d8d096eSAlan Cox 	int size;
3104d8d096eSAlan Cox 	int ret;
3114d8d096eSAlan Cox 	struct gtt_range *backing;
312a9a644acSDave Airlie 	u32 bpp, depth;
3131b223c9eSAlan Cox 	int gtt_roll = 0;
3141b223c9eSAlan Cox 	int pitch_lines = 0;
3154d8d096eSAlan Cox 
3164d8d096eSAlan Cox 	mode_cmd.width = sizes->surface_width;
3174d8d096eSAlan Cox 	mode_cmd.height = sizes->surface_height;
318a9a644acSDave Airlie 	bpp = sizes->surface_bpp;
3196aa1ead1SKirill A. Shutemov 	depth = sizes->surface_depth;
3204d8d096eSAlan Cox 
3214d8d096eSAlan Cox 	/* No 24bit packed */
322a9a644acSDave Airlie 	if (bpp == 24)
323a9a644acSDave Airlie 		bpp = 32;
3244d8d096eSAlan Cox 
3251b223c9eSAlan Cox 	do {
3261b223c9eSAlan Cox 		/*
3271b223c9eSAlan Cox 		 * Acceleration via the GTT requires pitch to be
3281b223c9eSAlan Cox 		 * power of two aligned. Preferably page but less
3291b223c9eSAlan Cox 		 * is ok with some fonts
3301b223c9eSAlan Cox 		 */
3311b223c9eSAlan Cox         	mode_cmd.pitches[0] =  ALIGN(mode_cmd.width * ((bpp + 7) / 8), 4096 >> pitch_lines);
3324d8d096eSAlan Cox 
333a9a644acSDave Airlie         	size = mode_cmd.pitches[0] * mode_cmd.height;
3344d8d096eSAlan Cox         	size = ALIGN(size, PAGE_SIZE);
3354d8d096eSAlan Cox 
3361b223c9eSAlan Cox 		/* Allocate the fb in the GTT with stolen page backing */
337a6ba582dSAlan Cox 		backing = psbfb_alloc(dev, size);
3381b223c9eSAlan Cox 
3391b223c9eSAlan Cox 		if (pitch_lines)
3401b223c9eSAlan Cox 			pitch_lines *= 2;
3411b223c9eSAlan Cox 		else
3421b223c9eSAlan Cox 			pitch_lines = 1;
3431b223c9eSAlan Cox 		gtt_roll++;
3441b223c9eSAlan Cox 	} while (backing == NULL && pitch_lines <= 16);
3451b223c9eSAlan Cox 
3461b223c9eSAlan Cox 	/* The final pitch we accepted if we succeeded */
3471b223c9eSAlan Cox 	pitch_lines /= 2;
3481b223c9eSAlan Cox 
349a6ba582dSAlan Cox 	if (backing == NULL) {
350a6ba582dSAlan Cox 		/*
351a6ba582dSAlan Cox 		 *	We couldn't get the space we wanted, fall back to the
352a6ba582dSAlan Cox 		 *	display engine requirement instead.  The HW requires
353a6ba582dSAlan Cox 		 *	the pitch to be 64 byte aligned
354a6ba582dSAlan Cox 		 */
355a6ba582dSAlan Cox 
356a6ba582dSAlan Cox 		gtt_roll = 0;	/* Don't use GTT accelerated scrolling */
3571b223c9eSAlan Cox 		pitch_lines = 64;
358a6ba582dSAlan Cox 
359a6ba582dSAlan Cox 		mode_cmd.pitches[0] =  ALIGN(mode_cmd.width * ((bpp + 7) / 8), 64);
360a6ba582dSAlan Cox 
361a6ba582dSAlan Cox 		size = mode_cmd.pitches[0] * mode_cmd.height;
362a6ba582dSAlan Cox 		size = ALIGN(size, PAGE_SIZE);
363a6ba582dSAlan Cox 
3644d8d096eSAlan Cox 		/* Allocate the framebuffer in the GTT with stolen page backing */
3654d8d096eSAlan Cox 		backing = psbfb_alloc(dev, size);
3664d8d096eSAlan Cox 		if (backing == NULL)
3674d8d096eSAlan Cox 			return -ENOMEM;
368a6ba582dSAlan Cox 	}
3694d8d096eSAlan Cox 
370bb849779SAlan Cox 	memset(dev_priv->vram_addr + backing->offset, 0, size);
371bb849779SAlan Cox 
3726b7ce2c4SThomas Zimmermann 	info = drm_fb_helper_alloc_fbi(fb_helper);
373546187c8SArchit Taneja 	if (IS_ERR(info)) {
374546187c8SArchit Taneja 		ret = PTR_ERR(info);
375da7bdda2SDaniel Vetter 		goto out;
3764d8d096eSAlan Cox 	}
3774d8d096eSAlan Cox 
378a9a644acSDave Airlie 	mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
379a9a644acSDave Airlie 
3800a8ec2edSThomas Zimmermann 	fb = psb_framebuffer_create(dev, &mode_cmd, &backing->gem);
3810a8ec2edSThomas Zimmermann 	if (IS_ERR(fb)) {
3820a8ec2edSThomas Zimmermann 		ret = PTR_ERR(fb);
383da7bdda2SDaniel Vetter 		goto out;
3840a8ec2edSThomas Zimmermann 	}
3854d8d096eSAlan Cox 
3866b7ce2c4SThomas Zimmermann 	fb_helper->fb = fb;
3874d8d096eSAlan Cox 
3881b223c9eSAlan Cox 	if (dev_priv->ops->accel_2d && pitch_lines > 8)	/* 2D engine */
3891b223c9eSAlan Cox 		info->fbops = &psbfb_ops;
3901b223c9eSAlan Cox 	else if (gtt_roll) {	/* GTT rolling seems best */
391a6ba582dSAlan Cox 		info->fbops = &psbfb_roll_ops;
392a6ba582dSAlan Cox 		info->flags |= FBINFO_HWACCEL_YPAN;
3931b223c9eSAlan Cox 	} else	/* Software */
394a6ba582dSAlan Cox 		info->fbops = &psbfb_unaccel_ops;
3954d8d096eSAlan Cox 
3964d8d096eSAlan Cox 	info->fix.smem_start = dev->mode_config.fb_base;
3974d8d096eSAlan Cox 	info->fix.smem_len = size;
398a6ba582dSAlan Cox 	info->fix.ywrapstep = gtt_roll;
399a6ba582dSAlan Cox 	info->fix.ypanstep = 0;
4004d8d096eSAlan Cox 
4014d8d096eSAlan Cox 	/* Accessed stolen memory directly */
40237214ca0SKirill A. Shutemov 	info->screen_base = dev_priv->vram_addr + backing->offset;
4034d8d096eSAlan Cox 	info->screen_size = size;
4044d8d096eSAlan Cox 
4054d8d096eSAlan Cox 	if (dev_priv->gtt.stolen_size) {
4064d8d096eSAlan Cox 		info->apertures->ranges[0].base = dev->mode_config.fb_base;
4074d8d096eSAlan Cox 		info->apertures->ranges[0].size = dev_priv->gtt.stolen_size;
4084d8d096eSAlan Cox 	}
4094d8d096eSAlan Cox 
4106b7ce2c4SThomas Zimmermann 	drm_fb_helper_fill_info(info, fb_helper, sizes);
4114d8d096eSAlan Cox 
4124d8d096eSAlan Cox 	info->fix.mmio_start = pci_resource_start(dev->pdev, 0);
4134d8d096eSAlan Cox 	info->fix.mmio_len = pci_resource_len(dev->pdev, 0);
4144d8d096eSAlan Cox 
415fb2a99e1SSascha Hauer 	/* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
4164d8d096eSAlan Cox 
417e18da8ceSThomas Zimmermann 	dev_dbg(dev->dev, "allocated %dx%d fb\n", fb->width, fb->height);
4184d8d096eSAlan Cox 
4194d8d096eSAlan Cox 	return 0;
420da7bdda2SDaniel Vetter out:
4214d8d096eSAlan Cox 	psb_gtt_free_range(dev, backing);
4224d8d096eSAlan Cox 	return ret;
4234d8d096eSAlan Cox }
4244d8d096eSAlan Cox 
4254d8d096eSAlan Cox /**
4264d8d096eSAlan Cox  *	psb_user_framebuffer_create	-	create framebuffer
4274d8d096eSAlan Cox  *	@dev: our DRM device
4284d8d096eSAlan Cox  *	@filp: client file
4294d8d096eSAlan Cox  *	@cmd: mode request
4304d8d096eSAlan Cox  *
4314d8d096eSAlan Cox  *	Create a new framebuffer backed by a userspace GEM object
4324d8d096eSAlan Cox  */
4334d8d096eSAlan Cox static struct drm_framebuffer *psb_user_framebuffer_create
4344d8d096eSAlan Cox 			(struct drm_device *dev, struct drm_file *filp,
4351eb83451SVille Syrjälä 			 const struct drm_mode_fb_cmd2 *cmd)
4364d8d096eSAlan Cox {
4374d8d096eSAlan Cox 	struct drm_gem_object *obj;
4384d8d096eSAlan Cox 
4394d8d096eSAlan Cox 	/*
4404d8d096eSAlan Cox 	 *	Find the GEM object and thus the gtt range object that is
4414d8d096eSAlan Cox 	 *	to back this space
4424d8d096eSAlan Cox 	 */
443a8ad0bd8SChris Wilson 	obj = drm_gem_object_lookup(filp, cmd->handles[0]);
4444d8d096eSAlan Cox 	if (obj == NULL)
4454d8d096eSAlan Cox 		return ERR_PTR(-ENOENT);
4464d8d096eSAlan Cox 
4474d8d096eSAlan Cox 	/* Let the core code do all the work */
4480471c9f5SThomas Zimmermann 	return psb_framebuffer_create(dev, cmd, obj);
4494d8d096eSAlan Cox }
4504d8d096eSAlan Cox 
4516b7ce2c4SThomas Zimmermann static int psbfb_probe(struct drm_fb_helper *fb_helper,
4524d8d096eSAlan Cox 				struct drm_fb_helper_surface_size *sizes)
4534d8d096eSAlan Cox {
4546b7ce2c4SThomas Zimmermann 	struct drm_device *dev = fb_helper->dev;
4553aad16d2SAlan Cox 	struct drm_psb_private *dev_priv = dev->dev_private;
456fd1a5e52SPaul Kocialkowski 	unsigned int fb_size;
4573aad16d2SAlan Cox 	int bytespp;
4584d8d096eSAlan Cox 
4593aad16d2SAlan Cox 	bytespp = sizes->surface_bpp / 8;
4603aad16d2SAlan Cox 	if (bytespp == 3)	/* no 24bit packed */
4613aad16d2SAlan Cox 		bytespp = 4;
4623aad16d2SAlan Cox 
4633aad16d2SAlan Cox 	/* If the mode will not fit in 32bit then switch to 16bit to get
4643aad16d2SAlan Cox 	   a console on full resolution. The X mode setting server will
4653aad16d2SAlan Cox 	   allocate its own 32bit GEM framebuffer */
466fd1a5e52SPaul Kocialkowski 	fb_size = ALIGN(sizes->surface_width * bytespp, 64) *
467fd1a5e52SPaul Kocialkowski 		  sizes->surface_height;
468fd1a5e52SPaul Kocialkowski 	fb_size = ALIGN(fb_size, PAGE_SIZE);
469fd1a5e52SPaul Kocialkowski 
470fd1a5e52SPaul Kocialkowski 	if (fb_size > dev_priv->vram_stolen_size) {
4713aad16d2SAlan Cox                 sizes->surface_bpp = 16;
4723aad16d2SAlan Cox                 sizes->surface_depth = 16;
4733aad16d2SAlan Cox         }
4743aad16d2SAlan Cox 
4756b7ce2c4SThomas Zimmermann 	return psbfb_create(fb_helper, sizes);
4764d8d096eSAlan Cox }
4774d8d096eSAlan Cox 
4783a493879SThierry Reding static const struct drm_fb_helper_funcs psb_fb_helper_funcs = {
4794d8d096eSAlan Cox 	.fb_probe = psbfb_probe,
4804d8d096eSAlan Cox };
4814d8d096eSAlan Cox 
4826b7ce2c4SThomas Zimmermann static int psb_fbdev_destroy(struct drm_device *dev,
4836b7ce2c4SThomas Zimmermann 			     struct drm_fb_helper *fb_helper)
4844d8d096eSAlan Cox {
4856b7ce2c4SThomas Zimmermann 	struct drm_framebuffer *fb = fb_helper->fb;
4864d8d096eSAlan Cox 
4876b7ce2c4SThomas Zimmermann 	drm_fb_helper_unregister_fbi(fb_helper);
488546187c8SArchit Taneja 
4896b7ce2c4SThomas Zimmermann 	drm_fb_helper_fini(fb_helper);
490e18da8ceSThomas Zimmermann 	drm_framebuffer_unregister_private(fb);
491e18da8ceSThomas Zimmermann 	drm_framebuffer_cleanup(fb);
4924d8d096eSAlan Cox 
493e18da8ceSThomas Zimmermann 	if (fb->obj[0])
494e18da8ceSThomas Zimmermann 		drm_gem_object_put_unlocked(fb->obj[0]);
4950a8ec2edSThomas Zimmermann 	kfree(fb);
4960a8ec2edSThomas Zimmermann 
4974d8d096eSAlan Cox 	return 0;
4984d8d096eSAlan Cox }
4994d8d096eSAlan Cox 
5004d8d096eSAlan Cox int psb_fbdev_init(struct drm_device *dev)
5014d8d096eSAlan Cox {
5026b7ce2c4SThomas Zimmermann 	struct drm_fb_helper *fb_helper;
5034d8d096eSAlan Cox 	struct drm_psb_private *dev_priv = dev->dev_private;
50401934c2aSThierry Reding 	int ret;
5054d8d096eSAlan Cox 
5066b7ce2c4SThomas Zimmermann 	fb_helper = kzalloc(sizeof(*fb_helper), GFP_KERNEL);
5076b7ce2c4SThomas Zimmermann 	if (!fb_helper) {
5084d8d096eSAlan Cox 		dev_err(dev->dev, "no memory\n");
5094d8d096eSAlan Cox 		return -ENOMEM;
5104d8d096eSAlan Cox 	}
5114d8d096eSAlan Cox 
5126b7ce2c4SThomas Zimmermann 	dev_priv->fb_helper = fb_helper;
51310a23102SThierry Reding 
5146b7ce2c4SThomas Zimmermann 	drm_fb_helper_prepare(dev, fb_helper, &psb_fb_helper_funcs);
5154d8d096eSAlan Cox 
5166b7ce2c4SThomas Zimmermann 	ret = drm_fb_helper_init(dev, fb_helper, INTELFB_CONN_LIMIT);
51701934c2aSThierry Reding 	if (ret)
51801934c2aSThierry Reding 		goto free;
5194d8d096eSAlan Cox 
5206b7ce2c4SThomas Zimmermann 	ret = drm_fb_helper_single_add_all_connectors(fb_helper);
52101934c2aSThierry Reding 	if (ret)
52201934c2aSThierry Reding 		goto fini;
52376a39dbfSDaniel Vetter 
52476a39dbfSDaniel Vetter 	/* disable all the possible outputs/crtcs before entering KMS mode */
52576a39dbfSDaniel Vetter 	drm_helper_disable_unused_functions(dev);
52676a39dbfSDaniel Vetter 
5276b7ce2c4SThomas Zimmermann 	ret = drm_fb_helper_initial_config(fb_helper, 32);
52801934c2aSThierry Reding 	if (ret)
52901934c2aSThierry Reding 		goto fini;
53001934c2aSThierry Reding 
5314d8d096eSAlan Cox 	return 0;
53201934c2aSThierry Reding 
53301934c2aSThierry Reding fini:
5346b7ce2c4SThomas Zimmermann 	drm_fb_helper_fini(fb_helper);
53501934c2aSThierry Reding free:
5366b7ce2c4SThomas Zimmermann 	kfree(fb_helper);
53701934c2aSThierry Reding 	return ret;
5384d8d096eSAlan Cox }
5394d8d096eSAlan Cox 
540bc7f2b08SKirill A. Shutemov static void psb_fbdev_fini(struct drm_device *dev)
5414d8d096eSAlan Cox {
5424d8d096eSAlan Cox 	struct drm_psb_private *dev_priv = dev->dev_private;
5434d8d096eSAlan Cox 
5446b7ce2c4SThomas Zimmermann 	if (!dev_priv->fb_helper)
5454d8d096eSAlan Cox 		return;
5464d8d096eSAlan Cox 
5476b7ce2c4SThomas Zimmermann 	psb_fbdev_destroy(dev, dev_priv->fb_helper);
5486b7ce2c4SThomas Zimmermann 	kfree(dev_priv->fb_helper);
5496b7ce2c4SThomas Zimmermann 	dev_priv->fb_helper = NULL;
5504d8d096eSAlan Cox }
5514d8d096eSAlan Cox 
5524d8d096eSAlan Cox static const struct drm_mode_config_funcs psb_mode_funcs = {
5534d8d096eSAlan Cox 	.fb_create = psb_user_framebuffer_create,
554cdc13f68SNoralf Trønnes 	.output_poll_changed = drm_fb_helper_output_poll_changed,
5554d8d096eSAlan Cox };
5564d8d096eSAlan Cox 
5574d8d096eSAlan Cox static void psb_setup_outputs(struct drm_device *dev)
5584d8d096eSAlan Cox {
5594d8d096eSAlan Cox 	struct drm_psb_private *dev_priv = dev->dev_private;
5604d8d096eSAlan Cox 	struct drm_connector *connector;
5614d8d096eSAlan Cox 
5624d8d096eSAlan Cox 	drm_mode_create_scaling_mode_property(dev);
5634d8d096eSAlan Cox 
56413619ce5SAlan Cox 	/* It is ok for this to fail - we just don't get backlight control */
56513619ce5SAlan Cox 	if (!dev_priv->backlight_property)
56613619ce5SAlan Cox 		dev_priv->backlight_property = drm_property_create_range(dev, 0,
56713619ce5SAlan Cox 							"backlight", 0, 100);
5684d8d096eSAlan Cox 	dev_priv->ops->output_init(dev);
5694d8d096eSAlan Cox 
5704d8d096eSAlan Cox 	list_for_each_entry(connector, &dev->mode_config.connector_list,
5714d8d096eSAlan Cox 			    head) {
572367e4408SPatrik Jakobsson 		struct gma_encoder *gma_encoder = gma_attached_encoder(connector);
573367e4408SPatrik Jakobsson 		struct drm_encoder *encoder = &gma_encoder->base;
5744d8d096eSAlan Cox 		int crtc_mask = 0, clone_mask = 0;
5754d8d096eSAlan Cox 
5764d8d096eSAlan Cox 		/* valid crtcs */
577367e4408SPatrik Jakobsson 		switch (gma_encoder->type) {
5784d8d096eSAlan Cox 		case INTEL_OUTPUT_ANALOG:
5794d8d096eSAlan Cox 			crtc_mask = (1 << 0);
5804d8d096eSAlan Cox 			clone_mask = (1 << INTEL_OUTPUT_ANALOG);
5814d8d096eSAlan Cox 			break;
5824d8d096eSAlan Cox 		case INTEL_OUTPUT_SDVO:
583cf8efd3aSPatrik Jakobsson 			crtc_mask = dev_priv->ops->sdvo_mask;
5844d8d096eSAlan Cox 			clone_mask = (1 << INTEL_OUTPUT_SDVO);
5854d8d096eSAlan Cox 			break;
5864d8d096eSAlan Cox 		case INTEL_OUTPUT_LVDS:
587d235e64aSAlan Cox 		        crtc_mask = dev_priv->ops->lvds_mask;
5884d8d096eSAlan Cox 			clone_mask = (1 << INTEL_OUTPUT_LVDS);
5894d8d096eSAlan Cox 			break;
5904d8d096eSAlan Cox 		case INTEL_OUTPUT_MIPI:
5914d8d096eSAlan Cox 			crtc_mask = (1 << 0);
5924d8d096eSAlan Cox 			clone_mask = (1 << INTEL_OUTPUT_MIPI);
5934d8d096eSAlan Cox 			break;
5944d8d096eSAlan Cox 		case INTEL_OUTPUT_MIPI2:
5954d8d096eSAlan Cox 			crtc_mask = (1 << 2);
5964d8d096eSAlan Cox 			clone_mask = (1 << INTEL_OUTPUT_MIPI2);
5974d8d096eSAlan Cox 			break;
5984d8d096eSAlan Cox 		case INTEL_OUTPUT_HDMI:
599d235e64aSAlan Cox 		        crtc_mask = dev_priv->ops->hdmi_mask;
6004d8d096eSAlan Cox 			clone_mask = (1 << INTEL_OUTPUT_HDMI);
6014d8d096eSAlan Cox 			break;
602220801bdSAlan Cox 		case INTEL_OUTPUT_DISPLAYPORT:
603220801bdSAlan Cox 			crtc_mask = (1 << 0) | (1 << 1);
604220801bdSAlan Cox 			clone_mask = (1 << INTEL_OUTPUT_DISPLAYPORT);
605220801bdSAlan Cox 			break;
606d112a816SZhao Yakui 		case INTEL_OUTPUT_EDP:
607d112a816SZhao Yakui 			crtc_mask = (1 << 1);
608d112a816SZhao Yakui 			clone_mask = (1 << INTEL_OUTPUT_EDP);
6094d8d096eSAlan Cox 		}
6104d8d096eSAlan Cox 		encoder->possible_crtcs = crtc_mask;
6114d8d096eSAlan Cox 		encoder->possible_clones =
612a3d5d75fSPatrik Jakobsson 		    gma_connector_clones(dev, clone_mask);
6134d8d096eSAlan Cox 	}
6144d8d096eSAlan Cox }
6154d8d096eSAlan Cox 
6164d8d096eSAlan Cox void psb_modeset_init(struct drm_device *dev)
6174d8d096eSAlan Cox {
6184d8d096eSAlan Cox 	struct drm_psb_private *dev_priv = dev->dev_private;
6194d8d096eSAlan Cox 	struct psb_intel_mode_device *mode_dev = &dev_priv->mode_dev;
6204d8d096eSAlan Cox 	int i;
6214d8d096eSAlan Cox 
6224d8d096eSAlan Cox 	drm_mode_config_init(dev);
6234d8d096eSAlan Cox 
6244d8d096eSAlan Cox 	dev->mode_config.min_width = 0;
6254d8d096eSAlan Cox 	dev->mode_config.min_height = 0;
6264d8d096eSAlan Cox 
627e6ecefaaSLaurent Pinchart 	dev->mode_config.funcs = &psb_mode_funcs;
6284d8d096eSAlan Cox 
6294d8d096eSAlan Cox 	/* set memory base */
630dffc9cebSAlan Cox 	/* Oaktrail and Poulsbo should use BAR 2*/
6314d8d096eSAlan Cox 	pci_read_config_dword(dev->pdev, PSB_BSM, (u32 *)
6324d8d096eSAlan Cox 					&(dev->mode_config.fb_base));
6334d8d096eSAlan Cox 
6344d8d096eSAlan Cox 	/* num pipes is 2 for PSB but 1 for Mrst */
6354d8d096eSAlan Cox 	for (i = 0; i < dev_priv->num_pipe; i++)
6364d8d096eSAlan Cox 		psb_intel_crtc_init(dev, i, mode_dev);
6374d8d096eSAlan Cox 
638cbbd379aSPatrik Jakobsson 	dev->mode_config.max_width = 4096;
639cbbd379aSPatrik Jakobsson 	dev->mode_config.max_height = 4096;
6404d8d096eSAlan Cox 
6414d8d096eSAlan Cox 	psb_setup_outputs(dev);
642d235e64aSAlan Cox 
643d235e64aSAlan Cox 	if (dev_priv->ops->errata)
644d235e64aSAlan Cox 	        dev_priv->ops->errata(dev);
6454ab2c7f1SAlan Cox 
6464ab2c7f1SAlan Cox         dev_priv->modeset = true;
6474d8d096eSAlan Cox }
6484d8d096eSAlan Cox 
6494d8d096eSAlan Cox void psb_modeset_cleanup(struct drm_device *dev)
6504d8d096eSAlan Cox {
6514ab2c7f1SAlan Cox 	struct drm_psb_private *dev_priv = dev->dev_private;
6524ab2c7f1SAlan Cox 	if (dev_priv->modeset) {
6534d8d096eSAlan Cox 		drm_kms_helper_poll_fini(dev);
6544d8d096eSAlan Cox 		psb_fbdev_fini(dev);
6554d8d096eSAlan Cox 		drm_mode_config_cleanup(dev);
6564d8d096eSAlan Cox 	}
6574ab2c7f1SAlan Cox }
658